windows学习(1)_简单的窗口
#include <windows.h>
//回调函数
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
//主函数
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow){
static TCHAR szAppName[] = "HelloWin" ;
HWND hwnd ;
MSG msg ;
WNDCLASS 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.lpszMenuName = NULL ;//不用菜单
wndclass.lpszClassName = szAppName ;//AppName
//如果注册窗口不成功,弹出提示框
if (!RegisterClass (&wndclass)){
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
//创建窗口
hwnd = CreateWindow (szAppName, // 窗口的class名字
TEXT ("The Hello Program"), // 窗口的名字
WS_OVERLAPPEDWINDOW, // 窗口的样式
CW_USEDEFAULT, // 默认的左上角的x坐标
CW_USEDEFAULT, // 默认的左上角的y坐标
CW_USEDEFAULT, // 默认的长
CW_USEDEFAULT, // 默认的宽
NULL, // 父窗口句柄为空
NULL, // 窗口的菜单句柄为空
hInstance, // 实例句柄
NULL) ; // 参数为空
//显示窗口
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:
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) ;
}
下面是运行效果

posted on 2018-02-23 12:33 孙悟空son_ku_kong 阅读(215) 评论(0) 收藏 举报
浙公网安备 33010602011771号