定义对话框模板后,再定义对话框回调函数并调用DialogBoxIndirectParam建立对话框
例:
static int InitDialogBoxProc(HWND hDlg,int message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case MSG_INITDIALOG:
return 1;
case MSG_COMMAND:
switch(wParam)
{
case IDOK:
case IDCANCEL:
EndDialog(hDlg,wParam);
break;
}
break;
}
return DefaultDialogProc(hDlg,message,wParam,lParam);
}
static void InitDialogBox(HWND hWnd)
{
DlgInitProcess.controls=CtrlInitProcess;
DialogBoxIndirectParam(&DlgInitProcess,hWnd,InitDialogBoxProc,0L);
}
创建对话框函数
int GUIAPI DialogBoxIndirectParam(PDLGTEMPLATE pDlgTemplate,HWND hOwner,WNDPROC DlgProc,LPARAM lParam);
参数:对话框模板,托管主窗口句柄,回调函数地址,附加参数值
BOOL GUIAPI EndDialog(HWND hDlg,int endcode);
结束对话框过程
void GUIAPI DestroyAllControls(HWND hDlg);
销毁对话框中所有子控件
创建非模态对话框
HWND GUIAPI CreateMainWindowIndirect(PDLGTEMPLATE pDlgTemplate,HWND hOwner,WNDPROC WndProc);
BOOL GUIAPI DestroyMainWindowIndirect(HWND hMainWin);
例:
static int InitWindowProc(HWND hDlg,int message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case MSG_COMMAND:
swtich(wParam)
{
case IDOK:
case IDCANCEL:
DestroyMainWindowIndirect(hWnd);
break;
}
break;
}
return DefaultWindowProc(hDlg,message,wParam,lParam);
}
...
{
HWND hwnd;
MSG msg;
DlgInitProcess.controls=CtrlInitProcess;
hwnd=CreateMainWindowIndirect(&DlgInitProcess,HWND_DESKTOP,InitWindowProc);
if(hwnd==HWND_INVALID)
return -1;
while(GetMessage(&msg,hwnd))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
对话框控件风格
WS_GROUP 成为同组控件头一个
WS_TABSTOP 支持TAB切换
对话框操作函数:
DestroyAllControls
GetDlgCtrlID
GetDlgItem
GetDlgItemInt
SetDlgItemInt
GetDlgItemText
GetDlgItemText2
SetDlgItemText
GetNextDlgGroupItem
GetNextDlgGroupItem
SendDlgItemMessage
CheckDlgButton
CheckRadioButton
IsDlgButtonChecked
GetDlgDefPushButton
MinGUI预定义控件类:
"static" CTRL_STATIC
"button" CTRL_BUTTON
"sledit" CTRL_SLEDIT (Single Line)
"mledit" CTRL_MLEDIT (Multi Line)
"textbox" CTRL_TEXTBOX
"listbox" CTRL_LISTBOX
"progressbar" CTRL_PROGRESSBAR
"trackbar" CTRL_TRACKBAR
"combobox" CTRL_COMBOBOX
"newtoolbar" CTRL_NEWTOOLBAR
"menubutton" CTRL_MENUBUTTON
"propsheet" CTRL_PROPSHEET
"ScrollWnd" CTRL_SCROLLWND
"ScrollView" CTRL_SCROLLVIEW
"treeview" CTRL_TREEVIEW
"listview" CTRL_LISTVIEW
"MonthCalendar" CTRL_MONTHCALENDAR
"SpinBox" CTRL_SPINBOX
"CoolBar" CTRL_COOLBAR
"IconView" CTRL_ICONVIEW
"gridview" CTRL_GRIDVIEW
"Animation" CTRL_ANIMATION
调用CreateWindow/CreateWindowEx创建预定义控件类的实例
HWND GUIAPI CreateWindowEx(const char*spClassName,const char*spCaption,DWORD dwStyle,DWORD dwExStyle,int id,int x,int y,int w,int h,HWND hParentWnd,DWORD dwAddData);
BOOL GUIAPI DestroyWindow(HWND hWnd);
#define CreateWindow(class_name,caption,style,id,x,y,w,h,parent,add_data) CreateWindowEx(class_name,caption,style,0,id,x,y,w,h,parent,add_data)
参数:
控件类,标题,风格,标识符,位置,父窗口,附加数据
例:
#define IDC_STATIC1 100
#define IDC_STATIC2 150
#define BUTTON1 110
#define BUTTON2 120
#define EDIT1 130
#define EDIT2 140
//创建一个静态框
hStaticWnd1=CreateWindow(CTRL_STATIC,"This is a static control",WS_CHILD|WS_VISIBLE|WS_BORDER|SS_NOTIFY|SS_SIMPLE,
IDC_STATIC1,
10,10,180,300,
hWnd,
0);
//在hStaticWnd1中创建两个按钮
hButton1=CreateWindow(CTRL_BUTTON,"button1",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
IDC_BUTTON1,
20,20,80,20,
hStaticWnd1,
0);
hButton2=CreateWindow(CTRL_BUTTON,"button2",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
IDC_BUTTON2,
20,50,80,20,
hStaticWnd1,
0);
//在hStaticWnd1中创建一个编辑框
hEdit1=CreateWindow(CTRL_EDIT,"edit box 1",
WS_CHILD|WS_VISIBLE|WS_BORDER,
IDC_EDIT1,
20,80,100,24,
hStaticWnd1,
0);
//在hStaticWnd1中创建一个静态框
hStaticWnd2=CreateWindow(CTRL_STAIC,"This is a child static control",
WS_CHILD|WS_VISIBLE|WS_BORDER|SS_NOTIFY|SS_SIMPLE,
IDC_STATIC2,
20,110,100,50,
hStaticWnd1,
0);
//在hStaticWnd2中创建一个编辑框(为hStaticWnd1的孙窗口)
hEdit2=CreateWindow(CTRL_EDIT,"edit box 2",
WS_CHILD|WS_VISIBLE|WS_BORDER,
IDC_EDIT2,
0,20,100,24,
hStaticWnd2,
0);
控件内部发生事件时,向父窗口发送MSG_COMMAND消息
该消息的wParam由子窗口标识符和通知码组成
lParam含有发出通知消息的控件句柄
如:
编辑框向父窗口发送EN_CHANGE消息
switch(message)
{
case MSG_COMMAND:
int id=LOWORD(wParam);
int nc=HIWORD(wParam);
if(id==ID_MYEDIT&&nc==EN_CHANGE)
{...
}
break;
}
设置控件通知消息回调函数
SetNotificationCallback
例:
编辑框的回调函数
static void notif_proc(HWND hwnd,int id,int nc,DWORD add_data)
当对话框中有多个控件时要分别定义每一个控件的回调函数
并在对话框初始话消息时设置
case MSG_INITDIALOG:
SetNotificationCallback(GetDlgItem(hDlg,IDC_SIZE_MM),notif_proc);
控件子类化,定制特殊的控件
#define IDC_CTRL1 100
#define IDC_CTRL2 110
#define IDC_CTRL3 120
#define IDC_CTRL4 130
#define MY_ES_DIGIT_ONLY 0x0001
#define MY_ES_ALPHA_ONLY 0x0002
staic WNDPROC old_edit_proc;