2008年4月2日 星期三

Common Dialog and Controls 簡介

Common Dialog and Controls Introduction

Contents:

1. Common Controls

(i) Button

(ii) Edit / Static Text

(iii) Check Box

(iv) Combo Box

2. Common Dialog

3. Modal / Modeless

4. Child Window

Preface

Key concept: All Controls Are Window.

Common Controls

Remark: GetDlgItem, DDX/DDV, Message Map

1. Button:

(i) ON_BN_CLIECKED(Control ID, Function)



in your header file, may find something like following:

BEGIN_MESSAGE_MAP(CClass3Dlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON6, OnBnClickedButton6)
ON_BN_CLICKED(IDC_BUTTON7, OnBnClickedButton7)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON8, OnBnClickedButton8)
END_MESSAGE_MAP()



(ii) See WM_COMMAND and BN_CLICKED in MSDN.

(iii) When you click a button, the child window control (button) sends WM_COMMAND message to its parent window (Main window). And BN_CLICKED (notification code) is in HIWORD of WParam.

- LOWORD(wParam) Child window ID

- HIWORD(wParam) Notification code

- lParam Child window Handle

2. Static Text:

(i) For Showing text

3. Edit Control:

(i) For inputting text

(ii) Multi-Line / Want Return

(iii) Controlling it like window

4. Check Box:

(i) For Boolean

Common Dialogs

1. CFileDialog: Open/Save file

2. CFontDialog: Choose font type and style

3. CColorDialog: Choose color

4. CPrintDialog: Set printer

Modal / Modeless

1. Modal (DoModal)

(i) Loading the dialog resource

(ii) Disabling (freezing) parent and stopping it from getting any messages.

(iii) Creating and displaying - RunModalLoop

(iv) Destroy window after user hit “OK” or “Cancel”.


code like following:

CDemo demo; // CDemo inherts CDialog

if(IDOK == demo.DoModal())
{
CString tmp;

tmp.Format(TEXT(" edit1 = %sn edit2 = %dn check = %dn combo = %sn"),
demo.m_sEdit1, demo.m_nEdit2, demo.m_bCheck, demo.m_sCombo);

MessageBox(tmp);
}// if(IDOK == demo.DoModal())


2. Modeless

(i) Using new operator and then call CDialog::Create() to initialize and display with ShowWindow(SW_SHOW).

(ii) Using EndDialog() to terminate (not destroy or delete) modeless dialog.

(iii) Destroying and deleting it finally.

code like following:

show dialog:

if(about == 0)
{
about = new CAboutDlg();
about->Create(IDD_ABOUTBOX);
}

about->ShowWindow(SW_SHOW);



destroy dialog:

if(about != 0)
{
about->EndDialog(0);
BOOL B = about->DestroyWindow();
delete about;
about = 0;
}




ChildWindow

1. Remember it: All Controls Are Child Windows.

2. If you want to create a window to embed it into a window (parent window).

3. How to embed a window in the other.

(i) Add a FORMVIEW into resource and create a class for it.

(ii) Generate instance with new operator and call Create to initial it.

(iii) Move the window to specific location.

(iv) Show it!!!!


code like following:
child = new CTestChild(this);
child->Create(IDD_FORMVIEW, this);

RECT rect = {0}, rect1 = {0};

this->GetClientRect(&rect);
child->GetWindowRect(&rect1);

int w = rect1.right - rect1.left;
int h = rect1.bottom - rect1.top;

rect1.top = rect.bottom - h - 5;
rect1.bottom = rect1.top + h;

child->MoveWindow(&rect1);

child->ShowWindow(SW_SHOW);

張貼留言