2024年4月21日发(作者:)

VC中WM_CREATE、WM_INITDIALOG消息出现顺序及调用方式

wxleasyland@

2012.7

wxleasyland试验:

VC建的标准WIN32-HELLO WORLD程序中:

1.在CreateWindow()前面加上语句WinExec("cmd /k echo now call

CreateWindow()",SW_SHOW); Sleep(5000);

2.在CreateWindow()后面加上语句WinExec("cmd /k echo CreateWindow()

RETURN",SW_SHOW);

3.在WndProc()中加上

case WM_CREATE:

WinExec("cmd /k echo WndProc(): WM_CREATE: appear!",SW_SHOW);

Sleep(5000);

break;

4.运行,发现,先出现now call CreateWindow()、再WndProc(): WM_CREATE: appear!、

最后CreateWindow() RETURN。

结论:

CreateWindow()是在运行后、返回前,发送消息WM_CREATE,并阻塞!

WM_CREATE消息直接发给消息处理函数WndProc()执行!而不是通过消息队列

(GetMessage、DispatchMessage等)!!

其实就是CreateWindow()直接拿消息WM_CREATE去调用WndProc()!!!!!

如果WndProc()返回0,CreateWindow()正常返回hWnd。

如果WndProc()返回-1,CreateWindow()销毁窗口,并返回NULL。

所以WM_CREATE时,CreateWindow()内部已经创建好窗口了,但还没有返回,还没有

hWnd!即窗口真正还没有创建结束。这时这里不能有对窗口上的控件操作的函数,不然

就会“程序执行了非法操作”。比如GetDlgItem等。

标准WIN32程序中,是先RegisterClassEx()注册窗口类->再CreateWindow()创建窗

口->再ShowWindow()显示出窗口->再UpdateWindow()立即用WM_PAINT刷新窗口->

再LoadAccelerators()->再进入消息循环(GetMessage、DispatchMessage等)

在MFC对话框中,试验,是:先WM_CREATE,这时窗口未显示出来->再WM_INITDIALOG,

这时窗口仍未显示出来-> OnInitDialog()运行完毕后,窗口才显示出来(应是默认通

过ShowWindow())。

====================================

h p://o .com/en-us/library/windows/desktop/ms632619(v=vs.85).aspx

WM_CREATE message

Sent when an application requests that a window be created by calling the

CreateWindowEx or CreateWindow function. (The message is sent before the

function returns.这个消息在函数返回前被发送) The window procedure of the

new window receives this message after the window is created, but before the window

becomes visible.

A window receives this message through its WindowProc function. 窗口通过

WindowProc函数来接收这个消息!

If an application processes this message, it should return zero to continue

creation of the window. If the application returns –1, the window is destroyed

and the CreateWindowEx or CreateWindow function returns a NULL handle. 处理

这个消息的程序应返回0,如果返回-1,则窗口被销毁并且CreateWindow返回NULL。

CreateWindow function

Creates an overlapped, pop-up, or child window. It specifies the window class,

window title, window style, and (optionally) the initial position and size of the window.

The function also specifies the window's parent or owner, if any, and the window's

menu.

Before returning, CreateWindow sends a WM_CREATE message to the window

procedure. 在CreateWindow返回前,它会发送WM_CREATE给窗口处理程序。

For overlapped, pop-up, and child windows, CreateWindow sends

WM_CREATE, WM_GETMINMAXINFO, and WM_NCCREATE messages to the

window. The lParam parameter of theWM_CREATE message contains a pointer

to a CREATESTRUCT structure. If the WS_VISIBLE style is specified,

CreateWindow sends the window all the messages required to activate and show the

window.

WM_NCCREATE message

Sent prior to the WM_CREATE message when a window is first created. 在

WM_CREATE之前被发送。