1 在使用任何资源的时候都要加上头文件:#include "resource.h"
2 资源的分类(部分):
菜单资源
图标资源
光标资源
字符串资源
加速键资源
位图、对话框资源等等
3 菜单资源总结:
加载菜单资源有两种方式:
1)在注册窗口类的时候进行挂载,用到宏MAKEINTRESOURCE
2)在创建窗口函数中可以挂载,用到的函数为LoadMenu
4 图标资源总结:
1)加载图标用到的函数为LoadIcon
2)在窗口中挂载图标的两种方式:
a、在注册窗口类中挂载,需要用到MAKEINTRESOURCE
b、可以发送WM_SETICON消息,该消息的附加参数说明:WPARAM - 图标的类型(ICON_BIG/ICON_SMALL)大图标还是小图标;LPARAM - 图标的句柄(LoadIcon)
5 光标资源总结:
1)每一个光标都要设置热点(HotSpot)
2)加载光标函数LoadCursor
3)窗口中挂载光标的两种方式:
a、在注册窗口类时,挂载
b、可以发送WM_SETCURSOR消息,在该消息处理函数中调用SetCursor函数来设置光标
6 字符串资源总结:
1)加载字符串函数LoadString
7 加速键使用总结:
1)加载加速键,LoadAccelerators
2)处理加速键消息,TranslateAccelerator
int TranslateAccelerator(
HWND hWnd, //处理消息的窗口句柄
HACCEL hAccTable, //加速键句柄
LPMSG lpMsg //消息
);如果是加速键返回非0
3)加速键处理过程:
1 检测消息是否是WM_KEYDOWN/WM_SYSKEYDOWN获取按键状态
2 根基按键状态,从HACCEL中查找对应命令ID
3 把对应ID封装在WM_COMMAND消息中并发送,处理ID所对应的命令
注:TranslateAccelerator函数既封装消息,又会发送消息,所以不需要DispatchMessage。
附加以上学习代码:
/*
说明:实现了资源的练习,包括菜单资源、光标资源、图标资源、字符串资源和加速键资源的练习。
菜单资源的加载有两种方式,分别是在注册窗口类和创建窗口的时候,加载菜单
功能:点击“关于”可以更改图标,光标在客户区内左右两半边切换
*/
#include
#include "resource.h"
HINSTANCE g_hInstance;
HCURSOR g_hCursor[2];
void OnCommand(HWND hWnd,WPARAM wParam)
{
//SHORT uMenuID = LOWORD(wParam);
switch(LOWORD(wParam))
{
case ID_NEW:
MessageBox(hWnd,__TEXT("点击了新建"),__TEXT("提示"),MB_OK);
break;
case ID_QUIT:
PostQuitMessage(0);
break;
case ID_ABOUT:
HICON hIc = LoadIcon(g_hInstance,MAKEINTRESOURCE(IDI_ICON1));
SendMessage(hWnd,WM_SETICON,ICON_BIG,(LPARAM)hIc);
MessageBox(hWnd,__TEXT("点击了关于"),__TEXT("提示"),MB_OK);
break;
}
}
void OnCreate(HWND hWnd,WPARAM wParam)
{
g_hCursor[0] = LoadCursor(g_hInstance,MAKEINTRESOURCE(IDC_CURSOR1));
g_hCursor[1] = LoadCursor(g_hInstance,MAKEINTRESOURCE(IDC_CURSOR2));
}
BOOL OnSetCursor(HWND hWnd,LPARAM lParam)
{
if(LOWORD(lParam) == HTCLIENT )
{
//获取窗口大小
RECT rt = {0};
GetClientRect(hWnd,&rt);
//获取鼠标的位置
POINT pt = {0};
GetCursorPos(&pt);
//屏幕坐标向窗口坐标转换
ScreenToClient(hWnd,&pt);
if (pt.x < rt.right / 2)
{
SetCursor(g_hCursor[0]);
}
else if(pt.x >= rt.right / 2)
{
SetCursor(g_hCursor[1]);
}
return TRUE;
}
return FALSE;
}
LRESULT CALLBACK MyProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_SETCURSOR:
if(OnSetCursor(hWnd,lParam))
return 0;
//return 0;
break;
case WM_CREATE:
OnCreate(hWnd,wParam);
break;
case WM_COMMAND:
OnCommand(hWnd,wParam);
break;
case WM_SYSCOMMAND:
if (wParam == SC_CLOSE)
{
PostQuitMessage(0);
}
break;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
ATOM Register(LPCWSTR className)
{
WNDCLASSEX wc = {0};
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(g_hInstance,MAKEINTRESOURCE(IDC_CURSOR1));//NULL;
wc.hIcon = LoadIcon(g_hInstance,MAKEINTRESOURCE(IDI_ICON1));//NULL;
wc.hIconSm = LoadIcon(g_hInstance,MAKEINTRESOURCE(IDI_ICON2));//NULL;
wc.hInstance = g_hInstance;
wc.lpfnWndProc = MyProc;
wc.lpszClassName = className;
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);//NULL;
wc.style = CS_VREDRAW | CS_HREDRAW;
return RegisterClassEx(&wc);
}
HWND Create(LPCWSTR className,LPCWSTR windowName)
{
//在创建窗口中,只能用来修改菜单
return CreateWindowEx(0,className,windowName,WS_OVERLAPPEDWINDOW,100,100,600,600,NULL,NULL,g_hInstance,NULL);
}
void Show(HWND hWnd)
{
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
}
void Message(HWND hWnd)
{
MSG ms = {0};
HACCEL hAC = LoadAccelerators(g_hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1));
while (GetMessage(&ms,NULL,0,0))
{
if(!TranslateAccelerator(hWnd,hAC,&ms))
{
TranslateMessage(&ms);
DispatchMessage(&ms);
}
}
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
g_hInstance = hInstance;
Register(__TEXT("Main"));
//字符串资源的使用
TCHAR szText[256] = {0};
LoadString(g_hInstance,IDS_STRWINDOW,szText,256);
//HWND hWnd = Create(__TEXT("Main"),__TEXT("This is test"));
HWND hWnd = Create(__TEXT("Main"),szText);
Show(hWnd);
Message(hWnd);
return 0;
}