win32 API编程之常用控件
1)注意事项
- 创建按钮一般在WM_CREATE消息中;
- 系统注册的按钮类名是“button”,不区分大小写。
- WS_CHILD:控件是放在我们的窗口上的,自然要作为窗口的子窗口
- WS_CHILDWINDOW也一样,为了节约几个字母,用WS_CHILD吧。
- WS_VISIBLE:设置可见。
- 创建窗口函数讲解
HWND WINAPI CreateWindow( 类名, 显示内容, 风格, nt x, int y,
int nWidth, int nHeight, 父窗口, 控件ID,
HINSTANCE hInstance, LPVOID lpParam );
2)按钮样式
3)获取单选按钮与复选按钮的状态
4)代码实例
#include<windows.h> #include<windowsx.h> //Button_GetCheck的头文件 #include<stdio.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: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //创建控件或者子窗口 //普通按钮BS_PUSHBUTTON HWND hBtn=CreateWindow(L"button", L"普通按钮", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON ,30, 30, 80, 30, hWnd, (HMENU)1000,//控件ID pcs->hInstance, NULL); //单选按钮 HWND hBtn1 = CreateWindow(L"button", L"单选按钮", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 30, 70, 80, 30, hWnd, (HMENU)1001,//控件ID pcs->hInstance, NULL); //复选按钮 HWND hBtn2 = CreateWindow(L"button", L"复选按钮", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 30, 110, 80, 30, hWnd, (HMENU)1002,//控件ID pcs->hInstance, NULL); return 0; } case WM_COMMAND: { WORD id = LOWORD(wParam); WORD code= HIWORD(wParam); HWND hCtrl = (HWND)lParam; if (id == 1000 && code == BN_CLICKED) { MessageBox(hWnd, L"普通按钮被单击!", L"提示",MB_OK); } else if (id == 1001) { MessageBox(hWnd, L"单选按钮被单击!", L"提示", MB_OK); if (SendMessage(hCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED) { MessageBox(hWnd, L"被选中!", L"提示", MB_OK); } else if (SendMessage(hCtrl, BM_GETCHECK, 0, 0) == BST_UNCHECKED) { MessageBox(hWnd, L"没选中!", L"提示", MB_OK); } } else if (id == 1002) { MessageBox(hWnd, L"复选按钮被单击!", L"提示", MB_OK); /* if (SendMessage(hCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED) { MessageBox(hWnd, L"被选中!", L"提示", MB_OK); } else if (SendMessage(hCtrl, BM_GETCHECK, 0, 0) == BST_UNCHECKED) { MessageBox(hWnd, L"没选中!", L"提示", MB_OK); } */ //简写形式((int)(DWORD)SNDMSG((hwndCtl), BM_GETCHECK, 0L, 0L)) if (Button_GetCheck(hCtrl) == BST_CHECKED) { MessageBox(hWnd, L"被选中!", L"提示", MB_OK); } else if (Button_GetCheck(hCtrl) == BST_UNCHECKED) { MessageBox(hWnd, L"没选中!", L"提示", MB_OK); } } }break; default: 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);//分发消息到窗口过程 } }
代码实例
#include<windows.h> #include<stdio.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: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //创建位图按钮 HWND hBtn = CreateWindow(L"BuTtOn" /*不区分大小写*/, L"XXX", WS_CHILD | WS_VISIBLE | BS_BITMAP,//指定为图片 30, 30, 217, 46, hWnd, (HMENU)1000, pcs->hInstance, NULL); //加载位图 HBITMAP hBmp=(HBITMAP)LoadImage(NULL, L"C:\\Users\\35497\\Desktop\\code\\016\\Debug\\btn1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); //向控件发送BM_SETIMAGE消息设置按钮图片 LRESULT ret=SendMessage(hBtn, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp); return 0; } case WM_COMMAND: { WORD id = LOWORD(wParam); WORD code = HIWORD(wParam); HWND hCtrl = (HWND)lParam; if (id == 1000 && code == BN_CLICKED) { MessageBox(hWnd, L"普通按钮被单击!", L"提示", MB_OK); } }break; default: 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)创建步骤
1)相关函数
2)LoadBitmap函数
3)LoadCursor函数
4)代码实例
#include<windows.h> #include<stdio.h> #include"resource.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: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //创建位图按钮 HWND hBtn = CreateWindow(L"BuTtOn" /*不区分大小写*/, L"XXX", WS_CHILD | WS_VISIBLE | BS_BITMAP,//指定为图片 30, 30, 217, 46, hWnd, (HMENU)1000, pcs->hInstance, NULL); //LoadImage加载位图 //HBITMAP hBmp=(HBITMAP)LoadImage(NULL, L"C:\\Users\\35497\\Desktop\\code\\016\\Debug\\btn1.bmp", // IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); //采用LoadBitmap加载位图资源(必须在rc文件中定义) HBITMAP hBmp= LoadBitmap(pcs->hInstance, MAKEINTRESOURCE(IDB_BITMAP1) ); //向控件发送BM_SETIMAGE消息设置按钮图片 LRESULT ret=SendMessage(hBtn, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp); return 0; } case WM_COMMAND: { WORD id = LOWORD(wParam); WORD code = HIWORD(wParam); HWND hCtrl = (HWND)lParam; if (id == 1000 && code == BN_CLICKED) { MessageBox(hWnd, L"普通按钮被单击!", L"提示", MB_OK); } }break; default: 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));//背景色 //加载系统光标,第一个参数为NULL //wnd.hCursor = LoadCursor(NULL, IDC_ARROW); //加载资源中的光标 wnd.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR2)); //加载系统图标,第一个参数为NULL //wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); //加载资源中的图标 wnd.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON2)); //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息 wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数 wnd.lpszClassName = L"MrHuang";//窗口类名 wnd.lpszMenuName = NULL;//菜单资源名称 wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式 wnd.hInstance = hInstance;//实例句柄 RegisterClass(&wnd); //加载字符串资源 TCHAR buf[64] = {0}; LoadString(hInstance, IDS_STRING1, buf, sizeof(buf)); LoadString(GetModuleHandle(NULL), IDS_STRING2, buf, sizeof(buf)); //创建窗口(返回之前发送WM_CREATE) HWND hWnd = CreateWindow(L"MrHuang", buf, 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)设置文本内容SetWindowText
4)可以向静态文本框发送消息来设置图标、位图等
5)代码实例
#include<windows.h> #include"resource.h" #include<stdio.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: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //静态文本控件 HWND hStatic= CreateWindow(L"Static", L"这是一个静态文本", WS_CHILD | WS_VISIBLE |WS_BORDER|SS_BITMAP|SS_NOTIFY, 30, 30,200, 30, hWnd, (HMENU)111, pcs->hInstance, NULL); //更改窗口标题 SetWindowText(hWnd, L"新窗口标题"); //设置控件(子窗口)内容 SetWindowText(hStatic, L"更改后的内容!"); //加载位图 HBITMAP hBmp= LoadBitmap(pcs->hInstance, MAKEINTRESOURCE(IDB_BITMAP1)); //向控件发送STM_SETIMAGE消息设置静态控件的位图 SendMessage(hStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBmp); return 0; } case WM_COMMAND: { WORD id = LOWORD(wParam);//控件ID WORD code = HIWORD(wParam);//消息类型 HWND hCtrl = (HWND)lParam;//窗口句柄 //父窗口接收到子控件发的消息 if (id == 111 && code == STN_CLICKED) { MessageBox(NULL, L"静态文本被点击!", L"提示", MB_OK); } }break; default: 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)在WM_CTLCOLORSTATIC消息中美化静态文本。
3)GetWindowLong()函数可以获取控件的信息。
2)代码实例
#include<windows.h> #include<stdio.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: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //静态文本控件 HWND hStatic = CreateWindow(L"Static", L"这是一个静态文本", WS_CHILD | WS_VISIBLE , 30, 30, 200, 30, hWnd, (HMENU)111, pcs->hInstance, NULL); //静态文本控件 HWND hStatic2 = CreateWindow(L"Static", L"这是二个静态文本", WS_CHILD | WS_VISIBLE, 30, 80, 200, 30, hWnd, (HMENU)222, pcs->hInstance, NULL); return 0; } //静态文本控件绘制之前发送的消息 case WM_CTLCOLORSTATIC: { HDC hdc = (HDC)wParam;//设备上下文的句柄 HWND hStatic = (HWND)lParam;//控件句柄 UINT CtrlID = GetWindowLong(hStatic, GWL_ID); //获取控件ID if(CtrlID ==111) //当控件ID=111时才美化 { SetTextColor(hdc, RGB(255, 0, 0));//设置文字为红色 //SetBkColor(hdc, RGB(0, 255, 0));//设置文字的背景色 SetBkMode(hdc, TRANSPARENT);//设置文字背景透明 //return (LRESULT)GetStockObject(BLACK_BRUSH);//返回系统画笔 return (LRESULT)CreateSolidBrush(RGB(0, 0, 255)); //控件的背景色,自己创建的画刷,需要释放 } else if (CtrlID == 222) { SetTextColor(hdc, RGB(0, 255, 0));//设置文字为红色 SetBkColor(hdc, RGB(255, 255, 0));//设置文字的背景色 return (LRESULT)GetStockObject(BLACK_BRUSH);//返回系统画笔 } }break; case WM_COMMAND: { WORD id = LOWORD(wParam);//控件ID WORD code = HIWORD(wParam);//消息类型 HWND hCtrl = (HWND)lParam;//窗口句柄 //父窗口接收到子控件发的消息 if (id == 111 && code == STN_CLICKED) { MessageBox(NULL, L"静态文本被点击!", L"提示", MB_OK); } }break; default: 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)代码实例
#include<windows.h> #include<stdio.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: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //创建编辑框控件 HWND hEdit=CreateWindow(L"ediT", L"这是一个编辑框", WS_CHILD | WS_VISIBLE | WS_VSCROLL |ES_MULTILINE|ES_AUTOVSCROLL|ES_NOHIDESEL, 30, 30, 200, 100, hWnd, (HMENU)111, pcs->hInstance, NULL); //向编辑框发送选择字符串的消息 SendMessage(hEdit, EM_SETSEL, 2, 6); //剪切刚刚选择的文字 //SendMessage(hEdit, WM_CUT, 0, 0); //撤销 //SendMessage(hEdit, EM_UNDO, 0, 0); return 0; } case WM_COMMAND: { WORD id = LOWORD(wParam);//控件ID WORD code = HIWORD(wParam);//消息类型 HWND hCtrl = (HWND)lParam;//窗口句柄 //父窗口接收到子控件发的消息 if (id == 111 && code == EN_CHANGE) { //MessageBox(NULL, L"编辑框的文本被改变!", L"提示", MB_OK); TCHAR buf[64] = {0}; GetWindowText(hCtrl, buf, sizeof(buf)); MessageBox(NULL, buf , L"提示", MB_OK); } }break; default: 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);//分发消息到窗口过程 } }
代码实例
#include<windows.h> #include<stdio.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: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //创建编辑框控件 HWND hEdit = CreateWindow(L"ediT", L"这是一个编辑框", WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_NOHIDESEL, 30, 30, 200, 100, hWnd, (HMENU)111, pcs->hInstance, NULL); //向编辑框发送选择字符串的消息 SendMessage(hEdit, EM_SETSEL, 2, 6); //剪切刚刚选择的文字 //SendMessage(hEdit, WM_CUT, 0, 0); //撤销 //SendMessage(hEdit, EM_UNDO, 0, 0); return 0; } case WM_COMMAND: { WORD id = LOWORD(wParam);//控件ID WORD code = HIWORD(wParam);//消息类型 HWND hCtrl = (HWND)lParam;//窗口句柄 //父窗口接收到子控件发的消息 if (id == 111 && code == EN_CHANGE) { //MessageBox(NULL, L"编辑框的文本被改变!", L"提示", MB_OK); TCHAR buf[64] = { 0 }; GetWindowText(hCtrl, buf, sizeof(buf)); MessageBox(NULL, buf, L"提示", MB_OK); } }break; case WM_CTLCOLOREDIT: { HDC hdc = (HDC)wParam; HWND hCtrl = (HWND)lParam; //获取编辑框控件的ID UINT CtrlID = GetWindowLong(hCtrl, GWL_ID); if (CtrlID == 111) { SetTextColor(hdc, RGB(255, 0, 0));//文字颜色 SetBkMode(hdc, TRANSPARENT);//文字背景色透明 } HBRUSH hbr = CreateSolidBrush(RGB(255, 255, 0)); return (LRESULT)hbr;//控件的背景色 }break; default: 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)利用SendMessage()函数获取选择的内容。
4)列表控件消息
5)代码实例
#include<windows.h> #include<stdio.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: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //创建列表框控件 HWND hListBox=CreateWindow(L"listBOX", L"XXX", WS_CHILD | WS_VISIBLE|LBS_SORT|LBS_NOTIFY, 30, 30, 200, 100, hWnd, (HMENU)111, GetModuleHandle(NULL), NULL); //向末尾添加选项 SendMessage(hListBox, LB_ADDSTRING, 0,(LPARAM) L"B音乐"); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)L"A啤酒"); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)L"C啤酒"); //插入字符串 SendMessage(hListBox, LB_INSERTSTRING, 1, (LPARAM)L"炸鸡"); //选择其中一项,选择索引为2的,单选模式才有效 SendMessage(hListBox, LB_SETCURSEL, 2, 0 ); return 0; } case WM_COMMAND: { WORD id = LOWORD(wParam);//控件ID WORD code = HIWORD(wParam);//消息类型 HWND hCtrl = (HWND)lParam;//窗口句柄 //父窗口接收到子控件发的消息 if (id == 111 && code == LBN_SELCHANGE)//创建时需指定LBS_NOTIFY样式 { // 获取当前选择的索引 int index=SendMessage(hCtrl, LB_GETCURSEL, 0,0); //获取索引对应的字符串 TCHAR buf[64] = { 0 }; SendMessage(hCtrl, LB_GETTEXT, index, (LPARAM)buf); MessageBox(NULL, buf, L"提示", MB_OK); } }break; default: 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);//分发消息到窗口过程 } }
代码实例
#include<windows.h> #include<stdio.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: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //创建列表框控件 HWND hListBox = CreateWindow(L"listBOX", L"XXX", WS_CHILD | WS_VISIBLE | LBS_SORT | LBS_NOTIFY, 30, 30, 200, 100, hWnd, (HMENU)111, GetModuleHandle(NULL), NULL); //向末尾添加选项 SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)L"B音乐"); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)L"A啤酒"); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)L"C啤酒"); //插入字符串 SendMessage(hListBox, LB_INSERTSTRING, 1, (LPARAM)L"炸鸡"); //选择其中一项,选择索引为2的,单选模式才有效 SendMessage(hListBox, LB_SETCURSEL, 2, 0); return 0; } case WM_CTLCOLORLISTBOX: { HDC hdc = (HDC)wParam; HWND hCtrl = (HWND)lParam; UINT CtrlID = GetWindowLong(hCtrl, GWL_ID); if (CtrlID == 111)//是不是想要美化的某一个列表框控件 { SetTextColor(hdc, RGB(255, 0, 0)); SetBkMode(hdc , TRANSPARENT); HBRUSH bsh = CreateSolidBrush(RGB(0, 255, 255)); return (LRESULT)bsh; } }break; case WM_COMMAND: { WORD id = LOWORD(wParam);//控件ID WORD code = HIWORD(wParam);//消息类型 HWND hCtrl = (HWND)lParam;//窗口句柄 //父窗口接收到子控件发的消息 if (id == 111 && code == LBN_SELCHANGE)//创建时需指定LBS_NOTIFY样式 { // 获取当前选择的索引 int index = SendMessage(hCtrl, LB_GETCURSEL, 0, 0); //获取索引对应的字符串 TCHAR buf[64] = { 0 }; SendMessage(hCtrl, LB_GETTEXT, index, (LPARAM)buf); MessageBox(NULL, buf, L"提示", MB_OK); } }break; default: 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)代码实例
#include<windows.h> #include<stdio.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: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //创建组合框 HWND hComboBox=CreateWindow(L"comboBOX", L"XXX", WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST|CBS_SORT, 30, 30, 200, 100, hWnd, (HMENU)111, pcs->hInstance, NULL); //向组合框添加选项 SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)L"C音乐"); SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)L"A啤酒"); SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)L"B炸鸡"); //插入选项 SendMessage(hComboBox, CB_INSERTSTRING, 2, (LPARAM)L"编程"); //选择某一项 SendMessage(hComboBox, CB_SETCURSEL, 3, 0 ); return 0; } case WM_COMMAND: { WORD id = LOWORD(wParam);//控件ID WORD code = HIWORD(wParam);//消息类型 HWND hCtrl = (HWND)lParam;//窗口句柄 //父窗口接收到子控件发的消息 if (id == 111 && code ==CBN_SELCHANGE) { //获取当前选择的索引 int index=SendMessage(hCtrl, CB_GETCURSEL, 0, 0); //获取索引对应的文本 TCHAR buf[64] = {0}; SendMessage(hCtrl, CB_GETLBTEXT, index, (LPARAM)buf); MessageBox(hWnd, buf, L"提示", MB_OK); } }break; default: 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)WM_HSCROLL接收并处理水平滚动条发送的消息
3)代码实例
#include<windows.h> #include<stdio.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: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //创建滚动条控件 HWND hScrollBar=CreateWindow(L"scrollBAR", L"XXX", WS_CHILD | WS_VISIBLE | SBS_HORZ, 30, 30, 200, 30, hWnd, (HMENU)111, pcs->hInstance, NULL); //设置滚动条的范围 SendMessage(hScrollBar, SBM_SETRANGE, 0, 100); //设置滚动条的滑块位置 SendMessage(hScrollBar, SBM_SETPOS, 40, 1); return 0; } //水平滚动条发送的消息 case WM_HSCROLL: { WORD code = LOWORD(wParam);//操作码 HWND hCtrl = (HWND)lParam;//控件句柄 //获取滚动条的滑块位置 int pos= SendMessage(hCtrl, SBM_GETPOS, 0, 0); switch (code) { //点击左边箭头 case SB_LINELEFT: OutputDebugString(L"SB_LINELEFT\n"); pos -= 1; break; //点击右边箭头 case SB_LINERIGHT: OutputDebugString(L"SB_LINERIGHT\n"); pos += 1; break; //点击左空白区 case SB_PAGELEFT: OutputDebugString(L"SB_PAGELEFT\n"); pos -= 10; break; //点击右空白区 case SB_PAGERIGHT: OutputDebugString(L"SB_PAGERIGHT\n"); pos += 10; break; //拖动滑块时的消息 case SB_THUMBPOSITION: pos = HIWORD(wParam);//拖拽位置,仅当SB_THUMBPOSITION有用 char str[64]; sprintf(str, "SB_THUMBPOSITION %d\n", pos); OutputDebugStringA(str); break; } //设置滚动条的滑块位置 SendMessage(hCtrl, SBM_SETPOS, pos, 1); }break; default: 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);//分发消息到窗口过程 } }
代码实例
#include<windows.h> #include<stdio.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: { LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam; //创建滚动条控件 HWND hScrollBar = CreateWindow(L"scrollBAR", L"XXX", WS_CHILD | WS_VISIBLE | SBS_HORZ, 30, 30, 200, 30, hWnd, (HMENU)111, pcs->hInstance, NULL); //设置滚动条的范围 SendMessage(hScrollBar, SBM_SETRANGE, 0, 100); //设置滚动条的滑块位置 SendMessage(hScrollBar, SBM_SETPOS, 40, 1); return 0; } case WM_CTLCOLORSCROLLBAR: { HDC hdc = (HDC)wParam; HWND hCtrl = (HWND)lParam; LONG CtrlID=GetWindowLong(hCtrl, GWL_ID); if (CtrlID == 111) { HBRUSH bsh = CreateSolidBrush(RGB(255, 0, 0)); return (LRESULT)bsh; } }break; //水平滚动条发送的消息 case WM_HSCROLL: { WORD code = LOWORD(wParam);//操作码 HWND hCtrl = (HWND)lParam;//控件句柄 //获取滚动条的滑块位置 int pos = SendMessage(hCtrl, SBM_GETPOS, 0, 0); switch (code) { //点击左边箭头 case SB_LINELEFT: OutputDebugString(L"SB_LINELEFT\n"); pos -= 1; break; //点击右边箭头 case SB_LINERIGHT: OutputDebugString(L"SB_LINERIGHT\n"); pos += 1; break; //点击左空白区 case SB_PAGELEFT: OutputDebugString(L"SB_PAGELEFT\n"); pos -= 10; break; //点击右空白区 case SB_PAGERIGHT: OutputDebugString(L"SB_PAGERIGHT\n"); pos += 10; break; //拖动滑块时的消息 case SB_THUMBPOSITION: pos = HIWORD(wParam);//拖拽位置,仅当SB_THUMBPOSITION有用 char str[64]; sprintf(str, "SB_THUMBPOSITION %d\n", pos); OutputDebugStringA(str); break; } //设置滚动条的滑块位置 SendMessage(hCtrl, SBM_SETPOS, pos, 1); }break; default: 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);//分发消息到窗口过程 } }