关于windows窗体的创建过程,记录一下,加深印象!
#include<Windows.h> #include<stdio.h> LRESULT CALLBACK WindowSunProc( HWND hwnd, //handle to window UINT uMsg, //message identifier WPARAM wParam, //first message parameter LPARAM lParam //second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, //handle to current instance HINSTANCE hPrevInstance,//handle to previous instance LPSTR lpCmdLine, //command line int nCmdShow //show state ) { //初始化,初始化包括窗口类的定义、注册、创建窗口实例和显示窗口四部分 HWND hwnd; MSG Msg; WNDCLASS wndclass; char lpszClassName[] = "窗口"; //窗口类名 char lpszTitle[] = "测试窗口"; //窗口标题名 //窗口类定义,窗口类定义了窗口的形式与功能,窗口类定义通过给窗口类数据结构WNDCLASS赋值完成 //该数据结构中包含窗口类的各种属性 wndclass.style = 0; // 窗口类型为缺省类型CS_ Class Style wndclass.lpfnWndProc = WindowSunProc; //定义窗口处理函数 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 = (LPCWCHAR)lpszClassName; //窗口类名为“窗口” //以下是窗口类的注册-----------Windows系统本身提供部分预定义的窗口类,程序员也可以自定义窗口类,窗口类必须先注册后使用。 if (!RegisterClass(&wndclass)) { MessageBeep(0); return -1; } char *windowClassName = "窗口类名"; char *windowsApplicationName = "窗口标题"; hwnd = CreateWindow((LPCWSTR)windowClassName, (LPCWSTR)windowsApplicationName, WS_OVERLAPPEDWINDOW, 100, 50, 400, 250, NULL, NULL, hInstance, NULL ); ShowWindow(hwnd, SW_SHOWNORMAL); UpdateWindow(hwnd); while (GetMessage(&Msg,NULL,0,0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } LRESULT CALLBACK WindowSunProc( HWND hwnd, //handle to window UINT uMsg, //message identifier WPARAM wParam, //first message parameter LPARAM lParam //second message parameter ) { switch (uMsg) { case WM_CHAR: //在窗口中按下一个字符键,将弹出相应的信息 char szChar[20]; sprintf_s(szChar, "char code is %d", wParam);//将字符消息放入szChar中 MessageBox(hwnd, (LPCWSTR)szChar, (LPCWSTR)"char", 0); break; case WM_LBUTTONDOWN: //在窗口中按下鼠标左键时,弹出提示信息并输出文字 MessageBox(hwnd, (LPCWSTR)"mouse clicked", (LPCWSTR)"message", 0); HDC hdc; hdc = GetDC(hwnd);//不能在响应WM_PAINT消息时调用 TextOut(hdc, 0, 50, (LPCWSTR)"快乐生活", strlen("快乐生活")); ReleaseDC(hwnd, hdc); break; case WM_PAINT: //重新绘制窗口 HDC hDC; PAINTSTRUCT ps; hDC = BeginPaint(hwnd, &ps);//BeginPaint只能在响应WM_PAINT消息时调用 TextOut(hDC, 0, 0, (LPCWSTR)"只要努力就会看到阳光", strlen("只要努力就会看到阳光")); EndPaint(hwnd, &ps); break; case WM_CLOSE: //关闭窗口时弹出消息框 if (IDYES == MessageBox(hwnd, (LPCWSTR)"是否真的结束?", (LPCWSTR)"message", MB_YESNO)) { DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } return 0; }