一、对话框模板方式:
1、定义对话框模板
#define sz_Or_Ord WORD
TYPE DLGTEMPLATEEX Field = 4
dlgVer AS WORD
signature AS WORD
helpID AS DWORD
exStyle AS DWORD
style AS DWORD
cDlgItems AS WORD
x AS SHORT
y AS SHORT
cx AS SHORT
cy AS SHORT
menu AS sz_Or_Ord
windowClass AS sz_Or_Ord
title AS WSTRING * 256 'WCHAR
pointsize AS WORD
weight AS WORD
italic AS BYTE
charset AS BYTE
typeface AS WSTRING * 256 'WCHAR
END TYPE
根据自定义内存对话框模板创建窗口
STATIC DialogTemp AS DLGTEMPLATEEX
STATIC hWnd AS HWND
'=================
'Dialog Template
'=================
WITH DialogTemp
.dlgVer = 1 '必须为1
.signature = 65535 '必须为0xFFFF
.helpID = NULL
.exStyle = WS_EX_WINDOWEDGE OR WS_EX_CONTROLPARENT OR WS_EX_TOPMOST
.style = WS_POPUP OR WS_CAPTION OR WS_SYSMENU OR DS_CENTER OR DS_SETFONT
.cDlgItems = 0
.x = 10
.y = 10
.cx = 320
.cy = 200
.menu = 0
.windowClass = 0
.title = “我的对话框模板窗口”
.pointsize = 8
.weight = FW_NORMAL
.italic = FLASE
.charset = GB2312_CHARSET
.typeface = "MS Shell Dlg"
END WITH
'================
hWnd = CreateDialogIndirectParam ( GetModuleHandle ( NULL ), CAST ( LPCDLGTEMPLATEA, @DialogTemp ), NULL, @ShowDIALOG1Proc, CAST ( LPARAM, 0 ) )
ShowWindow ( hWnd, SW_SHOW )
STATIC wMsg AS MSG
WHILE GetMessage ( @wMsg, NULL, 0, 0 )
IF NOT( IsDialogMessage ( hWnd, @wMsg ) ) THEN
TranslateMessage ( @wMsg )
DispatchMessage ( @wMsg )
END IF
WEND
'=====================================
'窗口过程
'=====================================
FUNCTION ShowDIALOG1Proc ( byval hWnd as HWND, byval wMsg as UINT, byval wParam as WPARAM, byval lParam as LPARAM ) AS WINBOOL
SELECT CASE wMsg
CASE WM_INITDIALOG
CASE WM_CTLCOLORDLG
FUNCTION = GetClassLongPtr ( hWnd, GCLP_HBRBACKGROUND )
CASE WM_CLOSE
DestroyWindow ( hWnd )
CASE WM_DESTROY
PostQuitMessage ( 0 )
CASE ELSE
FUNCTION = FALSE
END SELECT
END FUNCTION
二、利用CreateWindowEx直接创建
STATIC hWnd AS HWND
STATIC wcls AS WNDCLASSEX
WITH wcls
.cbSize = SIZEOF ( WNDCLASSEX ) '结构的大小
.style = CS_HREDRAW OR CS_VREDRAW '类样式
.lpfnWndProc = @DefDlgProc '指向窗口过程的指针
.cbClsExtra = 0 '窗口类结构分配的额外字节数
.cbWndExtra = DLGWINDOWEXTRA '(窗口实例之后分配的额外字节数) 把一个对话框模板注册成一个窗口类
.hInstance = GetModuleHandle ( NULL ) '窗口过程的实例的句柄
.hIcon = LoadIcon ( wcls.hInstance, IDI_APPLICATION ) '窗口类图标的句柄
.hCursor = LoadCursor ( NULL, IDC_ARROW ) '窗口游标的句柄
.hbrBackground = CAST ( HBRUSH, GetStockObject ( WHITE_BRUSH ) ) '窗口类背景画笔的句柄
.lpszMenuName = NULL '窗口类菜单的资源名称
.lpszClassName = @"MyDialog" '窗口类名称
.hIconSm = LoadIcon ( wcls.hInstance, IDI_APPLICATION ) '窗口类关联的小图标的句柄
END WITH
RegisterClassEx @wcls
hWnd = CreateWindowEx ( WS_EX_WINDOWEDGE OR WS_EX_TOPMOST, _ '窗口的扩展风格
wcls.lpszClassName, _ '指向注册类名的指针
@“我的对话框模板窗口”, _ '指向窗口名称的指针
WS_POPUP OR WS_CAPTION OR WS_SYSMENU, _ '窗口风格
10, _ '窗口的水平位置
10, _ '窗口的垂直位置
320, _ '窗口的宽度
200, _ '窗口的高度
NULL, _ '父窗口的句柄
NULL, _ '菜单的句柄或是子窗口的标识符
wcls.hInstance, _ '应用程序实例的句柄
0 ) '指向窗口的创建数据
SetWindowLongPtr hWnd, DWLP_USER, CAST ( LONG_PTR, hWnd )
'子类化窗口过程
SetWindowLongPtr hWnd, DWLP_DLGPROC, @ShowDIALOG1Proc
SendMessage hWnd, WM_INITDIALOG, CAST ( WPARAM, hWndParent ), NULL
ShowWindow ( hWnd, SW_SHOW )
STATIC wMsg AS MSG
WHILE GetMessage ( @wMsg, NULL, 0, 0 )
TranslateMessage ( @wMsg )
DispatchMessage ( @wMsg )
WEND
'=====================================
'窗口过程
'=====================================
FUNCTION ShowDIALOG1Proc ( byval hWnd as HWND, byval wMsg as UINT, byval wParam as WPARAM, byval lParam as LPARAM ) AS WINBOOL
SELECT CASE wMsg
CASE WM_INITDIALOG
CASE WM_CTLCOLORDLG
FUNCTION = GetClassLongPtr ( hWnd, GCLP_HBRBACKGROUND )
CASE WM_CLOSE
DestroyWindow ( hWnd )
CASE WM_DESTROY
PostQuitMessage ( 0 )
CASE ELSE
FUNCTION = FALSE
END SELECT
END FUNCTION
三、总计:
上面演示了两种非模式对话框窗口的不同创建方式。第一种默认创建不能通过双击鼠标获得最大化窗口,第二种方式可以。