SDK 小结
MSDN 控件消息
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644927(v=vs.85).aspx
MSG 消息结构
typedef struct tagMSG {
HWND hwnd; //接受消息的窗口句柄
UINT message;//消息号
WPARAM wParam; //32位特定值,根据消息号确定
LPARAM lParam; //32位特定值,根据消息号确定
DWORD time; //消息创建时的时间
POINT pt; //消息创建时的鼠标/光标 在屏幕坐标系中的位置
} MSG, *PMSG, NEAR *NPMSG, FAR *LPMSG;
WM_COMMAND(0x111):标准控件通知消息 Edit,Combo,List,Button等
消息源 wParam(高位字) wParam(低位字) lParam
菜单 0 菜单 ID(IDM_) 0
快捷键 1 快捷键 ID(IDM_) 0
控件 控件定义通知代码 控件 ID 控件窗口句柄
WM_NOTIFY(0X4E):通用控件通知消息List View,Image List,IP Address,Tree View,Toolbar等
消息类型 WPARAM LPARAM
VM_NOTIFY 发生VM_NOTIFY消息的控件ID NMHDR指针
查找窗口句柄:枚举查找
HWND hWnd1 = FindWindow(L"窗口类", 0); //根据类名查找
HWND hWnd2 = FindWindow(0, L"标题"); //根据标题查找
HWND hWnd3 = FindWindow(L"窗口类", L"哈哈"); //根据类名标题名查找
HWND hWnd4= GetDlgItem(hWnd , 控件id); //父窗口句柄+控件id查找
int id=GetDlgCtrlID(hWnd);//根据句柄获取控件id
枚举查找: EnumChildWindows
EnumChildWindows(hcalc, EnumChildProc1,0x96);//最后一个自定义参数
BOOL CALLBACK EnumChildProc1(
In HWND hwnd,
In LPARAM lParam
)
{
int teId = GetDlgCtrlID(hwnd);//获取控件ID
if (teId == lParam) //根据要查找的控件id 获取句柄
{
hgetCalc = hwnd;
return false;
}
return true;
}
设置图标
HICON hNewIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON1));
HICON hOldIcon = (HICON)SetClassLong(hwndDlg, // 设置哪个对话框
GCL_HICON, // 你要改变这个对话框的什么属性
(LONG)hNewIcon);// 设置的新属性值
静态设置图片 Picture Control
1.添加 Picture Control控件
2.修改type 属性为 bitmip
3.设置 center image
4.image 选择图片资源
动态设置图片(更换static 控件中的图片)
//创建static 类型控件
HWND hPicture = CreateWindow(
TEXT("static"),
TEXT(""),
WS_VISIBLE | WS_CHILD | SS_BITMAP | SS_CENTERIMAGE,
7, 10, 420, 260,
hwndDlg,
NULL,
GetModuleHandleW(0), 0);
//设置图片
HBITMAP hBmp = LoadBitmap(GetModuleHandle(0), MAKEINTRESOURCE(IDB_BITMAP2));
SendMessage(hPicture, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBmp);
设置菜单
HMENU hMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU1));
SetMenu(hWnd, hMenu);
设置进度条
HWND hProgress = GetDlgItem(hWnd, 1000);
for (int i = 0; i < 100; ++i)
{
SendMessage(hProgress, PBM_SETPOS, i, 0);// 设置进度条的进度位置
Sleep(5); // 设置延时
}
SDK 或MFC 中使用高清图片的方法
//双缓冲技术:就是把内容先绘制在内存DC上,最后统一拷贝到界面DC中
//sdk 中使用高清图片的办法 利用了双缓冲,可以省略
include <atlimage.h>
CImage nImage; //cImage 对象
nImage.Load(L"1.jpg"); //载入.jpg 格式图片
HBITMAP hBtm=nImage.Detach(); //获取句柄
HDC hDc = GetDC(hwndDlg); //获取设备DC
HDC hMdc=CreateCompatibleDC(hDc);//创建
SelectObject(hMdc, hBtm); //将图片选入内存DC中
StretchBlt(hDc,0,0, 626, 586, hMdc,0,0, 626, 586, SRCCOPY); //拉伸拷贝
Edit操作
//对 Edit 操作 方法1
wchar_t STem[100] = {};
wchar_t STem1[100] = {};
HWND HIDC_EDIT2= GetDlgItem(hwndDlg, IDC_EDIT2);
SetWindowText(HIDC_EDIT2,L"username"); //设置内容
GetWindowTextW(HIDC_EDIT2, STem,100); //获取内容
//MessageBox(0, STem, 0, 0);
HWND HIDC_EDIT1 = GetDlgItem(hwndDlg, IDC_EDIT1);
SetWindowText(HIDC_EDIT1, L"password");
//对 Edit 操作 方法2:
SetDlgItemText(hwndDlg, IDC_EDIT1,L"哈哈哈");
GetDlgItemText(hwndDlg, IDC_EDIT1, STem,100);
//MessageBox(0, STem, 0, 0);
//对Edit 操作方法 3: 发消息
SendMessage(HIDC_EDIT2, WM_GETTEXT, 100, (LPARAM)STem1);
//MessageBox(0, STem1, 0, 0);
SendMessage(HIDC_EDIT2, WM_SETTEXT, 0, (LPARAM)L"你好");
Combox操作
//对Combox 添加数据 方法1:
wchar_t str[] = L"北京";
wchar_t str1[] = L"上海";
wchar_t ComStr[100];
wchar_t ComStr1[100];
HWND HIDC_COMBO1= GetDlgItem(hwndDlg, IDC_COMBO1);
LRESULT ret= SendMessage(HIDC_COMBO1, CB_ADDSTRING, NULL, (LPARAM)str);
//对 combox 添加数据 方法2: 是方法1 的封装
ComboBox_AddString(HIDC_COMBO1, str1);
ComboBox_SetCurSel(HIDC_COMBO1, 0);//设置默认选项
//对ComboBox 获取数据
GetWindowTextW(HIDC_COMBO1, ComStr, 100); //获取内容()
//GetWindowTextW 也是通过WM_GETTEXT消息 实现的
SendMessage(HIDC_COMBO1, WM_GETTEXT, 100, (LPARAM)ComStr1);//获取内容
//MessageBox(0, ComStr1, 0, 0);
check 控件操作
HWND HIDC_CHECK1= GetDlgItem(hwndDlg, IDC_CHECK1);
HWND HIDC_CHECK2 = GetDlgItem(hwndDlg, IDC_CHECK2);
SendMessage(HIDC_CHECK1, BM_SETCHECK, BST_CHECKED, 0); //设置选中状态
LRESULT num=SendMessage(HIDC_CHECK2, BM_GETCHECK, 0, 0);//获取选中状态 选中为1 未选中为0 不确定 2
radio控件
//radio 注 分组 第一个radio group属性为true 到第二个 radio group属性为true 中间的为一组
HWND HIDC_RADIO6 = GetDlgItem(hwndDlg, IDC_RADIO6);
HWND HIDC_RADIO7 = GetDlgItem(hwndDlg, IDC_RADIO7);
SendMessage(HIDC_RADIO6, BM_SETCHECK, BST_CHECKED, 0);
num=SendMessage(HIDC_RADIO7, BM_GETCHECK, 0, 0);
List 控件
include<CommCtrl.h>
//List 控件 需 设置 list 的View属性为Report(报表)
HWND HIDC_LIST1 =GetDlgItem(hwndDlg, IDC_LIST1);
RECT rt = {};
GetClientRect(HIDC_LIST1, &rt);
LV_COLUMN lc;
lc.mask = LVCF_WIDTH | LVCF_TEXT;
lc.cx = (rt.right - rt.left) / 4;
lc.pszText = L"姓名";
ListView_InsertColumn(HIDC_LIST1, 0, &lc);//通过宏发消息...LVM_INSERT
lc.pszText = L"年龄";
ListView_InsertColumn(HIDC_LIST1, 1, &lc);
lc.pszText = L"籍贯";
ListView_InsertColumn(HIDC_LIST1, 2, &lc);
lc.pszText = L"其他";
ListView_InsertColumn(HIDC_LIST1, 3, &lc);
//插入行 //自动出现滚动条
LV_ITEM li = {};
for (int i = 0; i < 100; i++)
{
li.iItem = i;
ListView_InsertItem(HIDC_LIST1, &li);
ListView_SetItemText(HIDC_LIST1, i, 0, L"李四");
ListView_SetItemText(HIDC_LIST1, i, 1, L"12");
ListView_SetItemText(HIDC_LIST1, i, 2, L"北京");
ListView_SetItemText(HIDC_LIST1, i, 3, L"1");
}
//拓展风格需要额外设置
ListView_SetExtendedListViewStyle(HIDC_LIST1, LVS_EX_FULLROWSELECT);
//List 获取列行值
case WM_NOTIFY: //获取列行值
{
//LPNMITEMACTIVATE
LPNMLISTVIEW temp = (LPNMLISTVIEW)lParam; //WM_NOTIFY 的消息结构 针对listView 和True 控件
if (temp->hdr.code == NM_CLICK&&temp->hdr.idFrom == IDC_LIST1)
{
TCHAR str[100] = {};
ListView_GetItemText(temp->hdr.hwndFrom, temp->iItem, temp->iSubItem, str, 100);
MessageBox(0, str, 0, 0);
}
}
树控件基本操作
HWND HIDC_TREE1 = GetDlgItem(hwndDlg, IDC_TREE1);
TV_INSERTSTRUCT tvins;
tvins.hParent = TVI_ROOT; //指定父节点
tvins.hInsertAfter = TVI_SORT; //指定兄弟节点
tvins.item.mask = TVIF_TEXT; //tvins.item为要插入的内容 TV_ITEMW
//插入根节点
tvins.item.pszText = L"事件1";
HTREEITEM hti = TreeView_InsertItem(HIDC_TREE1, &tvins);
tvins.hParent = hti;
tvins.item.pszText = L"事件1.1";
TreeView_InsertItem(HIDC_TREE1, &tvins);
tvins.item.pszText = L"事件1.2";
TreeView_InsertItem(HIDC_TREE1, &tvins);
tvins.hParent = TVI_ROOT;
tvins.item.pszText = L"事件2";
hti = TreeView_InsertItem(HIDC_TREE1, &tvins);
//插入子节点
tvins.hParent = hti;
tvins.item.pszText = L"事件2.2";
hti = TreeView_InsertItem(HIDC_TREE1, &tvins);
case WM_NOTIFY:
{ //获取树控件的值
LPNMLISTVIEW temp = (LPNMLISTVIEW)lParam; //WM_NOTIFY 的消息结构 针对listView 和True 控件
if (temp->hdr.code == TVN_SELCHANGED&&temp->hdr.idFrom == IDC_TREE1)
{
LPNMTREEVIEW TreeTemp = (LPNMTREEVIEW)lParam;
TVITEM tvi = {};
TCHAR str[100] = {};
tvi.hItem = TreeTemp->itemNew.hItem;//行号 //树没有列 只需行号就行啦
tvi.pszText = str;
tvi.cchTextMax = 1024;
tvi.mask = TVIF_TEXT;
TreeView_GetItem(TreeTemp->hdr.hwndFrom,&tvi);// sendmessage TVM_GETITEM
MessageBox(0, str, 0, 0);
}
}break;
模态对话框和非模态对话框
include <windows.h>
include "resource.h"
INT_PTR CALLBACK DialogProc1(
In HWND hwndDlg,
In UINT uMsg,
In WPARAM wParam,
In LPARAM lParam
)
{
switch(uMsg)
{
case WM_CLOSE:
{
EndDialog(hwndDlg, 0);
return 0;
}
default:break;
}
return 0;
}
INT_PTR CALLBACK DialogProc(
In HWND hwndDlg,
In UINT uMsg,
In WPARAM wParam,
In LPARAM lParam
)
{
switch (uMsg)
{
case WM_NOTIFY:
case WM_INITDIALOG:
{
}break;
case WM_COMMAND:
{
WORD HiwParam = HIWORD(wParam);//高两字节:控件通码
WORD LowParam = LOWORD(wParam);//低两字节:控件id
if (HiwParam== BN_CLICKED&&LowParam== IDC_BUTTON1)
{
//MessageBox(0, L"我被点击了", 0, 0);
//DialogBox 的第一个参数是可以选参数,可填null 但是图片加载可能有问题
//创建模态对话框
DialogBox(GetModuleHandle(0), MAKEINTRESOURCE(IDD_DIALOG2), NULL, DialogProc1);
return 1;
}
} break;
case WM_CLOSE:
{
EndDialog(hwndDlg,0);
return 0;
}
default:
break;
}
return 0;
}
INT WINAPI WinMain(In HINSTANCE hInstance, In_opt HINSTANCE hPrevInstance, In LPSTR lpCmdLine, In int nShowCmd)
{
//非模态对话框 需要自建 循环 结构
HWND h1=CreateDialog((HINSTANCE)0x400000, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DialogProc);
ShowWindow(h1,5); //显示窗口
MSG msg = {};
// 自建消息循环需要收到WM_QUIT消息才会退出循环
while (GetMessage(&msg, NULL, NULL, NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg); //分发消息
}
return 0;
}
网友评论