#include "stdafx.h"
#include "windows.h"
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
HWND hWnd;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
#include "windows.h"
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
HWND hWnd;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
WNDCLASS win ;
win.style = CS_HREDRAW | CS_VREDRAW ;
win.lpfnWndProc = WinProc;
win.cbClsExtra = 0;
win.cbWndExtra = 0;
win.hInstance = hInstance;
win.hIcon = NULL ;
win.hCursor = NULL;
win.hbrBackground = CreateSolidBrush(RGB(100,0,0));
win.lpszMenuName = NULL;
win.lpszClassName = "My first dialog";
if(!RegisterClass(&win))
{
MessageBox(NULL,"rigister failure","My first dialog",MB_OK);
return 0;
}
win.style = CS_HREDRAW | CS_VREDRAW ;
win.lpfnWndProc = WinProc;
win.cbClsExtra = 0;
win.cbWndExtra = 0;
win.hInstance = hInstance;
win.hIcon = NULL ;
win.hCursor = NULL;
win.hbrBackground = CreateSolidBrush(RGB(100,0,0));
win.lpszMenuName = NULL;
win.lpszClassName = "My first dialog";
if(!RegisterClass(&win))
{
MessageBox(NULL,"rigister failure","My first dialog",MB_OK);
return 0;
}
hWnd = CreateWindow("my first dialog",
" my first dialog",
WS_POPUP,0,0,
100,
100,
NULL,NULL,hInstance,NULL);
if(!hWnd)
{
MessageBox(NULL,"OH NO","My first dialog",MB_OK);
}
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
//Sleep(10000);
return 0;
}
" my first dialog",
WS_POPUP,0,0,
100,
100,
NULL,NULL,hInstance,NULL);
if(!hWnd)
{
MessageBox(NULL,"OH NO","My first dialog",MB_OK);
}
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
//Sleep(10000);
return 0;
}
//////窗口处理函数
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
{
switch(message)
{
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:MessageBox(hWnd,"ESC","KEYBOARD",MB_OK);
PostMessage(hWnd,WM_CLOSE,0,0);
{
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:MessageBox(hWnd,"ESC","KEYBOARD",MB_OK);
PostMessage(hWnd,WM_CLOSE,0,0);
}
return 0;
case WM_CLOSE:DestroyWindow(hWnd);return 0;
case WM_DESTROY:PostQuitMessage(0);return 0;
}
// MessageBox(NULL,"OH NO","My first dialog",MB_OK);
return DefWindowProc(hWnd,message,wParam,lParam);
return 0;
case WM_CLOSE:DestroyWindow(hWnd);return 0;
case WM_DESTROY:PostQuitMessage(0);return 0;
}
// MessageBox(NULL,"OH NO","My first dialog",MB_OK);
return DefWindowProc(hWnd,message,wParam,lParam);
}
按照上面的这段代码,成功创建的窗口会一闪而过,因为这个程序的主线程即WinMain()函数执行完毕,窗口也会跟着主线程的结束而消失,所以要让窗口长时间显示出来,就要让WinMain函数不那么快就结束,如果在代码中加入Sleep函数,窗口就可以显示出来了~~
因此可以在WinMain()函数里加入消息循环处理,直到主线程接收到窗口关闭的消息(WM_QUIT)后,才会跳出循环,结束主线程,随之窗口才消失,这就实现了用消息机制去控制窗口的消失。
在UpdateWindow(hWnd)后加上下面这段代码:
MSG msg;
for(;;)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
for(;;)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
进入这段代码后,主线程进入等待消息的循环
PeekMessage函数的作用是用来从应用程序的消息队列中按照先进先出的原则将这些消息逐个取出来,放进一个Msg结构中去。如果队列中没有任何消息,PeekMessage函数将立即返回;如果队列中有消息,它将取出一个后返回。
BOOL PeekMessage(
LPMSG lpMsg , // message information
HWND hWnd , // handle to window
UINT wMsgFilterMin , // first message
UINT wMsgFilterMax , // last message
UINT wRemoveMsg // removal options
);
LPMSG lpMsg , // message information
HWND hWnd , // handle to window
UINT wMsgFilterMin , // first message
UINT wMsgFilterMax , // last message
UINT wRemoveMsg // removal options
);
返回值:If a message is available, the return value is nonzero.
If no messages are available, the return value is zero.
PeekMessage retrieves only messages associated with the window identified by the
hWnd parameter or any of its children as specified by the
IsChild function, and within the range of message values given by the
wMsgFilterMin and
wMsgFilterMax parameters. If
hWnd is NULL,
PeekMessage retrieves messages for any window that belongs to the current thread making the call.
PeekMessage does not retrieve messages for windows that belong to other threads.
TranslateMessage函数的作用是把虚拟键消息转换到字符消息,以满足键盘输入的需要。DispatchMessage函数的作用是把当前的消息发送到对应的窗口过程(窗口处理函数)中去。
最终程序:
// win2.cpp : Defines the entry point for the application.
//
//
#include "stdafx.h"
#include "windows.h"
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
HWND hWnd;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
#include "windows.h"
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
HWND hWnd;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
WNDCLASS win ;
win.style = CS_HREDRAW | CS_VREDRAW ;
win.lpfnWndProc = WinProc;
win.cbClsExtra = 0;
win.cbWndExtra = 0;
win.hInstance = hInstance;
win.hIcon = NULL ;
win.hCursor = LoadCursor (NULL, IDC_ARROW) ;
win.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);//CreateSolidBrush(RGB(100,0,0));
win.lpszMenuName = NULL;
win.lpszClassName = "My first dialog";
if(!RegisterClass(&win))
{
MessageBox(NULL,"rigister failure","My first dialog",MB_OK);
return 0;
}
win.style = CS_HREDRAW | CS_VREDRAW ;
win.lpfnWndProc = WinProc;
win.cbClsExtra = 0;
win.cbWndExtra = 0;
win.hInstance = hInstance;
win.hIcon = NULL ;
win.hCursor = LoadCursor (NULL, IDC_ARROW) ;
win.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);//CreateSolidBrush(RGB(100,0,0));
win.lpszMenuName = NULL;
win.lpszClassName = "My first dialog";
if(!RegisterClass(&win))
{
MessageBox(NULL,"rigister failure","My first dialog",MB_OK);
return 0;
}
hWnd = CreateWindow("my first dialog",
"my first dialog",
WS_POPUP|WS_BORDER|WS_CAPTION|WS_MAXIMIZEBOX|WS_MINIMIZEBOX,0,0,
100,
100,
NULL,NULL,hInstance,NULL);
if(!hWnd)
{
MessageBox(NULL,"OH NO","My first dialog",MB_OK);
return 0;
}
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
"my first dialog",
WS_POPUP|WS_BORDER|WS_CAPTION|WS_MAXIMIZEBOX|WS_MINIMIZEBOX,0,0,
100,
100,
NULL,NULL,hInstance,NULL);
if(!hWnd)
{
MessageBox(NULL,"OH NO","My first dialog",MB_OK);
return 0;
}
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
MSG msg;
for(;;)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//Sleep(10000);
return (int)(msg.wParam);
}
for(;;)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//Sleep(10000);
return (int)(msg.wParam);
}
///处理窗口消息
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:MessageBox(hWnd,"ESC","KEYBOARD",MB_OK);
PostMessage(hWnd,WM_CLOSE,0,0);
}
return 0;
case WM_CLOSE:DestroyWindow(hWnd);return 0;
case WM_DESTROY:PostQuitMessage(0);return 0;
}
// MessageBox(NULL,"OH NO","My first dialog",MB_OK);
return DefWindowProc(hWnd,message,wParam,lParam);
return 0;
case WM_CLOSE:DestroyWindow(hWnd);return 0;
case WM_DESTROY:PostQuitMessage(0);return 0;
}
// MessageBox(NULL,"OH NO","My first dialog",MB_OK);
return DefWindowProc(hWnd,message,wParam,lParam);
}