10.1.1 向程序添加图标
(1)加载图标:(注意:第1个参数为hInstance,不能为NULL表示从程序本身加载)
图标ID为数字 |
①wndclass.hIcon = LoadIcon(hInstance,MAXINTRESOURCE(IDI_ICON); ②wndclass.hIcon = LoadIcon(hInstance,MAXINTRESOURCE(125) |
图标ID为字符串 |
①wndclass.hIcon = LoadIcon(hInstance,TEXT(“ICONDEMO”)); //须在资源视图中将图标ID输入为字符串“ICONDEMO”,要加引号 ②wndclass.hIcon = LoadIcon(hInstance,TEXT(“#125”)); //windows将起始的#字符识别为ASCII格式的数字前缀 |
①第1个参数:为hInstance而不是NULL,表示从程序自身的资源加载
②第2个参数:可以是MAKEINTERSOURCE(数字标识符)或字符串标识符,但当为字符串标识符时,必须把图标ID改变字符串,如”ICONDEMO”。注意,须加引号。
(2)获取图标的大小
大图标 |
小图标 |
cxIcon = GetSystemMetrics(SM_CXICON) cyIcon = GetSystemMetrics(SM_CYICON) |
cxIcon = GetSystemMetrics(SM_CXSMSIZE) cyIcon = GetSystemMetrics(SM_CYSMSIZE) |
(3)绘制图标:DrawIcon(hdc,x,y,hIcon);
10.1.2 在应用程序中使用图标
(1)窗口类WNDCLASSEX和WNDCLASS
WNDCLASSEX |
WNDCLASS |
hIcon ——大图标句柄(EXE程序图标) |
hIcon:大小图标从单文件中自动提取或缩放 |
hIconSm——小图标句柄(标题和任务栏) |
无 |
用RegisterClassEx注册 |
用RegisterClass注册 |
(2)动态改变图标
①SetClassLong(hwnd,GCL_HCION,LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON)));
②DrawIcon(hdc,x,y,GetClassLong(hwnd,GCL_HICON)); //直接从窗口类取出图标句柄
10.1.3 自定义鼠标指针
(1)加载鼠标指针
wndclass.hCursor= LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR);//ID为数字
wndclass.hCursor= LoadCursor(hInstance,szCursor);//鼠标ID为字符串
(2)定义鼠标热点——使用最右边的工具在鼠标图标上点一下(没明显变化,但己起作用)
(3)子窗口中改变鼠标:
SetClassLong(hwndChild,GCL_HCURSOR,LoadCursor(hInstance,Text(“childCursor”)));
(4)在WM_MOUSEMOVE消息中调用SetCursor(hCursor)重绘图标;
【ICONDEMO】程序
/*------------------------------------------------------------ ICONDEMO.C -- Icon Demonstration Program (c) Charles Petzold, 1998 ------------------------------------------------------------*/ #include <windows.h> #include "resource.h" LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("IconDemo"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)); wndclass.hCursor = LoadCursor(hInstance, szAppName); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, // window class name TEXT("Icon Demo"), // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL); // creation parameters ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static HICON hIcon; static int cxIcon, cyIcon, cxClient, cyClient; HINSTANCE hInstance; switch (message) { case WM_CREATE: hInstance = ((LPCREATESTRUCT)lParam)->hInstance; hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)); cxIcon = GetSystemMetrics(SM_CXICON); cyIcon = GetSystemMetrics(SM_CYICON); return 0; case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); return 0; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); for (int x = 0; x < cxClient; x += cxIcon) for (int y = 0; y < cyClient; y += cyIcon) { DrawIcon(hdc, x, y, hIcon); } EndPaint(hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }
//resource.h
//{{NO_DEPENDENCIES}} // Microsoft Visual C++ 生成的包含文件。 // 供 IconDemo.rc 使用 // #define IDI_ICON 101 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 102 #define _APS_NEXT_COMMAND_VALUE 40001 #define _APS_NEXT_CONTROL_VALUE 1001 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif
//IconDemo.rc
// Microsoft Visual C++ generated resource script. // #include "resource.h" #define APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 2 resource. // #include "winres.h" ///////////////////////////////////////////////////////////////////////////// #undef APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // 中文(简体,中国) resources #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS) LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED #ifdef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // TEXTINCLUDE // 1 TEXTINCLUDE BEGIN "resource.h " END 2 TEXTINCLUDE BEGIN "#include ""winres.h"" " "