在Windows系统中几乎我们看到的元素都是窗体,通常我们将带有WS_OVERLAPPED或WS_POPUP风格的窗体被称之为顶层窗体,而WS_CHILD风格属性的窗体称之为控件如: Button, ListBox, CommboBox, TreeView等; 为了创建窗体以及控件Window提供了二个函数:
(一)创建窗体
HWND CreateWindowEx(DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWinName, DWORD dwStyle, int x, int y, int nWidth, int nHeight,
HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
HWND CreateWindow(LPCTSTR lpClassName, LPCTSTR lpWinName, DWORD dwStyle, int x, int y, int nWidth, int nHeight,
HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
此函数是CreateWindowEx更高些的函数, 缺少窗体的扩展风格的设置
以下代码我们分别演示两种窗体的创建
a. 顶层窗体
HWND
hWnd
=
CreateWindow
(
szWndClass
,
szWndCaptoin
,
dwStyle
, 0, 0, 800, 480,
NULL
,
NULL
,
hInstance
, (
LPVOID
)7000);
if
(
NULL
!=
hWnd
&&
IsWindow
(
hWnd
))
{
ShowWindow
(
hWnd
,
SW_SHOW
);
UpdateWindow
(
hWnd
);
bCreated
=
true
;
}
b. 控件
//--: Create sub control
hButton
=
CreateWindow
(
_T
(
"bUTTON"
),
_T
(
"&Exit"
),
WS_VISIBLE
|
WS_CHILD
, 0, 0, 100, 50,
hWnd
, (
HMENU
)1001,
NULL
, 0);
(二)创建过程中触发的消息
在窗体创建过程中我们将会依次触发几个主要消息(当然还会有别的消息):
1. WM_NCCREATE
2. WM_CREATE
3. WM_SHOWWINDOW
我们看看验证代码:
switch
(
uMsg
)
{
case
WM_NCCREATE
:
{
TCHAR
szInfo
[256];
CREATESTRUCT
*
pcs
= (
CREATESTRUCT
*)
lParam
;
_stprintf
(
szInfo
,
_T
(
"\nCreate Step: %d (WM_NCCREATE) lParam: [Instance: 0x%0X CreateParams: %d Size: (%d, %d, %d, %d)]\n"
),
++
nStep
,
pcs
->
hInstance
,
pcs
->
lpCreateParams
,
pcs
->
x
,
pcs
->
y
,
pcs
->
cx
,
pcs
->
cy
);
OutputDebugString
(
szInfo
);
break
;
}
case
WM_CREATE
:
{
TCHAR
szInfo
[256];
CREATESTRUCT
*
pcs
= (
CREATESTRUCT
*)
lParam
;
_stprintf
(
szInfo
,
_T
(
"\nCreate Step: %d (WM_CREATE) lParam: [Instance: 0x%0X CreateParams: %d Size: (%d, %d, %d, %d)]\n"
),
++
nStep
,
pcs
->
hInstance
,
pcs
->
lpCreateParams
,
pcs
->
x
,
pcs
->
y
,
pcs
->
cx
,
pcs
->
cy
);
OutputDebugString
(
szInfo
);
//--: Create sub control
hButton
=
CreateWindow
(
_T
(
"bUTTON"
),
_T
(
"&Exit"
),
WS_VISIBLE
|
WS_CHILD
, 0, 0, 100, 50,
hWnd
, (
HMENU
)1001,
NULL
, 0);
break
;
}
case
WM_SHOWWINDOW
:
{
TCHAR
szInfo
[256];
_stprintf
(
szInfo
,
_T
(
"\nCreate Step: %d (WM_SHOWWINDOW)\n"
),
++
nStep
);
OutputDebugString
(
szInfo
);
break
;
}
执行后结果:
Create Step: 1 (WM_NCCREATE) lParam: [Instance: 0x8C0000 CreateParams: 7000 Size: (0, 0, 800, 480)]
¡°\uc1Demo0021.exe¡±: ÒѼÓÔØ¡°\uc1C:\Windows\System32\dwmapi.dll¡±
Create Step: 2 (WM_CREATE) lParam: [Instance: 0x8C0000 CreateParams: 7000 Size: (0, 0, 800, 480)]
Create Step: 3 (WM_SHOWWINDOW)
可以看到,我们在没有看到窗体时会触发二个消息(WM_NCWREATE, WM_CREATE)
这两个消息可以lParam参数指向一个CREATESTRUCT类型的指针,其CREATESTRUCT成员LPVOID lpCreateParams 接收创建函数的最后一个参数传递过去的参数.
演示代码