首页 > 代码库 > SDK源码分析 10
SDK源码分析 10
1 #include <windows.h> 2 3 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 4 5 //定义结构体 一个是 int 一个是字符串 6 struct 7 { 8 int iStyle; 9 TCHAR *szText; 10 } 11 button[] = 12 { 13 BS_PUSHBUTTON, TEXT("PUSHBUTTON"), 14 BS_DEFPUSHBUTTON, TEXT("DEFPUSHBUTTON"), 15 BS_CHECKBOX, TEXT("CHECKBOX"), 16 BS_AUTOCHECKBOX,TEXT("AUTOCHECKBOX"), 17 BS_RADIOBUTTON,TEXT("RADIOBUTTON"), 18 BS_3STATE,TEXT("3STATE"), 19 BS_AUTO3STATE,TEXT("AUTO3STATE"), 20 BS_GROUPBOX,TEXT("GROUPBOX"), 21 BS_AUTORADIOBUTTON,TEXT("AUTORADTOBUTTON"), 22 BS_OWNERDRAW,TEXT("OWNERDRAW") 23 }; 24 #define NUM (sizeof button / sizeof button[0])//一共有多少个 25 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nCmdShow) 26 { 27 WNDCLASS wndclass; 28 HWND hWnd; 29 MSG msg; 30 TCHAR szClassName[] = TEXT("MyClass"), szWindowsName[] = TEXT("MyWindows"); 31 32 wndclass.cbClsExtra = 0; 33 wndclass.cbWndExtra = 0; 34 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 35 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); 36 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 37 wndclass.hInstance = hInstance; 38 wndclass.lpfnWndProc = WndProc; 39 wndclass.lpszClassName = szClassName; 40 wndclass.lpszMenuName = NULL; 41 wndclass.style = CS_VREDRAW | CS_HREDRAW; 42 43 if (!RegisterClass(&wndclass)) 44 { 45 MessageBox(hWnd, TEXT("The Application on Windows NT!"), TEXT("ERROR~"), MB_OK); 46 } 47 48 hWnd = CreateWindow(szClassName, 49 szWindowsName, 50 WS_OVERLAPPEDWINDOW, 51 CW_USEDEFAULT, 52 CW_USEDEFAULT, 53 CW_USEDEFAULT, 54 CW_USEDEFAULT, 55 NULL, NULL, hInstance, NULL); 56 ShowWindow(hWnd, nCmdShow); 57 UpdateWindow(hWnd); 58 59 while (GetMessage(&msg, NULL, NULL, NULL)) 60 { 61 TranslateMessage(&msg); 62 DispatchMessage(&msg); 63 } 64 return msg.wParam; 65 } 66 67 LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 68 { 69 static HWND hWndButton[NUM]; 70 static RECT rect; 71 static TCHAR szTop[] = TEXT("message wParam lParam"), 72 szUnd[] = TEXT("_______ ______ ______"), 73 szFormat[] = TEXT("%-16s%04X-%04X %04X-%04X"), szBuffer[50]; 74 static int cxChar, cyChar; 75 int i; 76 PAINTSTRUCT ps; 77 HDC hdc; 78 79 switch (msg) 80 { 81 case WM_CREATE: 82 cxChar = LOWORD(GetDialogBaseUnits());//获取字符宽度 83 cyChar = HIWORD(GetDialogBaseUnits());//获取字符高度 这两个是对话框用的API 84 85 for (i = 0; i < NUM; i++) 86 { 87 hWndButton[i] = CreateWindow(TEXT("button"), button[i].szText, WS_CHILD | WS_VISIBLE | button[i].iStyle, 88 cxChar, cyChar*(1 + 2 * i), 20 * cxChar, 7 * cyChar / 4, hWnd, (HMENU)i, 89 ((LPCREATESTRUCT)lParam)->hInstance, NULL); 90 //生成子窗口 句柄保存在hWndButton 91 //lParam其实也可以转换成STRUCT的 92 } 93 return 0; 94 case WM_SIZE: 95 rect.left = 24 * cxChar; 96 rect.top = 2 * cyChar; 97 rect.right = LOWORD(lParam); 98 rect.bottom = HIWORD(lParam); 99 //设置 显示文字区域 100 return 0; 101 case WM_PAINT: 102 hdc = BeginPaint(hWnd, &ps); 103 104 SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));//设置等宽字体 105 SetBkMode(hdc, TRANSPARENT);//设置透明模式 106 TextOut(hdc, 24 * cxChar, cyChar, szTop, lstrlen(szTop)); 107 TextOut(hdc, 24 * cxChar, cyChar, szUnd, lstrlen(szUnd)); 108 109 EndPaint(hWnd, &ps); 110 return 0; 111 case WM_DRAWITEM: 112 case WM_COMMAND: 113 ScrollWindow(hWnd, 0, -cyChar, &rect, &rect);//接收到 命令 就向上 滚动 114 hdc = GetDC(hWnd); 115 116 SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT)); 117 TextOut(hdc, 24 * cxChar, cyChar*(rect.bottom / cyChar - 1), szBuffer, 118 wsprintf(szBuffer, szFormat, 119 msg == WM_DRAWITEM ? TEXT("WM_DRAWITEM") : TEXT("WM_COMMAND"), 120 HIWORD(wParam), LOWORD(wParam), 121 HIWORD(lParam), LOWORD(lParam))); 122 123 //输出信息。 124 ReleaseDC(hWnd, hdc); 125 ValidateRect(hWnd, &rect);//使窗口有效 删除 WM_PAINT消息从消息队列里 126 break; 127 case WM_DESTROY: 128 PostQuitMessage(0); 129 return 0; 130 } 131 132 return DefWindowProc(hWnd, msg, wParam, lParam); 133 }
SDK源码分析 10
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。