消息
N(notify):
通知,通告,控件发送给使用者
M(message):
发送给控件,用于修改和控制控件的消息
查看控件消息
依次在MSDN中打开Platform SDK Documentation/User Interface Services/Windows User Interface/Controls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
case WM_COMMAND:
{
WORD wID = LOWORD(wParam);//低位表示控件ID
WORD wMsg = HIWORD(wParam);//高位表示消息
if (wMsg == BN_CLICKED)
{
switch (wID)
{
case BUT_TEST1:
MessageBox(hwndDlg, "按钮1按下", "Luo", MB_OK);
break;
case BUT_TEST2:
{
MessageBox(hwndDlg, "按钮2按下", "Luo", MB_OK);
HWND hWnBut1 = GetDlgItem(hwndDlg, BUT_TEST1);
SendMessage(hWnBut1, BM_CLICK, 0, 0);
break;
}
}
}
return true;
}
|
Edit Control
支持换行
Multiline设置为true
Want Return设置为true
获取文本
- EM_GETLINE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
case WM_COMMAND:
{
WORD wID = LOWORD(wParam);//低位表示控件ID
WORD wMsg = HIWORD(wParam);//高位表示消息
if (wMsg == BN_CLICKED)
{
switch (wID)
{
case BUT_GETTEXT:
{
CHAR szBuf[MAXBYTE] = "0";
HWND hWndEdit = GetDlgItem(hwndDlg, EDIT_TEST);
SendMessage(hWndEdit, EM_GETLINE, 0, (LPARAM)szBuf);
MessageBox(hwndDlg, szBuf, "Luo", MB_OK);
break;
}
}
}
return true;
}
|
- WM_GETTEXT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
case WM_COMMAND:
{
WORD wID = LOWORD(wParam);//低位表示控件ID
WORD wMsg = HIWORD(wParam);//高位表示消息
if (wMsg == BN_CLICKED)
{
switch (wID)
{
case BUT_GETTEXT:
{
CHAR szBuf[MAXBYTE] = "0";
HWND hWndEdit = GetDlgItem(hwndDlg, EDIT_TEST);
SendMessage(hWndEdit, WM_GETTEXT, MAXBYTE, (LPARAM)szBuf);
MessageBox(hwndDlg, szBuf, "Luo", MB_OK);
break;
}
}
}
return true;
}
|
- SendDlgItemMessage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
case WM_COMMAND:
{
WORD wID = LOWORD(wParam);//低位表示控件ID
WORD wMsg = HIWORD(wParam);//高位表示消息
if (wMsg == BN_CLICKED)
{
switch (wID)
{
case BUT_GETTEXT:
{
CHAR szBuf[MAXBYTE] = "0";
SendDlgItemMessage(hwndDlg, //对话框句柄
EDIT_TEST, //控件ID
WM_GETTEXT, //消息
MAXBYTE,
(LPARAM)szBuf);
MessageBox(hwndDlg, szBuf, "Luo", MB_OK);
break;
}
}
}
return true;
}
|
- GetWindowText
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
case WM_COMMAND:
{
WORD wID = LOWORD(wParam);//低位表示控件ID
WORD wMsg = HIWORD(wParam);//高位表示消息
if (wMsg == BN_CLICKED)
{
switch (wID)
{
case BUT_GETTEXT:
{
CHAR szBuf[MAXBYTE] = "0";
HWND hWndEdit = GetDlgItem(hwndDlg, EDIT_TEST);
GetWindowText(hWndEdit, szBuf, sizeof(szBuf));
MessageBox(hwndDlg, szBuf, "Luo", MB_OK);
break;
}
}
}
return true;
}
|
- GetDlgItemText
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
case WM_COMMAND:
{
WORD wID = LOWORD(wParam);//低位表示控件ID
WORD wMsg = HIWORD(wParam);//高位表示消息
if (wMsg == BN_CLICKED)
{
switch (wID)
{
case BUT_GETTEXT:
{
CHAR szBuf[MAXBYTE] = "0";
GetDlgItemText(hwndDlg, EDIT_TEST, szBuf, sizeof(szBuf));
MessageBox(hwndDlg, szBuf, "Luo", MB_OK);
break;
}
}
}
return true;
}
|
设置文本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
case WM_COMMAND:
{
WORD wID = LOWORD(wParam);//低位表示控件ID
WORD wMsg = HIWORD(wParam);//高位表示消息
if (wMsg == BN_CLICKED)
{
switch (wID)
{
case BUT_SETTEXT:
{
char szBuf[] = "XiaLuoHun";
SetDlgItemText(hwndDlg, EDIT_TEST, szBuf);
break;
}
}
}
return true;
}
|
Check Box
在MSDN中查看BUTTON消息
BM_GETCHECK:
判断复选框是否被选中
若被选中,则返回BST_CHECKED
在MSDN中查看BUTTON消息
BM_GETCHECK:
判断单选框是否被选中
若被选中,则返回BST_CHECKED
Combo Box
CB_ADDSTRING:
向组合框中加内容
CB_SETCURSEL:
设置组合框默认选项
1
2
3
4
5
6
7
8
9
|
case WM_INITDIALOG:
{
//向组合框中加内容
SendDlgItemMessage(hwndDlg, CMB_TEST, CB_ADDSTRING, 0, (LPARAM)"LuoHun");
SendDlgItemMessage(hwndDlg, CMB_TEST, CB_ADDSTRING, 0, (LPARAM)"ShiTou");
//设置组合框默认选项
SendDlgItemMessage(hwndDlg, CMB_TEST, CB_SETCURSEL, 0, 0);
return true;
}
|
CB_GETCURSEL:
获取当前组合框选中项的索引
CB_GETLBTEXT:
根据索引值,获取组合框中的文本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
case WM_COMMAND:
{
WORD wID = LOWORD(wParam);//低位表示控件ID
WORD wMsg = HIWORD(wParam);//高位表示消息
if (wMsg == BN_CLICKED)
{
switch (wID)
{
case BUT_GETTEXT:
{
//获取组合框中当前选中项的索引值
HWND hWndComBox = GetDlgItem(hwndDlg, CMB_TEST);
int nCurIdx = SendMessage(hWndComBox, CB_GETCURSEL, 0, 0);
//获取组合框选中项的文本
char szBuf[MAXBYTE] = "0";
SendMessage(hWndComBox, CB_GETLBTEXT, nCurIdx, (LPARAM)szBuf);
MessageBox(hwndDlg, szBuf, "Luo", MB_OK);
break;
}
}
}
return true;
}
|
通用控件
需包头文件
1
2
|
#include <commctrl.h>
#pragma comment(lib,"comctl32.lib")
|
响应消息
List Control
设置整行选中
1
2
3
4
|
//获取句柄
HWND hListCtrl = GetDlgItem(hwndDlg, EXPORT_LISTCTRL);
//设置整行选中
SendMessage(hListCtrl, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
|
插入表头
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//插入表头
LV_COLUMN lv;
memset(&lv, 0, sizeof(lv));
//第一列
lv.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
lv.pszText = TEXT("序号");
lv.cx = 50;
lv.iSubItem = 0;
SendMessage(hListCtrl, LVM_INSERTCOLUMN, 0, (DWORD)&lv);
//第二列
lv.pszText = TEXT("RVA");
lv.cx = 80;
lv.iSubItem = 1;
ListView_InsertColumn(hListCtrl, 1, &lv);
|
添加数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
LV_ITEM vitem;
memset(&vitem, 0, sizeof(LV_ITEM));
//插入序号
CHAR szBuf[MAXBYTE] = "0";
vitem.mask = LVIF_TEXT;
DWORD dwOrdinalAddBase = dwOrdinal + pExportDirectory->Base;
sprintf_s(szBuf, "%04X", dwOrdinalAddBase);
vitem.pszText = szBuf;
vitem.iItem = i;//行
vitem.iSubItem = 0;//列
ListView_InsertItem(hListCtrl, &vitem);
//插入RVA
sprintf_s(szBuf, "%08X", dwFuncRva);
vitem.pszText = szBuf;
vitem.iItem = i;//行
vitem.iSubItem = 1;//列
ListView_SetItem(hListCtrl, &vitem);
|
获取选中行信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//获取列表控件句柄
HWND hLstCtrlSection = GetDlgItem(hwndDlg, RELOCATION_LSTCTRL_SECTION);
LV_ITEM vitem;
//初始化
memset(&vitem, 0, sizeof(LV_ITEM));
//获取当前选中行的序号
DWORD dwIdx = SendMessage(hLstCtrlSection, LVM_GETSELECTIONMARK, 0, 0);
//获取当前选中行的信息第1列信息
CHAR szBuf[MAXBYTE] = "0";
vitem.mask = TVIF_TEXT | TVIF_PARAM;//设置哪些成员是有效的
vitem.pszText = szBuf;
vitem.cchTextMax = sizeof(szBuf);
vitem.iItem = dwIdx;//选中行
vitem.iSubItem = 1;//第1列
BOOL bRet = ListView_GetItem(hLstCtrlSection, &vitem);
if (bRet == FALSE)
{
return FALSE;
}
|
Tree Control
设置风格
1
2
3
4
5
6
7
8
9
10
|
//获取树控件的句柄
HWND hwndTree = GetDlgItem(hwndDlg, RESOURCE_TREE);
//设置树控件的风格
DWORD dwStyle = GetWindowLong(hwndTree, GWL_STYLE);
dwStyle = dwStyle |
TVS_HASLINES |
TVS_LINESATROOT |
TVS_HASBUTTONS |
TVS_SHOWSELALWAYS;
SetWindowLong(hwndTree, GWL_STYLE, dwStyle);
|
添加数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
CHAR szBuf[MAXBYTE] = "LuoHun";//缓冲区
HTREEITEM hFirstItem;//保存结点句柄
TVITEM tvItem = { 0 };//结点
tvItem.mask = TVIF_TEXT | TVIF_STATE | TVIF_PARAM;//设置哪些成员是有效的
tvItem.cchTextMax = sizeof(szBuf);
tvItem.pszText = szBuf;
tvItem.lParam = (LPARAM)pFirstResDirEntry;//附加数据
TVINSERTSTRUCT tvStruct = { 0 };
tvStruct = { 0 };
tvStruct.hParent = TVI_ROOT;
tvStruct.item = tvItem;
hFirstItem = TreeView_InsertItem(hwndTree, &tvStruct);
|
获取选中结点信息
响应消息:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
case WM_NOTIFY:
{
NMHDR* pNmhdr = (NMHDR*)lParam;
NMTREEVIEW* pnmtv = (LPNMTREEVIEW)lParam;
if (wParam == RESOURCE_TREE &&
pNmhdr->code == TVN_SELCHANGED &&
pnmtv->action == TVC_BYMOUSE)
{
//给对话框相关控件赋值
FillResourceTableDlg(hwndDlg);
}
return TRUE;
}
|
FillResourceTableDlg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//获取树控件句柄
HWND hTree = GetDlgItem(hwndDlg, RESOURCE_TREE);
//获取选中结点句柄
HTREEITEM hTreeItem = TreeView_GetSelection(hTree);
//给tvItem赋值
CHAR szBuf[MAXBYTE] = "0";//缓冲区
TVITEM tvItem = { 0 };//结点
tvItem.mask = TVIF_TEXT | TVIF_PARAM ;//设置哪些成员是有效的
tvItem.hItem = hTreeItem;
tvItem.pszText = szBuf;
tvItem.cchTextMax = sizeof(szBuf);
//tvItem是一个传入传出参数,获取选中项数据
BOOL bRet = TreeView_GetItem(hTree, &tvItem);
if (bRet == FALSE)
{
return FALSE;
}
|