您的位置:首页 > 其它

windows 程序设计自学:添加字符串资源

2014-07-03 00:13 232 查看
#include <windows.h>
#include "resource.h"

LRESULT CALLBACK MyWndProc(  HWND hwnd,      // handle to window
UINT uMsg,      // message identifier
WPARAM wParam,  // first message parameter
LPARAM lParam   // second message parameter
);

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
WNDCLASS wnd;
HWND hwnd;
MSG msg;
TCHAR szAppName[40]; //定义应用程序名称
wnd.style = CS_HREDRAW | CS_VREDRAW; //水平或垂直改变窗口都被重绘 与MyWndProc的WM_PAINT消息关联
wnd.lpfnWndProc = MyWndProc;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wnd.lpszMenuName = NULL;
wnd.lpszClassName = "HelloClass"; //窗口类标识,用在CreateWindow的第一个参数
wnd.hInstance = hInstance;
if(!RegisterClass(&wnd))
{
MessageBox(NULL, TEXT("无法创建窗口"), TEXT("ERROR"), MB_OK|MB_ICONERROR);
return 0;
}

LoadString(hInstance, IDS_APPNAME, szAppName, sizeof(szAppName)); //LoadString函数为将资源复制到程序区缓存中
hwnd = CreateWindow("HelloClass", szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); //第二个参数为窗口标题
ShowWindow(hwnd, nShowCmd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

LRESULT CALLBACK MyWndProc(  HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc; //定义设备环境句柄
PAINTSTRUCT ps; //绘制结构
RECT rect; //矩形结构
switch(uMsg)
{
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
DrawText(hdc, TEXT("Hello,World!"), -1, &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


int LoadString( HINSTANCE hInstance, // handle to resource module UINT uID, // resource identifier LPTSTR lpBuffer, // resource buffer int nBufferMax // size of buffer);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: