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);

MFC 簡介

MFC Introduction

Contents:

1. Some Background on Object-Oriented Programming

2. A Little History

3. A Grand Tour of MFC

(i) CObject

(ii) CWnd

(iii) CWinApp / CWinThread

(iv) CString

(v) Remaining

4. A Simple Code about Dialog Style Application


Preface

MFC (Microsoft Foundation Classes) is a framework for Windows programming, and there are other ones like ATL (Active Template Library) and WTL (Windows Template Library) from Microsoft.

Some Background on Object-Oriented Programming

OOP Terminology

1. Abstraction

2. Encapsulation

3. Inheritance

4. Polymorphism

5. Modularity

A Little History

1989 – Microsoft establish the application framework technology development group (AFX group, and you will see many functions with prefix “AFX”) (Legend has it that “AF” didn’t sound so great by itself, so the threw in the letter X to complete the acronym, The X doesn’t really mean anything)

1992 – Microsoft released MFC 1.0

1993/04 – Microsoft released VC++ 1.0 with MFC 2.0

1993 late – VC++ 1.5 with MFC 2.5

1994 late – MFC 3.0

1995 – VC++ 4.0 with MFC 4.0 (the last version is MFC 4.2)

1995 ~ 200x – MFC 5.0, 6.0 Unknow

2002 - Visual Studio .Net 2003 (VC++ 7.0) with MFC 7.0

Future - Visual Studio .Net 2005 (VC++ 8.0) with MFC 8.0

A Grand Tour of MFC

CObject: The Mother of (Almost) All Classes

It is MFC’s strategy, like JAVA Object. Classes derived from CObject inherit some useful capabilities, including run-time type identification for derived class, serialization (that is, persistence), diagnostic functions, and support for dynamic object creation.

CWnd: The Mother of All Windows

CWnd serves as a breeding ground for all of Windows’ visual interface objects like dialog box and controls (button, list box, edit and so on).

Dialogs:

CFileDialog – Selects a file from a directory.

CColorDialog – Selects a specific color.

CFontDialog – Selects a font.

CPrintDialog – Handles printer setup and printing.

CFindReplaceDialog – Selects text to search and replace.

Most important skill for Dialog: (In MFC, I only like this)

Dialog Data Exchange and Validation

CWinApp / CWinThread

CwinThread represents a thread of execution within an MFC program. Though earlier versions of MFC weren’t thread safe, version 3.0 and later are.

CWinApp, which is derived from CWinThread, represents the standard Windows application. CWinApp has overrideable functions you can use to initialize your app and perform termination cleanup to suit your own needs.

CString

Creating strings with the CString class is a snap. MFC’s CString class provides basic operations such as concatenation, comparison, and assignments.

Remaining:

1. GDI Support and Drawing Object – See GDI topic in MSDN

2. Document/View Architecture – like MVC concept.

3. OLE Support: OLE Control

4. ODBC Support / DAO Support

A Simple Code about Dialog Style Application

See HelloWorld project

The Key point for Dialog

1. The disagreeable buttons: OK and Cancel

They are very convenient for dialog box, but not for application. Because of terminating application when user clicks OK or Cancel (presses Enter or ESC), we have to modify these undesired codes.

Solution 1: Modify the message mapping (I don’t like it).

Solution 2: Modify the virtual function and add new message mapping.

2. The Message MAP: the macro instructions for message map.

(i) DECLARE_MESSAGE_MAP(), BEGIN_MESSAGE_MAP / END_MESSAGE_MAP()

(ii) Combine the Windows message with your routine

(iii) Don’t add any extra codes in the Message Map declaration

3. DDX / DDV (Dialog Data Exchange / Validation)

(i) DoDataExchange(CDataExchange* pDX)

(ii) The DDX_ and DDV_ macro instructions

(iii) UpdateData(true) and UpdateData(false)

4. WM_INITDIALOG / OnInitDialog()

When a dialog is created, Windows sends a WM_INITDIALOG message just before the dialog is displayed so that the application can initialize any controls in the dialog before they become visible to the end user.

5. GetDlgItem: To get handle of child window

The HWND is the key to control any windows (including button and etc). If you want access unknown windows, you just find their handle (HWND) and call some windows function (like SetWindowText) with it.

Windows Programming 簡介

Windows Programming Introduction

Contents:

1. Windows History

2. Aspect of Windows

3. Windows Programming with Pure API

(i) A simple code with MessageBox

(ii) A simple code with CreateWindow

Preface

The word “Windows” means that Microsoft Windows OS in the text.

Windows History

1983/11 – Microsoft announce Windows

