windows 程序设计自学:添加图标资源

 #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 )
{
TCHAR szAppName[] = TEXT("IconDemo");
WNDCLASS wnd;
HWND hwnd;
MSG msg;
wnd.style = CS_HREDRAW | CS_VREDRAW; //水平或垂直改变窗口都被重绘 与MyWndProc的WM_PAINT消息关联
wnd.lpfnWndProc = MyWndProc;
wnd.cbClsExtra = ;
wnd.cbWndExtra = ;
wnd.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wnd.lpszMenuName = NULL;
wnd.lpszClassName = szAppName; //窗口类标识,用在CreateWindow的第一个参数
wnd.hInstance = hInstance;
if(!RegisterClass(&wnd))
{
MessageBox(NULL, TEXT("无法创建窗口"), TEXT("ERROR"), MB_OK|MB_ICONERROR);
return ;
} hwnd = CreateWindow(szAppName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); //第二个参数为窗口标题
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return ;
} LRESULT CALLBACK MyWndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc; //定义设备环境句柄
PAINTSTRUCT ps; //绘制结构 static HICON hIcon;
static int cxIcon, cyIcon, cxClient, cyClient;
HINSTANCE hInstance;
int x, y; switch(uMsg)
{ case WM_CREATE:
{
hInstance = ((LPCREATESTRUCT) lParam)->hInstance;//WM_CREATE消息的lParam参数是一个CREATESTRUCT结构的指针,包含hInstance.
hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
cxIcon = GetSystemMetrics(SM_CXICON); //获取图标大小,上一条LoadIcon关联
cyIcon = GetSystemMetrics(SM_CYICON);
return ;
}
break; case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
for (y = ; y < cyClient; y += cyIcon)
{
for (x = ; x < cxClient; x += cxIcon)
{
DrawIcon(hdc, x, y, hIcon); //绘制
} }
EndPaint(hwnd, &ps); }
break; case WM_SIZE:
{
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
}
break; case WM_DESTROY:
PostQuitMessage();
return ;
} return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

注:参阅windows 程序设计第五版334页

上一篇:移植milo的全局光照渲染器代码


下一篇:236. Lowest Common Ancestor of a Binary Tree 二叉树的最低公共祖先