1)什么是富文本编辑框
2)样式
3)注意事项
- include<Richedit.h>//富文本编辑控件的头文件
- 必须加载动态库LoadLibrary(L"Msftedit.dll");
4)CHARFORMAT结构体
5)WM_NOTIFY消息
6)代码实例
#include<windows.h> #include<stdio.h> #include<Richedit.h>//富文本编辑控件的头文件 //自定义的窗口过程 LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 return 0; case WM_COMMAND: { WORD CtrlID = LOWORD(wParam); HWND hCtrl = (HWND)lParam; if (CtrlID == 111) { WORD code= HIWORD(wParam); if (code == EN_CHANGE) { TCHAR buf[64]; GetWindowText(hCtrl, buf, 64); MessageBox(NULL, buf,L"提示",MB_OK); } } }break; case WM_NOTIFY: { //先看公共头部 NMHDR *pHDR = (NMHDR *)lParam; if (pHDR->code == EN_SELCHANGE) { SELCHANGE * pSC= (SELCHANGE *)lParam; TCHAR buf[64]; wsprintf(buf, L"选择文本索引%d - %d ", pSC->chrg.cpMin, pSC->chrg.cpMax); OutputDebugString(buf); } }break; case WM_CREATE: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //必须加载动态库 LoadLibrary(L"Msftedit.dll"); HWND hRichEdit=CreateWindow(MSFTEDIT_CLASS, L"这是一个富文本编辑控件!!!", WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE|ES_AUTOVSCROLL, 30, 30, 200, 200, hWnd, (HMENU)111, pcs->hInstance, NULL); //选中索引2-7的文本 SendMessage(hRichEdit, EM_SETSEL, 2, 7); //显示选择背景色 SendMessage(hRichEdit, EM_HIDESELECTION,0 , 0); //先获取默认的字符格式 CHARFORMAT cf; ZeroMemory(&cf, sizeof(CHARFORMAT)); cf.cbSize = sizeof(CHARFORMAT); SendMessage(hRichEdit, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &cf); //让选择的文字加粗 cf.dwMask |= CFM_BOLD; cf.dwEffects |= CFE_BOLD; //设置下划线 cf.dwMask |= CFM_UNDERLINE; cf.dwEffects |= CFE_UNDERLINE; //设置字体大小 cf.dwMask |= CFM_SIZE; cf.yHeight =20*20; //设置字体 cf.dwMask |= CFM_FACE; wcscpy(cf.szFaceName , L"楷体"); //设置字体颜色 cf.dwMask |= CFM_COLOR; cf.dwEffects &= ~CFE_AUTOCOLOR; cf.crTextColor = RGB(255, 0, 0); //设置选择字符的格式 SendMessage(hRichEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf); //取消选择 SendMessage(hRichEdit, EM_SETSEL, -1, -1); //设置富文本编辑框的背景色 SendMessage(hRichEdit, EM_SETBKGNDCOLOR, 0, (LPARAM) RGB(0,255,0)); //发送控件内容改变事件、选择改变事件掩码 SendMessage(hRichEdit, EM_SETEVENTMASK, 0, (LPARAM)ENM_CHANGE| ENM_SELCHANGE); }break; } return DefWindowProc(hWnd, Msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //1.注册窗口类 WNDCLASS wnd; wnd.cbClsExtra = 0; wnd.cbWndExtra = 0; wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色 wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标 //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息 wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数 wnd.lpszClassName = L"MrHuang";//窗口类名 wnd.lpszMenuName = NULL;//菜单资源名称 wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式 wnd.hInstance = hInstance;//实例句柄 RegisterClass(&wnd); //创建窗口(返回之前发送WM_CREATE) HWND hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, hInstance, NULL); //显示窗口 ShowWindow(hWnd, nShowCmd); //更新窗口(发送WM_PAINT) UpdateWindow(hWnd); //消息循环(收到WM_QUIT消息退出) MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg);//翻译消息 DispatchMessage(&msg);//分发消息到窗口过程 } }
1)什么是公共控件
2)初始化公共控件库
3)公共控件结构体
4)公共控件有哪些
1)什么是动画控件
2)样式
3)创建动画控件
4)相动画控件发送消息
例如:
5)代码实例
#include<windows.h> #include<CommCtrl.h> //公共控件的头文件 //自定义的窗口过程 LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 return 0; case WM_CREATE: { //创建动画控件 HWND hAnimate= CreateWindow(ANIMATE_CLASS, L"XXX", WS_CHILD | WS_VISIBLE | ACS_CENTER, 10, 10, 350, 350, hWnd, (HMENU)111, ((LPCREATESTRUCT)lParam)->hInstance, NULL); //打开一个AVI剪辑动画 SendMessage(hAnimate, ACM_OPEN, NULL, (LPARAM)L"C:\Users\35497\Desktop\code\030\Debug\clock.avi"); //播放剪辑动画(wparam播放次数,-1循环播放, lparam 低字节开始,高字节结束 ) SendMessage(hAnimate, ACM_PLAY, -1 , MAKELPARAM(0,-1)); }break; } return DefWindowProc(hWnd, Msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //1.注册窗口类 WNDCLASS wnd; wnd.cbClsExtra = 0; wnd.cbWndExtra = 0; wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色 wnd.hCursor= LoadCursor(NULL, IDC_ARROW);//光标 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标 //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息 wnd.lpfnWndProc =MyWindowProc;//自定义的窗口过程函数 wnd.lpszClassName = L"MrHuang";//窗口类名 wnd.lpszMenuName = NULL;//菜单资源名称 wnd.style = CS_HREDRAW| CS_DBLCLKS;//窗口类、样式 wnd.hInstance = hInstance;//实例句柄 RegisterClass(&wnd); //创建窗口(返回之前发送WM_CREATE) HWND hWnd = CreateWindow(L"MrHuang",L"第一个窗口程序", WS_OVERLAPPEDWINDOW,100,100,400 ,400,NULL,NULL, hInstance,NULL ); //显示窗口 ShowWindow(hWnd, nShowCmd); //更新窗口(发送WM_PAINT) UpdateWindow(hWnd); //消息循环(收到WM_QUIT消息退出) MSG msg; while ( GetMessage(&msg, NULL, 0,0 ) ) { TranslateMessage(&msg);//翻译消息 DispatchMessage(&msg);//分发消息到窗口过程 } }
1)什么是测量条
2)样式
3)代码实例
#include<windows.h> #include<CommCtrl.h> //自定义的窗口过程 LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 return 0; case WM_CREATE: { //创建刻度条控件 HWND hTackbar = CreateWindow(TRACKBAR_CLASS, L"XXX", WS_CHILD | WS_VISIBLE| TBS_TOP|TBS_AUTOTICKS|TBS_ENABLESELRANGE, 30, 30, 200, 70, hWnd, (HMENU)111, GetModuleHandle(NULL), NULL); //设置测量条的范围 SendMessage(hTackbar, TBM_SETRANGE, TRUE, MAKELPARAM(0, 10)); //设置测量条的滑块位置 SendMessage(hTackbar, TBM_SETPOS, TRUE, 5 ); //设置滑块的选择区域 SendMessage(hTackbar, TBM_SETSEL, TRUE, MAKELPARAM(1,3)); }break; case WM_HSCROLL: { WORD code = LOWORD(wParam);//指示用户的滚动行为 WORD value = HIWORD(wParam);//仅当code是TB_THUMBPOSITION或TB_THUMBTRACK ,HIWORD(wParam)指定在滚动框的当前位置,否则忽略! HWND hCtrl =(HWND) lParam;//控件句柄 /* code的值: #define TB_LINEUP 0 #define TB_LINEDOWN 1 #define TB_PAGEUP 2 #define TB_PAGEDOWN 3 #define TB_THUMBPOSITION 4 #define TB_THUMBTRACK 5 #define TB_TOP 6 #define TB_BOTTOM 7 #define TB_ENDTRACK 8 */ UINT v = SendMessage(hCtrl, TBM_GETPOS, 0, 0); TCHAR buf[64] = {0}; wsprintf(buf, L"当前位置: %d LOWORD: %d HIWORD: %d " , v, code ,value); OutputDebugString(buf); }break; } return DefWindowProc(hWnd, Msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //1.注册窗口类 WNDCLASS wnd; wnd.cbClsExtra = 0; wnd.cbWndExtra = 0; wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色 wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标 //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息 wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数 wnd.lpszClassName = L"MrHuang";//窗口类名 wnd.lpszMenuName = NULL;//菜单资源名称 wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式 wnd.hInstance = hInstance;//实例句柄 RegisterClass(&wnd); //创建窗口(返回之前发送WM_CREATE) HWND hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL); //显示窗口 ShowWindow(hWnd, nShowCmd); //更新窗口(发送WM_PAINT) UpdateWindow(hWnd); //消息循环(收到WM_QUIT消息退出) MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg);//翻译消息 DispatchMessage(&msg);//分发消息到窗口过程 } }
1)什么是日期时间控件
2)样式
3)向时间控件发送消息设置控件
4)格式字符串
5)设置时间格式字符串
6)控件向父窗口发送消息
7)代码实例
#include<windows.h> #include<CommCtrl.h> //#pragma comment(lib, "COMCTL32.LIB") //自定义的窗口过程 LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 return 0; case WM_NOTIFY: { NMHDR *pHDR = (NMHDR *)lParam; if (pHDR->code == DTN_DATETIMECHANGE) { LPNMDATETIMECHANGE pDT = (LPNMDATETIMECHANGE)lParam; SYSTEMTIME st = pDT->st; TCHAR buf[64] = {0}; wsprintf(buf, L"%04d-%02d-%02d %02d:%02d:%02d ", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); MessageBox(hWnd, buf, L"提示", MB_OK); } }break; case WM_CREATE: { //创建一个日期时间选择控件 HWND hDatetime = CreateWindow(DATETIMEPICK_CLASS, L"XXX", WS_CHILD | WS_VISIBLE| DTS_SHOWNONE, 30, 30, 200, 30, hWnd, (HMENU)111, ((LPCREATESTRUCT)lParam)->hInstance, NULL); //设置控件的显示格式 SendMessage(hDatetime, DTM_SETFORMAT, 0, (LPARAM)L"yyyy-MM-dd hh:mm:ss tt"); //设置控件的显示时间 SYSTEMTIME st; st.wYear = 1999; st.wMonth = 8; st.wDay = 7; st.wHour = 6; st.wMinute = 5; st.wSecond = 4; st.wMilliseconds = 3; SendMessage(hDatetime, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st); //设置时间为无 //SendMessage(hDatetime, DTM_SETSYSTEMTIME, GDT_NONE, 0); }break; } return DefWindowProc(hWnd, Msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //1.注册窗口类 WNDCLASS wnd; wnd.cbClsExtra = 0; wnd.cbWndExtra = 0; wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色 wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标 //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息 wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数 wnd.lpszClassName = L"MrHuang";//窗口类名 wnd.lpszMenuName = NULL;//菜单资源名称 wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式 wnd.hInstance = hInstance;//实例句柄 RegisterClass(&wnd); //创建窗口(返回之前发送WM_CREATE) HWND hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL); //显示窗口 ShowWindow(hWnd, nShowCmd); //更新窗口(发送WM_PAINT) UpdateWindow(hWnd); //消息循环(收到WM_QUIT消息退出) MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg);//翻译消息 DispatchMessage(&msg);//分发消息到窗口过程 } }
1)什么是热键控件
2)向控件发送消息
3)代码实例
#include<windows.h> #include<CommCtrl.h> //#pragma comment(lib, "COMCTL32.LIB") //自定义的窗口过程 LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 return 0; case WM_COMMAND: { HWND hCtrl = (HWND)lParam; WORD CtrlID = LOWORD(wParam); WORD code=HIWORD(wParam); if (code == EN_CHANGE) { //获取组合键的内容 WORD key = SendMessage(hCtrl, HKM_GETHOTKEY, 0, 0); BYTE l = LOBYTE(LOWORD(key));//字母等 BYTE h = HIBYTE(LOWORD(key));//CTRL | ALT |SHIFT TCHAR buf[64] = { 0 }; wsprintf(buf, L"%c + %d + %d ", l, bool(h & HOTKEYF_CONTROL), bool(h & HOTKEYF_ALT)); OutputDebugString(buf); } }break; case WM_CREATE: { //创建热键控件 HWND hHotkey = CreateWindow(HOTKEY_CLASS,L"XXX", WS_CHILD | WS_VISIBLE, 30, 30, 200, 30, hWnd, (HMENU)111, GetModuleHandle(NULL), NULL); //设置组合键的内容(CTRL+ALT+A) SendMessage(hHotkey, HKM_SETHOTKEY, MAKEWORD('A', HOTKEYF_ALT| HOTKEYF_CONTROL), 0); //获取组合键的内容 WORD key=SendMessage(hHotkey, HKM_GETHOTKEY, 0, 0); BYTE l = LOBYTE(LOWORD(key));//字母等 BYTE h =HIBYTE(LOWORD(key));//CTRL | ALT |SHIFT TCHAR buf[64] = {0}; wsprintf(buf, L"%c + %d + %d ", l, bool( h & HOTKEYF_CONTROL), bool(h & HOTKEYF_ALT)); OutputDebugString(buf); }break; } return DefWindowProc(hWnd, Msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //1.注册窗口类 WNDCLASS wnd; wnd.cbClsExtra = 0; wnd.cbWndExtra = 0; wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色 wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标 //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息 wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数 wnd.lpszClassName = L"MrHuang";//窗口类名 wnd.lpszMenuName = NULL;//菜单资源名称 wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式 wnd.hInstance = hInstance;//实例句柄 RegisterClass(&wnd); //创建窗口(返回之前发送WM_CREATE) HWND hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL); //显示窗口 ShowWindow(hWnd, nShowCmd); //更新窗口(发送WM_PAINT) UpdateWindow(hWnd); //消息循环(收到WM_QUIT消息退出) MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg);//翻译消息 DispatchMessage(&msg);//分发消息到窗口过程 } }
1)什么是链接控件
2)样式
3)注意事项
4)控件向父窗口发送消息
5)LITEM结构体
6)代码实例
#include<windows.h> #include<CommCtrl.h> //指定清单依赖项,否则无法创建SysLink控件 #pragma comment(linker,""/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version = '6.0.0.0' processorArchitecture = '*' publicKeyToken = '6595b64144ccf1df' language = '*'"") //自定义的窗口过程 LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 return 0; case WM_NOTIFY: { //WM_NOTIFY参数的公共头部 NMHDR *pHDR = (NMHDR*)lParam; if ((pHDR->code == NM_CLICK) && ( pHDR->idFrom = 111)) { PNMLINK pLink = (PNMLINK)lParam; LITEM & item = pLink->item; if (item.iLink == 0)//第一个<a>标签 { //打开网站item.szUrl; ShellExecute(NULL, L"open", item.szUrl, NULL, NULL, SW_SHOW); } else if (item.iLink == 1)//第二个<a>标签 { ShellExecute(NULL, L"open", L"https://www.baidu.com", NULL, NULL, SW_SHOW); } } }break; case WM_CREATE: { //创建超链接控件 HWND hLink = CreateWindow(WC_LINK, L"<A href="https://nmwsoft.taobao.com/">这是超链接</A>我爱中国!<A id="he" href="hello">这是第二个超链接</A>", WS_CHILD | WS_VISIBLE, 30, 30, 200, 30, hWnd, (HMENU)111, GetModuleHandle(NULL), NULL); } break; } return DefWindowProc(hWnd, Msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //1.注册窗口类 WNDCLASS wnd; wnd.cbClsExtra = 0; wnd.cbWndExtra = 0; wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色 wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标 //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息 wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数 wnd.lpszClassName = L"MrHuang";//窗口类名 wnd.lpszMenuName = NULL;//菜单资源名称 wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式 wnd.hInstance = hInstance;//实例句柄 RegisterClass(&wnd); //创建窗口(返回之前发送WM_CREATE) HWND hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL); //显示窗口 ShowWindow(hWnd, nShowCmd); //更新窗口(发送WM_PAINT) UpdateWindow(hWnd); //消息循环(收到WM_QUIT消息退出) MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg);//翻译消息 DispatchMessage(&msg);//分发消息到窗口过程 } }
1)什么是日历控件
2)样式
3)向控件发送消息设置控件
4)控件向父窗口发送消息
5)代码实例
#include<windows.h> #include<CommCtrl.h> //自定义的窗口过程 LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 return 0; case WM_NOTIFY: { //WM_NOTIFY参数的公共头部 NMHDR *pHDR = (NMHDR*)lParam; if ((pHDR->code == MCN_SELCHANGE) && (pHDR->idFrom == 111)) { LPNMSELCHANGE pSelect = (LPNMSELCHANGE)lParam; SYSTEMTIME st0 = pSelect->stSelStart; SYSTEMTIME st1 = pSelect->stSelEnd; TCHAR buf[64] = { 0 }; wsprintf(buf, L"%04d-%02d-%02d %02d:%02d:%02d 至 %04d-%02d-%02d %02d:%02d:%02d", st0.wYear, st0.wMonth, st0.wDay, st0.wHour, st0.wMinute, st0.wSecond, st1.wYear, st1.wMonth, st1.wDay, st1.wHour, st1.wMinute, st1.wSecond); MessageBox(hWnd, buf, L"提示", MB_OK); } }break; case WM_CREATE: { //创建日历控件 HWND hCalendar = CreateWindow(MONTHCAL_CLASS, L"XXX", WS_CHILD | WS_VISIBLE , 30, 30, 600, 600, hWnd, (HMENU)111, ((LPCREATESTRUCT)lParam)->hInstance, NULL); //设置可选择日期的范围 SYSTEMTIME st[2];//0放最小值,1放最大值 st[0].wYear = 2018; st[0].wMonth = 1; st[0].wDay = 1; st[1].wYear = 2018; st[1].wMonth = 1; st[1].wDay = 7; SendMessage(hCalendar, MCM_SETRANGE, GDTR_MAX | GDTR_MIN, (LPARAM)st); //选择某个日期值20180107 SendMessage(hCalendar, MCM_SETCURSEL, 0 , (LPARAM)&st[1]); } break; } return DefWindowProc(hWnd, Msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //1.注册窗口类 WNDCLASS wnd; wnd.cbClsExtra = 0; wnd.cbWndExtra = 0; wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色 wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标 //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息 wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数 wnd.lpszClassName = L"MrHuang";//窗口类名 wnd.lpszMenuName = NULL;//菜单资源名称 wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式 wnd.hInstance = hInstance;//实例句柄 RegisterClass(&wnd); //创建窗口(返回之前发送WM_CREATE) HWND hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL); //显示窗口 ShowWindow(hWnd, nShowCmd); //更新窗口(发送WM_PAINT) UpdateWindow(hWnd); //消息循环(收到WM_QUIT消息退出) MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg);//翻译消息 DispatchMessage(&msg);//分发消息到窗口过程 } }
1)什么是进度条控件
2)样式
3)向控件发送消息
4)代码实例
#include<windows.h> #include<CommCtrl.h> //依赖项指令 #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"") HWND hProgress = NULL; //自定义的窗口过程 LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 return 0; case WM_LBUTTONDOWN: { //让进度条移动一个步长 SendMessage(hProgress, PBM_STEPIT, 10, 0); }break; case WM_CREATE: { //创建进度条 hProgress = CreateWindow(PROGRESS_CLASS,L"XXX", WS_CHILD | WS_VISIBLE , 30, 30, 200,30, hWnd,(HMENU)111,GetModuleHandle(NULL),NULL); //设置进度条的范围 SendMessage(hProgress, PBM_SETRANGE, 0, MAKELPARAM(1, 100)); //设置进度条的位置 SendMessage(hProgress, PBM_SETPOS, 40, 0); //设置进度条步长 SendMessage(hProgress, PBM_SETSTEP, 10, 0); //开启进度条左右重复滚动 //SendMessage(hProgress, PBM_SETMARQUEE, 1, 10); }break; } return DefWindowProc(hWnd, Msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //1.注册窗口类 WNDCLASS wnd; wnd.cbClsExtra = 0; wnd.cbWndExtra = 0; wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色 wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标 //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息 wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数 wnd.lpszClassName = L"MrHuang";//窗口类名 wnd.lpszMenuName = NULL;//菜单资源名称 wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式 wnd.hInstance = hInstance;//实例句柄 RegisterClass(&wnd); //创建窗口(返回之前发送WM_CREATE) HWND hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL); //显示窗口 ShowWindow(hWnd, nShowCmd); //更新窗口(发送WM_PAINT) UpdateWindow(hWnd); //消息循环(收到WM_QUIT消息退出) MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg);//翻译消息 DispatchMessage(&msg);//分发消息到窗口过程 } }
1)什么是列表视图控件
2)样式
图标和列表视图的样式
报告视图的样式
3)向控件发送消息
4)图像列表(可以在资源中加载)
5)加载图像列表
6)控件向窗口发送消息
7)代码实例
①图标视图
#include<windows.h> #include<CommCtrl.h> #include"resource.h" //包含图标资源ID #pragma comment(lib, "Comctl32.lib") //加入lib文件 //自定义的窗口过程 LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 return 0; case WM_NOTIFY: { NMHDR *pHDR = (NMHDR*)lParam; if ((pHDR->code == LVN_ITEMCHANGED) && (pHDR->idFrom == 111)) { LPNMLISTVIEW pnmv = (LPNMLISTVIEW)lParam; if (pnmv->uChanged == LVIF_STATE)//是不是选中状态改变了 { if (pnmv->uNewState & LVIS_SELECTED) { //根据索引pnmv->iItem获取文本 LVITEM lv; TCHAR buf[64] = {0}; lv.iSubItem = 0; lv.pszText = buf; //向系统提供写入内存 lv.cchTextMax = sizeof(buf);//buf的长度 SendMessage(pHDR->hwndFrom, LVM_GETITEMTEXT, pnmv->iItem, (LPARAM)&lv); MessageBox(hWnd, buf, L"提示", MB_OK); } } } }break; case WM_CREATE: { //创建列表视图 LVS_ICON 图标视图 HWND hListView = CreateWindow(WC_LISTVIEW, L"XXX", WS_CHILD | WS_VISIBLE | LVS_ICON, 30, 30, 300, 300, hWnd, (HMENU)111, ((LPCREATESTRUCT)lParam)->hInstance, NULL); //创建Image Lists UINT x = GetSystemMetrics(SM_CXICON); UINT y = GetSystemMetrics(SM_CYICON); HIMAGELIST hLarge= ImageList_Create(x, y, ILC_MASK, 3, 2); //把图标加入到 ImageLists去 for (int i = 0; i < 3; ++i) { HICON hIcon = LoadIcon(((LPCREATESTRUCT)lParam)->hInstance, MAKEINTRESOURCE(IDI_ICON1+ i)); ImageList_AddIcon(hLarge, hIcon); DestroyIcon(hIcon); } //把image Lists与 listView 相关联 SendMessage(hListView, LVM_SETIMAGELIST, LVSIL_NORMAL, (LPARAM)hLarge); //插入项目 for (int i = 0; i < 3; ++i) { LVITEM lv; lv.pszText = (i == 0) ? L"音乐" : ((i == 1) ? L"啤酒" : L"炸鸡"); lv.mask = LVIF_TEXT| LVIF_IMAGE| LVIF_STATE ; //哪些值有效的标记码 lv.iItem = i; //插入的位置 lv.iImage = i;//图像的索引 lv.state = (i == 2) ? LVIS_SELECTED:0; //是否选中 lv.iSubItem = 0; SendMessage(hListView, LVM_INSERTITEM,0,(LPARAM) &lv); } //设置控件有焦点 SetFocus(hListView); //设置文本颜色 SendMessage(hListView, LVM_SETTEXTCOLOR, 0, (LPARAM) RGB(255,0,0)); //设置背景颜色 SendMessage(hListView, LVM_SETBKCOLOR, 0, (LPARAM)RGB(0, 255, 0)); //设置文本背景颜色 SendMessage(hListView, LVM_SETTEXTBKCOLOR, 0, (LPARAM)RGB(0, 0, 255)); }break; } return DefWindowProc(hWnd, Msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //1.注册窗口类 WNDCLASS wnd; wnd.cbClsExtra = 0; wnd.cbWndExtra = 0; wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色 wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标 //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息 wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数 wnd.lpszClassName = L"MrHuang";//窗口类名 wnd.lpszMenuName = NULL;//菜单资源名称 wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式 wnd.hInstance = hInstance;//实例句柄 RegisterClass(&wnd); //创建窗口(返回之前发送WM_CREATE) HWND hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL); //显示窗口 ShowWindow(hWnd, nShowCmd); //更新窗口(发送WM_PAINT) UpdateWindow(hWnd); //消息循环(收到WM_QUIT消息退出) MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg);//翻译消息 DispatchMessage(&msg);//分发消息到窗口过程 } }
②小图标以及列表视图
#include<windows.h> #include<CommCtrl.h> #include"resource.h" //包含图标资源ID #pragma comment(lib, "Comctl32.lib") //加入lib文件 //自定义的窗口过程 LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 return 0; case WM_NOTIFY: { NMHDR *pHDR = (NMHDR*)lParam; if ((pHDR->code == LVN_ITEMCHANGED) && (pHDR->idFrom == 111)) { LPNMLISTVIEW pnmv = (LPNMLISTVIEW)lParam; if (pnmv->uChanged == LVIF_STATE)//是不是选中状态改变了 { if (pnmv->uNewState & LVIS_SELECTED) { //根据索引pnmv->iItem获取文本 LVITEM lv; TCHAR buf[64] = { 0 }; lv.iSubItem = 0; lv.pszText = buf; //向系统提供写入内存 lv.cchTextMax = sizeof(buf);//buf的长度 SendMessage(pHDR->hwndFrom, LVM_GETITEMTEXT, pnmv->iItem, (LPARAM)&lv); MessageBox(hWnd, buf, L"提示", MB_OK); } } } }break; case WM_CREATE: { //创建列表视图 LVS_SMALLICON 小图标视图 /*HWND hListView = CreateWindow(WC_LISTVIEW, L"XXX", WS_CHILD | WS_VISIBLE | LVS_SMALLICON, 30, 30, 300, 300, hWnd, (HMENU)111, ((LPCREATESTRUCT)lParam)->hInstance, NULL);*/ //创建列表视图 LVS_LIST 列表视图 HWND hListView = CreateWindow(WC_LISTVIEW, L"XXX", WS_CHILD | WS_VISIBLE | LVS_LIST, 30, 30, 300, 300, hWnd, (HMENU)111, ((LPCREATESTRUCT)lParam)->hInstance, NULL); //创建Image Lists (小图标SM_CXSMICON) UINT x = GetSystemMetrics(SM_CXSMICON); UINT y = GetSystemMetrics(SM_CXSMICON); HIMAGELIST hSmall = ImageList_Create(x, y, ILC_MASK, 3, 2); //把图标加入到 ImageLists去 for (int i = 0; i < 3; ++i) { HICON hIcon = LoadIcon(((LPCREATESTRUCT)lParam)->hInstance, MAKEINTRESOURCE(IDI_ICON1 + i)); ImageList_AddIcon(hSmall, hIcon); DestroyIcon(hIcon); } //把image Lists与 listView 相关联 LVSIL_SMALL SendMessage(hListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)hSmall); //插入项目 for (int i = 0; i < 3; ++i) { LVITEM lv; lv.pszText = (i == 0) ? L"音乐" : ((i == 1) ? L"啤酒" : L"炸鸡"); lv.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE; //哪些值有效的标记码 lv.iItem = i; //插入的位置 lv.iImage = i;//图像的索引 lv.state = (i == 2) ? LVIS_SELECTED : 0; //是否选中 lv.iSubItem = 0; SendMessage(hListView, LVM_INSERTITEM, 0, (LPARAM)&lv); } //设置控件有焦点 SetFocus(hListView); //设置文本颜色 SendMessage(hListView, LVM_SETTEXTCOLOR, 0, (LPARAM)RGB(255, 0, 0)); //设置背景颜色 SendMessage(hListView, LVM_SETBKCOLOR, 0, (LPARAM)RGB(0, 255, 0)); //设置文本背景颜色 SendMessage(hListView, LVM_SETTEXTBKCOLOR, 0, (LPARAM)RGB(0, 0, 255)); }break; } return DefWindowProc(hWnd, Msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //1.注册窗口类 WNDCLASS wnd; wnd.cbClsExtra = 0; wnd.cbWndExtra = 0; wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色 wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标 //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息 wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数 wnd.lpszClassName = L"MrHuang";//窗口类名 wnd.lpszMenuName = NULL;//菜单资源名称 wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式 wnd.hInstance = hInstance;//实例句柄 RegisterClass(&wnd); //创建窗口(返回之前发送WM_CREATE) HWND hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL); //显示窗口 ShowWindow(hWnd, nShowCmd); //更新窗口(发送WM_PAINT) UpdateWindow(hWnd); //消息循环(收到WM_QUIT消息退出) MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg);//翻译消息 DispatchMessage(&msg);//分发消息到窗口过程 } }
③报表视图
#include<windows.h> #include<CommCtrl.h> #include"resource.h" //包含图标资源ID #pragma comment(lib, "Comctl32.lib") //加入lib文件 //加入指令改变控件的风格 #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"") //自定义的窗口过程 LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: PostQuitMessage(0);//可以使GetMessage返回0 return 0; case WM_NOTIFY: { NMHDR *pHDR = (NMHDR*)lParam; if ((pHDR->code == LVN_ITEMCHANGED) && (pHDR->idFrom == 111)) { LPNMLISTVIEW pnmv = (LPNMLISTVIEW)lParam; if (pnmv->uChanged == LVIF_STATE)//是不是选中状态改变了 { if (pnmv->uNewState & LVIS_SELECTED) { //根据索引pnmv->iItem获取文本 LVITEM lv; TCHAR buf[64] = { 0 }; lv.iSubItem = 2; lv.pszText = buf; //向系统提供写入内存 lv.cchTextMax = sizeof(buf);//buf的长度 SendMessage(pHDR->hwndFrom, LVM_GETITEMTEXT, pnmv->iItem, (LPARAM)&lv); MessageBox(hWnd, buf, L"提示", MB_OK); } } } }break; case WM_CREATE: { //创建列表视图 LVS_REPORT 报表视图 HWND hListView = CreateWindow(WC_LISTVIEW, L"XXX", WS_CHILD | WS_VISIBLE | LVS_REPORT, 30, 30, 300, 300, hWnd, (HMENU)111, ((LPCREATESTRUCT)lParam)->hInstance, NULL); //创建Image Lists (小图标SM_CXSMICON) UINT x = GetSystemMetrics(SM_CXSMICON); UINT y = GetSystemMetrics(SM_CXSMICON); HIMAGELIST hSmall = ImageList_Create(x, y, ILC_MASK, 3, 2); //把图标加入到 ImageLists去 for (int i = 0; i < 3; ++i) { HICON hIcon = LoadIcon(((LPCREATESTRUCT)lParam)->hInstance, MAKEINTRESOURCE(IDI_ICON1 + i)); ImageList_AddIcon(hSmall, hIcon); DestroyIcon(hIcon); } //把image Lists与 listView 相关联 LVSIL_SMALL SendMessage(hListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)hSmall); //设置控件的扩展样式 SendMessage(hListView, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES)); //插入列 for (int i = 0; i < 3; ++i) { LVCOLUMN lvc; lvc.pszText = (i == 0 ? L"姓名" : (i == 1 ? L"性别" : L"年龄")); lvc.mask= LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; lvc.iSubItem = 0; //第几列 lvc.cx = 100;//列的宽度 lvc.fmt = (i == 0 ? LVCFMT_LEFT : LVCFMT_CENTER); //文本对齐方式 SendMessage(hListView, LVM_INSERTCOLUMN, i , (LPARAM) &lvc ); } //插入一行数据 for (int i = 0; i < 3; ++i) { LVITEM lv; lv.pszText = (i == 0) ? L"张三" : ((i == 1) ? L"男" : L"13"); lv.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE; //哪些值有效的标记码 lv.iItem = 0; //插入第0行 lv.iImage = i;//图像的索引 lv.state = (i == 2) ? LVIS_SELECTED : 0; //是否选中 lv.iSubItem = i; //第几列的文本 //表示插入一行 if(i==0) SendMessage(hListView, LVM_INSERTITEM, 0, (LPARAM)&lv); //表示插入第几列的文本 else SendMessage(hListView, LVM_SETITEMTEXT, 0, (LPARAM)&lv); } //设置控件有焦点 SetFocus(hListView); //设置文本颜色 //SendMessage(hListView, LVM_SETTEXTCOLOR, 0, (LPARAM)RGB(255, 0, 0)); //设置背景颜色 //SendMessage(hListView, LVM_SETBKCOLOR, 0, (LPARAM)RGB(0, 255, 0)); //设置文本背景颜色 //SendMessage(hListView, LVM_SETTEXTBKCOLOR, 0, (LPARAM)RGB(0, 0, 255)); }break; } return DefWindowProc(hWnd, Msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //1.注册窗口类 WNDCLASS wnd; wnd.cbClsExtra = 0; wnd.cbWndExtra = 0; wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色 wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标 //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息 wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数 wnd.lpszClassName = L"MrHuang";//窗口类名 wnd.lpszMenuName = NULL;//菜单资源名称 wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式 wnd.hInstance = hInstance;//实例句柄 RegisterClass(&wnd); //创建窗口(返回之前发送WM_CREATE) HWND hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL); //显示窗口 ShowWindow(hWnd, nShowCmd); //更新窗口(发送WM_PAINT) UpdateWindow(hWnd); //消息循环(收到WM_QUIT消息退出) MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg);//翻译消息 DispatchMessage(&msg);//分发消息到窗口过程 } }