1 // 02 文本输出.cpp : 定义应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "02 文本输出.h" 6 7 #define MAX_LOADSTRING 100 8 9 // 全局变量: 10 HINSTANCE hInst; // 当前实例 11 TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 12 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 13 14 // 此代码模块中包含的函数的前向声明: 15 ATOM MyRegisterClass(HINSTANCE hInstance); 16 BOOL InitInstance(HINSTANCE, int); 17 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 18 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 19 20 int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, 21 _In_opt_ HINSTANCE hPrevInstance, 22 _In_ LPTSTR lpCmdLine, 23 _In_ int nCmdShow) 24 { 25 UNREFERENCED_PARAMETER(hPrevInstance); 26 UNREFERENCED_PARAMETER(lpCmdLine); 27 28 // TODO: 在此放置代码。 29 MSG msg; 30 HACCEL hAccelTable; 31 32 // 初始化全局字符串 33 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 34 LoadString(hInstance, IDC_MY02, szWindowClass, MAX_LOADSTRING); 35 MyRegisterClass(hInstance); 36 37 // 执行应用程序初始化: 38 if (!InitInstance (hInstance, nCmdShow)) 39 { 40 return FALSE; 41 } 42 43 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY02)); 44 45 // 主消息循环: 46 while (GetMessage(&msg, NULL, 0, 0)) 47 { 48 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 49 { 50 TranslateMessage(&msg); 51 DispatchMessage(&msg); 52 } 53 } 54 55 return (int) msg.wParam; 56 } 57 58 59 60 // 61 // 函数: MyRegisterClass() 62 // 63 // 目的: 注册窗口类。 64 // 65 ATOM MyRegisterClass(HINSTANCE hInstance) 66 { 67 WNDCLASSEX wcex; 68 69 wcex.cbSize = sizeof(WNDCLASSEX); 70 71 wcex.style = CS_HREDRAW | CS_VREDRAW; 72 wcex.lpfnWndProc = WndProc; 73 wcex.cbClsExtra = 0; 74 wcex.cbWndExtra = 0; 75 wcex.hInstance = hInstance; 76 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY02)); 77 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 78 wcex.hbrBackground = CreateSolidBrush(RGB(0,0,255)); 79 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_MY02); 80 wcex.lpszClassName = szWindowClass; 81 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 82 83 return RegisterClassEx(&wcex); 84 } 85 86 // 87 // 函数: InitInstance(HINSTANCE, int) 88 // 89 // 目的: 保存实例句柄并创建主窗口 90 // 91 // 注释: 92 // 93 // 在此函数中,我们在全局变量中保存实例句柄并 94 // 创建和显示主程序窗口。 95 // 96 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 97 { 98 HWND hWnd; 99 100 hInst = hInstance; // 将实例句柄存储在全局变量中 101 102 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 103 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 104 105 if (!hWnd) 106 { 107 return FALSE; 108 } 109 110 ShowWindow(hWnd, nCmdShow); 111 UpdateWindow(hWnd); 112 113 return TRUE; 114 } 115 116 // 117 // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) 118 // 119 // 目的: 处理主窗口的消息。 120 // 121 // WM_COMMAND - 处理应用程序菜单 122 // WM_PAINT - 绘制主窗口 123 // WM_DESTROY - 发送退出消息并返回 124 // 125 // 126 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 127 { 128 int wmId, wmEvent; 129 PAINTSTRUCT ps; 130 HDC hdc; 131 132 switch (message) 133 { 134 case WM_COMMAND: 135 wmId = LOWORD(wParam); 136 wmEvent = HIWORD(wParam); 137 // 分析菜单选择: 138 switch (wmId) 139 { 140 case IDM_ABOUT: 141 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); 142 break; 143 case IDM_EXIT: 144 DestroyWindow(hWnd); 145 break; 146 default: 147 return DefWindowProc(hWnd, message, wParam, lParam); 148 } 149 break; 150 case WM_PAINT: 151 hdc = BeginPaint(hWnd, &ps); 152 // TODO: 在此添加任意绘图代码... 153 SetTextAlign(hdc, TA_CENTER); 154 SetTextColor(hdc, RGB(0,255,255)); 155 SetBkColor(hdc, RGB(255, 255, 0)); 156 TextOut(hdc, 100, 100, TEXT("15PB"), 4); 157 SetBkMode(hdc, TRANSPARENT); 158 TextOut(hdc, 100, 140, TEXT("15PB132"), 7); 159 SetBkMode(hdc, OPAQUE); 160 161 TextOut(hdc, 100, 180, TEXT("15PB456789"), 10); 162 EndPaint(hWnd, &ps); 163 break; 164 case WM_DESTROY: 165 PostQuitMessage(0); 166 break; 167 default: 168 return DefWindowProc(hWnd, message, wParam, lParam); 169 } 170 return 0; 171 } 172 173 // “关于”框的消息处理程序。 174 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 175 { 176 UNREFERENCED_PARAMETER(lParam); 177 switch (message) 178 { 179 case WM_INITDIALOG: 180 return (INT_PTR)TRUE; 181 182 case WM_COMMAND: 183 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 184 { 185 EndDialog(hDlg, LOWORD(wParam)); 186 return (INT_PTR)TRUE; 187 } 188 break; 189 } 190 return (INT_PTR)FALSE; 191 }