windows 程序设计自学:添加字符串资源
1 #include <windows.h> 2 #include "resource.h" 3 4 LRESULT CALLBACK MyWndProc( HWND hwnd, // handle to window 5 UINT uMsg, // message identifier 6 WPARAM wParam, // first message parameter 7 LPARAM lParam // second message parameter 8 ); 9 10 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) 11 { 12 WNDCLASS wnd; 13 HWND hwnd; 14 MSG msg; 15 TCHAR szAppName[40]; //定义应用程序名称 16 wnd.style = CS_HREDRAW | CS_VREDRAW; //水平或垂直改变窗口都被重绘 与MyWndProc的WM_PAINT消息关联 17 wnd.lpfnWndProc = MyWndProc; 18 wnd.cbClsExtra = 0; 19 wnd.cbWndExtra = 0; 20 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); 21 wnd.hCursor = LoadCursor(NULL, IDC_ARROW); 22 wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 23 wnd.lpszMenuName = NULL; 24 wnd.lpszClassName = "HelloClass"; //窗口类标识,用在CreateWindow的第一个参数 25 wnd.hInstance = hInstance; 26 if(!RegisterClass(&wnd)) 27 { 28 MessageBox(NULL, TEXT("无法创建窗口"), TEXT("ERROR"), MB_OK|MB_ICONERROR); 29 return 0; 30 } 31 32 LoadString(hInstance, IDS_APPNAME, szAppName, sizeof(szAppName)); //LoadString函数为将资源复制到程序区缓存中 33 hwnd = CreateWindow("HelloClass", szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); //第二个参数为窗口标题 34 ShowWindow(hwnd, nShowCmd); 35 while (GetMessage(&msg, NULL, 0, 0)) 36 { 37 TranslateMessage(&msg); 38 DispatchMessage(&msg); 39 } 40 return 0; 41 } 42 43 LRESULT CALLBACK MyWndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 44 { 45 HDC hdc; //定义设备环境句柄 46 PAINTSTRUCT ps; //绘制结构 47 RECT rect; //矩形结构 48 switch(uMsg) 49 { 50 case WM_PAINT: 51 { 52 hdc = BeginPaint(hwnd, &ps); 53 GetClientRect(hwnd, &rect); 54 DrawText(hdc, TEXT("Hello,World!"), -1, &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER); 55 EndPaint(hwnd, &ps); 56 } 57 break; 58 case WM_DESTROY: 59 PostQuitMessage(0); 60 return 0; 61 } 62 63 return DefWindowProc(hwnd, uMsg, wParam, lParam); 64 }
int LoadString( HINSTANCE hInstance, // handle to resource module UINT uID, // resource identifier LPTSTR lpBuffer, // resource buffer int nBufferMax // size of buffer);