1985/11 – Microsoft release Windows 1.0

1987/11 – Windows 2.0

1990/05/02 – Windows 3.0 (16-bit protected-mode)

1992/04 – Windows 3.1 (True Type Font, OLE)

1993/07 - Window NT

1995/08 – Windows 95

1998/06 – Window 98

Aspect of Windows

1. Both Window 98 and NT are 32-bit preemptive multitasking and multithreading graphical operation system.

* Earlier versions of Windows used a system of multitasking called “non-preemptive”. This meant that Windows did not use the system timer to slice processing time between the various programs running under the system.

2. Programs running in Windows can share routines that are located in other files called “dynamic-link libraries”. Windows includes a mechanism to link the program with the routines in the dynamic-link libraries.

Kernel: krnl386.exe for 16-bit, and kernel32.dll for 32-bit.

Kernel handles OS kernel traditional routines like memory management, I/O, and tasking.

User: user.exe for 16-bit, and user32.dll for 32-bit

User refers to the user interface, and implements all the windowing logic.



GDI: gdi.exe for 16-bit, and gdi32.dll for 32-bit.

GDI is the Graphics Device Interface, which allows a program to display text and graphics on the screen and printer.

Windows Programming with Pure API


A very simple code with MessageBox

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, PSTR szCmdLine, int nCmdShow)

{

MessageBox(NULL, TEXT("Hello World!"), TEXT("First Example"), MB_OK | MB_ICONINFORMATION);

return 0;

}

1. The Header Files

The “windows.h” is a master include file that includes other Windows header files. And the most important and most basic of these header files are:

windef.h: Basic type definitions

winnt.h: Type definitions for Unicode support.

winbase.h: Kernel functions.

winuser.h: User interface functions.

wingdi.h: Graphics device interface functions.

2. Program Entry Point

WinMain:

hInstance

[in] Handle to the current instance of the application.

hPrevInstance

[in] Handle to the previous instance of the application. This parameter is always NULL. If you need to detect whether another instance already exists, create a uniquely named mutex using the CreateMutex function. CreateMutex will succeed even if the mutex already exists, but the function will return ERROR_ALREADY_EXISTS. This indicates that another instance of your application exists, because it created the mutex first.

lpCmdLine

[in] Pointer to a null-terminated string specifying the command line for the application, excluding the program name. To retrieve the entire command line, use the GetCommandLine function.

nCmdShow

[in] Specifies how the window is to be shown. This parameter can be one of the following values.

See MSDN

A very simple code with CreateWindow

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, PSTR szCmdLine, int nCmdShow)

{

static TCHAR szAppName[] = TEXT("Hello Window");

HWND hWnd;

MSG msg;

WNDCLASS wndclass;

wndclass.cbClsExtra = 0;

wndclass.cbWndExtra = 0;

wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);

wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

wndclass.hInstance = hInstance;

wndclass.lpfnWndProc = WndProc;

wndclass.lpszClassName = szAppName;

wndclass.lpszMenuName = NULL;

wndclass.style = CS_HREDRAW | CS_VREDRAW;

if(!RegisterClass(&wndclass))

{

MessageBox(0, TEXT("This program requires Windows NT!"), szAppName, MB_OK | MB_ICONERROR);

return 0;

}// if(!RegisterClass(&wndclass))

hWnd = CreateWindow(szAppName,

TEXT("The Hello Program"),

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

NULL,

NULL,

hInstance,

NULL);

ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

while(GetMessage(&msg, NULL, 0, 0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}// while(GetMessage(&msg, NULL, 0, 0))

return msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

{

HDC hDC;

PAINTSTRUCT ps;

RECT rect;

switch(msg)

{

case WM_CREATE:

return 0;

case WM_PAINT:

hDC = BeginPaint(hWnd, &ps);

GetClientRect(hWnd, &rect);

DrawText(hDC, TEXT("Hello World~~~~"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

EndPaint(hWnd, &ps);

return 0;

case WM_DESTROY:

PostQuitMessage(0);

return 0;

}// switch(msg)

return DefWindowProc(hWnd, msg, wParam, lParam);

}

An Architectural Overview

* Remark: “Windows sends a message to the program” is meant that Widnows calls a function within the program – a function that you write and which is an essential part of your program’s code.

1. A windows including sub-windows like button, radio box, check box, and etc. is always created based on a “window class”.

2. When a Windows program begins execution, Windows creates a “message queue” for the program.

3. Message Loop and WndProc

WM_CREATE:

WM_PAINT:

WM_DESTROY:

PostQuitMessage(0) -> return msg.wParam