windwos编程资源操作2

1

还是和前面的一样 先添加资源

2

3

这是顶级菜单 以及 子菜单

4

然后保存

还是先用前面的部分的代码

然后 开始添加菜单

第一种方式

直接在创建窗口的时候创建菜单

5

6

直接运行

7

菜单直接出来了

第二种方式:

通过传到createwindwoW 函数中 因为这个函数接受一个菜单句柄参数

8

9

因为涉及到句柄 所以就和前面的资源操作一样 需要先导入菜单

10

LoadMenuW函数接受两个参数 第一个是 这个程序的句柄 第二个是菜单资源的名称

11

然后运行

12

第三种:

通过点击 添加菜单

13

14

右键展示子菜单

重新添加一个右键点击消息

15

16

ClientToScreen 函数 将 获取的鼠标坐标转化为 相对于屏幕的鼠标坐标 因为没有转换之前 是相对于工作区 也就是 直接创建的窗口的坐标位置

但是弹出菜单的函数接受坐标 出现的位置是相对于电脑屏幕而言的坐标 所以需要转换

17

18

GetCursorPos 函数直接获取屏幕中的鼠标坐标

给子菜单添加消息

先找到子菜单的ID

19

20

点击子菜单

21

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include<Windows.h>
#include<iostream>
#include<CommCtrl.h>
#include"resource.h"
LRESULT CALLBACK WindowProc( //消息处理函数
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
) {
static HINSTANCE hInstance = GetModuleHandleW(NULL);
switch (uMsg)
{
case WM_CREATE:
//MessageBoxW(hwnd, L"窗口创建了", L"提示", MB_OK);
//添加控件
CreateWindowW(WC_BUTTON, L"移动窗口", WS_CHILD | WS_VISIBLE, 10, 10, 120, 40, hwnd, (HMENU)0x100, hInstance, 0);
CreateWindowW(WC_BUTTON, L"获取文本框内容", WS_CHILD | WS_VISIBLE, 10, 60, 120, 40, hwnd, (HMENU)0x101, hInstance, 0);
CreateWindowW(WC_BUTTON, L"设置文本框内容", WS_CHILD | WS_VISIBLE, 10, 110, 120, 40, hwnd, (HMENU)0x102, hInstance, 0);
CreateWindowW(WC_BUTTON, L"设置父窗口", WS_CHILD | WS_VISIBLE, 10, 160, 120, 40, hwnd, (HMENU)0x103, hInstance, 0);
CreateWindowW(WC_EDIT, L"文本框内容", WS_CHILD | WS_BORDER | WS_VISIBLE, 10, 320, 320, 80, hwnd, (HMENU)0x104, hInstance, 0);
CreateWindowW(WC_BUTTON, L"设置ICON", WS_CHILD | WS_VISIBLE, 10, 220, 120, 40, hwnd, (HMENU)0x105, hInstance, 0);
CreateWindowW(WC_BUTTON, L"设置菜单", WS_CHILD | WS_VISIBLE, 10, 420, 120, 40, hwnd, (HMENU)0x106, hInstance, 0);
break;
case WM_CLOSE:
MessageBoxW(hwnd, L"窗口关闭了", L"提示", MB_OK);
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
case WM_RBUTTONDOWN:
{
/*WORD x = LOWORD(lParam);
WORD y = HIWORD(lParam);*/
POINT point{ 0 };
/* point.x = x;
point.y = y;*/
GetCursorPos(&point);
//ClientToScreen(hwnd, &point);
HMENU hmenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_MENU1));
HMENU hSubMenu = GetSubMenu(hmenu, 0);
TrackPopupMenu(hSubMenu,TPM_LEFTALIGN, point.x, point.y,0,hwnd,NULL);
break;
}
case WM_COMMAND:
{
WORD ControlId = LOWORD(wParam);
switch (ControlId)
{
case 0x100:
{
RECT rect{ 0 };
GetClientRect(hwnd, &rect);
int x = rand() % (rect.right - 120);
int y = rand() % (rect.bottom - 40);
MoveWindow((HWND)lParam, x, y, 120, 40, TRUE);
break;
}
case 0x101:
{
WCHAR buff[100]{ 0 };
HWND hedit = GetDlgItem(hwnd, 0x104);
GetWindowTextW(hedit, buff, 100);
MessageBoxW(hwnd, buff, L"提示", MB_OK);
break;
}
case 0x102:
{
/*HWND hedit = GetDlgItem(hwnd, 0x104);
SetWindowTextW(hedit,0x104,L"SetWindowTextW");*/
SetDlgItemTextW(hwnd, 0x104, L"SetDlgItemTextW");
break;
}
case 0x103:
{
HWND hnote = FindWindowW(NULL, L"无标题 - 记事本");
SetParent((HWND)lParam, hnote);
break;
}
case 0x106:
{
HMENU hmenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_MENU1));
SetMenu(hwnd, hmenu);
break;
}
case ID_Menu:
{
MessageBoxW(hwnd, L"子菜单1", L"子菜单1被点击了", MB_OK);
break;
}
}
break;

}
default:
break;
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPreInstance,
LPSTR lpCmdeLine,
int nCmdShow
)
{
//1.创建一个窗口类
WNDCLASSW myClass = { 0 };
myClass.lpszClassName = L"51hook";
myClass.lpfnWndProc = WindowProc;
myClass.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));
//myClass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));//设置图标
//myClass.hCursor = LoadCursorW(hInstance, MAKEINTRESOURCEW(IDC_CURSOR1));
//myClass.lpszMenuName = MAKEINTRESOURCEW(IDR_MENU1);

//2.注册窗口类
RegisterClassW(&myClass);
//3.创建窗口

//HMENU hmenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_MENU1));
HWND hwindow = CreateWindowW(
myClass.lpszClassName,
L"51hook",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
0
);
//4.显示窗口
ShowWindow(hwindow, SW_SHOWNORMAL);

//5.获取消息
MSG msg = { 0 };
while (GetMessageW(&msg, 0, 0, 0)) {
DispatchMessageW(&msg);//分发消息给消息处理函数
}

return 0;
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!