一、图标资源
1、注意图标的大小,一个图标文件中,可以有多个不同大小的图标。
2 加载
HICON LoadIcon( HINSTANCE hInstance, // handle to application instance LPCTSTR lpIconName // name string or resource identifier ); 成功返回HICON句柄
3、从注册窗口设置
wc.hIcon = LoadIcon(g_hInstance,(LPCWSTR)IDI_ICON1);//默认左上角的图标 wc.hIconSm = LoadIcon(g_hInstance, (LPCWSTR)IDI_ICON2);
4、 通过消息机制设置,在创建窗口的时候
HICON hicon = LoadIcon(g_hInstance, (LPCWSTR)IDI_ICON1); SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hicon);
5、绘制图标
case WM_PAINT: 绘制图标(hWnd); break; void 绘制图标(HWND hWnd) { //创建笔 PAINTSTRUCT p = {}; HDC hdc = BeginPaint(hWnd, &p); HICON hico = LoadIcon(g_hInstance,(LPCWSTR)IDI_ICON3); DrawIcon(hdc, 200, 200, hico); EndPaint(hWnd, &p); }
二、光标资源
1、添加资源
光标的大小默认是32X32像素,每个光标有热点,是当前鼠标的热点
2、在窗口注册时使用资源
wc.hCursor = LoadCursor(g_hInstance,(LPCWSTR)IDC_CURSOR1);
3、设置光标消息
case WM_SETCURSOR: { if (LOWORD(lParam)==HTCLIENT)//当鼠标在客户区 { HCURSOR hCur = LoadCursor(g_hInstance, (LPCWSTR)IDC_CURSOR1); SetCursor(hCur); return 0;//不让系统默认处理所以直接跳过 }else if (LOWORD(lParam)==HTCAPTION) { HCURSOR hCur = LoadCursor(g_hInstance, (LPCWSTR)IDC_CURSOR2); SetCursor(hCur); return 0;//不让系统默认处理所以直接跳过 } } break;
三、不同客户区,显示不同内容
case WM_SETCURSOR: { if (LOWORD(lParam)==HTCLIENT)//当鼠标在客户区 { HCURSOR hCur = LoadCursor(g_hInstance, (LPCWSTR)IDC_CURSOR1); HCURSOR hCur2 = LoadCursor(g_hInstance, (LPCWSTR)IDC_CURSOR2); RECT rcCli = {0}; //获取客户区大小 GetClientRect(hWnd, &rcCli); POINT pt = { 0 }; //获取屏幕坐标 GetCursorPos(&pt); //屏幕区转为客户区 ScreenToClient(hWnd, &pt); if (pt.x<rcCli.right/2) { SetCursor(hCur); } else { SetCursor(hCur2); return 0; } //不让系统默认处理所以直接跳过 } } break;