窗口和消息
窗口和消息
2011年06月27日
#include
预处理头文件
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
此语句声明回调函数。
LRESULT是一个windows数据类型,保存了一个从程序或回调函数返回的长整型数据
CALLBACK意思是回调。
参数
1、接收消息的窗口的句柄。
2、消息类型。
3、消息附加值(短整型)。
4、消息附加值(长整型)。
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
WinMain程序入口。
返回值为int类型。
WINAPI为一个宏定义, _stdcall。
HINSTANCE是一个windows句柄类型,含意是一个执行实例程序句柄。
PSTR是一个字符串指针。
函数参数
1、hInstance代表当前实例(自已)。
2、hPrevInstance是上一个实例句柄。
3、szCmdLine一个字符串变量。
4、iCmdShow设定程序开始运行时的状态。
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
Static定义一个静态全局常量。
TCHAR宽字符等同于char.
HWND hwnd ;
定义一个窗口句柄->hwnd。
HWND是windows下的窗口句柄类型数据。
MSG msg ;
定义一个消息结构->msg.
MSG是windows下的一个消息结构类型。
其原型如下:
Typedef struct tagMSG
{
HWND hwnd;检索窗口句柄
UNIT message;消息值。
WPARAM wParam;参数
LPARAM lParam;长整型参数
DWORD time;消息送至队列的时间
POINT pt;消息发送时光标位置(结构体)
}MSG;
其中POINT的结构原型如下:
Typedef struct tagPOINT
{
LONG x,y;
} POINT;
WNDCLASS wndclass ;
定义一个窗口类实例->windclass。
WNDCLASS是一个windows下的窗口类别结构。
结构原型如下:
Typedef struct tagWNDCLASS
{
UINT style;标识窗口类样式(像素大小)。
WINDPROC lpfnWndProc;指定消息处理函数。
Int cbClsExtra;
Int cbWndExtra;这两句用于在窗口类别结构和Windows内部保存的窗口结构中预留一些额外空间。
HINSTANCE hInsttance;程序的执行实例句柄(它也是WinMain的参数之一)
HICON hIcon;为依据这个窗口类别建立的窗口设置一个图标、
HCURSOR hCursor;预定义鼠标形状。
HBRUSH hbrBackground;预定义窗口类背景颜色。
LPCTSTR lpszMenuName;预定义窗口类的菜单。
LPCTSTR lpszClassName; 定义窗口类别名称。
}WNDCLASS;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuNam = NULL ;
wndclass.lpszClassName= szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow( szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT,// initial x position
CW_USEDEFAULT,// initial y position
CW_USEDEFAULT,// initial x size
CW_USEDEFAULT,// initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE:
PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
猜你喜欢
转载自coub63coub.iteye.com/blog/1359797
今日推荐
周排行