Win32学习笔记(十) 字符串资源|加速资源

字符串资源
添加字符串资源
添加字符串表,在表中增加字符串
字符串资源的使用
int LoadString(
HINSTANCE hInstance, //handle to resource module
UINT uID, //字符串ID
LPTSTR ipBuffer, //存放字符串BUFF
int nBufferMax //字符串BUFF长度
);成功返回字符串长度,失败

#include <windows.h>
#include "resource.h"
HINSTANCE g_hInstance = 0;
LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM IParam)
{
	switch (msgID) {
	case WM_SETCURSOR:
	{
		HCURSOR hCur = LoadCursor(g_hInstance, (char*)IDC_CURSOR2);
		if (LOWORD(IParam) == HTCLIENT) {
			SetCursor(hCur);
			return 0;
		}
		else
		{

		}
	}
	break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, msgID, wParam, IParam);
}

int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns, LPSTR lpCmdLine, int nCmdShow) {
	g_hInstance = hIns;
	WNDCLASS wc = { 0 };
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc.hCursor = LoadCursor(hIns,(char*)IDC_CURSOR1);
	wc.hIcon = LoadIcon(hIns,(char*)IDI_ICON1); 
	wc.hInstance = hIns;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "Main";
	wc.lpszMenuName = NULL;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&wc); 

	
	char szTitele[256] = { 0 }; //!!
	LoadString(hIns, IDS_WND, szTitele, 256);//!!

	HWND hWnd = CreateWindowEx(0, "Main", szTitele, WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL
		, hIns
		, NULL);

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);

	MSG nMsg = { 0 };
	while (GetMessage(&nMsg, NULL, 0, 0)) { 
		TranslateMessage(&nMsg);
		DispatchMessage(&nMsg);
	
	return 0;
}



加速建资源
添加 资源添加加速键表,增加命令ID对应的加速键
使用
加载加速键表
HACCEL LoadAccelerators(
HINSTANCE hInstance, //handle to module
LPCTSTR ipTableName //accelerator table name
);返回加速键表句柄
翻译加速键
int TranslateAccelerator(
HWND hWnd,//处理消息的窗口句柄
HACCEL hAccTable, //加速键表句柄
LPMSG ipMsg //消息
);如果是加速键,返回非零

探究原理
TranslateAccelerator(hWnd,hAccel,&nMsg){
if(nMsg.message != WM_KEYDOWN)
return 0;
根据nMsg.wParam(键码值),获知哪些按键被按下(CTRL + M)
拿着(CTRL + M)到hAccel(加速键表)中去匹配查找
if(没找到)
return 0;
if(找到){
SendMessage(hWnd,WM_COMMAND,ID_NEW|||1,…);//ID_NEW是窗口菜单的ID
return 1;
}
}

在WM_COMMMAND中相应消息,消息参数
wPARAM : HIWORD 为1表示加速键,为0表示菜单。 //高两字节
LOWORD为命令ID。//低两字节
用于区分
IParam : 为0

#include <windows.h>
#include "resource.h"
HINSTANCE g_hInstance = 0;
void OnCommand(HWND hwnd, WPARAM wParam) {
	switch (LOWORD(wParam))
	{
	case ID_NEW:
		if (HIWORD(wParam) == 0)
			MessageBox(hwnd, "新建菜单项被点击", "Infor", MB_OK);
		else if (HIWORD(wParam) == 1)
			MessageBox(hwnd, "CTRL+M被点击", "Infor", MB_OK);
		break;
	}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM IParam)
{
	switch (msgID) {
	case WM_COMMAND://!!
		OnCommand(hWnd, wParam);
		break;
	case WM_SETCURSOR:
	{
		HCURSOR hCur = LoadCursor(g_hInstance, (char*)IDC_CURSOR2);
		if (LOWORD(IParam) == HTCLIENT) {
			SetCursor(hCur);
			return 0;
		}
		else
		{

		}
	}
	break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, msgID, wParam, IParam);
}

int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns, LPSTR lpCmdLine, int nCmdShow) {
	g_hInstance = hIns;
	WNDCLASS wc = { 0 };
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc.hCursor = LoadCursor(hIns,(char*)IDC_CURSOR1);
	wc.hIcon = LoadIcon(hIns,(char*)IDI_ICON1); 
	wc.hInstance = hIns;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "Main";
	wc.lpszMenuName = (CHAR*)IDR_MENU1;//!!!
	wc.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&wc); 

	
	char szTitele[256] = { 0 }; 
	LoadString(hIns, IDS_WND, szTitele, 256);

	HWND hWnd = CreateWindowEx(0, "Main", szTitele, WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL
		, hIns
		, NULL);

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);

	HACCEL hAccel = LoadAccelerators(hIns,(CHAR*)IDR_ACCELERATOR1);
	MSG nMsg = { 0 };
	while (GetMessage(&nMsg, NULL, 0, 0)) {
		if (!TranslateAccelerator(hWnd, hAccel, &nMsg)) {//!!
			TranslateMessage(&nMsg);
			DispatchMessage(&nMsg);
		}
		
	}
	return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值