Win32编程day09 学习笔记
一 对话框
1 对话框的分类
2 对话框的基本使用方式
3 对话框资源
4 有模式对话框的使用
5 无模式对话框的使用
5.1 添加对话框资源
5.2 定义窗口处理函数
BOOL CALLBACK DialogProc( HWND hwndDlg, // handle to dialog box UINT uMsg, // message WPARAM wParam, // first message parameter LPARAM lParam ); // second message parameter
5.3 创建对话框
HWND CreateDialog( HINSTANCE hInstance,//应用程序实例句柄 LPCTSTR lpTemplate,//对话框的资源ID HWND hWndParent,//父窗口 DLGPROC lpDialogFunc );//对话框的窗口处理函数
返回值是创建好的对话框的窗口句柄。
5.4 显示对话框
ShowWindow
5.5 关闭对话框
EndDialog
5.6 与有模式对话框对比
创建函数: 有模式对话框 DialogBox
无模式对话框 CreateDialog
程序执行方式:
有模式对话框会阻塞,禁止其他窗口的输入
无模式对话框会让主程序,不影响其它窗口的输入
6 对话框中的消息
WM_INITDIALOG消息,用途类似于WM_CREATE消息
可以在对话框创建完成后,进行数据初始化.
它与WM_CREATE不同之处是,WM_INITDIALOG消息
会在对话框资源模板上的所有子控件(按钮等)
创建完成后,再发送给对话框窗口.
wParam - 当前接受输入焦点子控件窗口句柄
lParam - 创建时附带的参数
7 对话框和普通窗口的对比
7.1 创建方式
普通窗口: CreateWindow/Ex
对话框: DialogBox/CreateDialog
7.2 消息
普通窗口: 会收到WM_CREATE
对话框: WM_INITDIALOG
7.3 关闭方式:
普通窗口: DestoryWindow等
对话框: EndDialog
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// WinModelessDlg.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" HINSTANCE g_hInst = NULL; BOOL CALLBACK FindDlgProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_CREATE: MessageBox( NULL, "WM_CREATE", "WinDlg", MB_OK ); break; case WM_INITDIALOG: MessageBox( NULL, "WM_INITDIALOG", "WinDlg", MB_OK ); break; case WM_SYSCOMMAND: { switch( wParam ) { case SC_CLOSE: EndDialog( hWnd, 0 ); break; } } break; } return 0; } void Find( HWND hWnd ) { //创建对话框 HWND hDlg = CreateDialog( g_hInst, MAKEINTRESOURCE(IDD_FIND), hWnd, FindDlgProc ); //显示对话框 ShowWindow( hDlg, SW_SHOW ); } void OnCommand( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { int nCmdID = LOWORD( wParam ); switch( nCmdID ) { case ID_EXIT: PostQuitMessage( 0 ); break; case ID_FIND: Find( hWnd ); break; } } LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_COMMAND: OnCommand( hWnd, nMsg, wParam, lParam ); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } BOOL RegisterWnd( LPSTR pszClassName ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = HBRUSH(COLOR_WINDOW); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInst; wce.lpfnWndProc = WndProc; wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW|CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if( 0 == nAtom ) { return FALSE; } return TRUE; } HWND CreateWnd( LPSTR pszClassName ) { HMENU hMenu = LoadMenu( g_hInst, MAKEINTRESOURCE(IDR_MAIN) ); HWND hWnd = CreateWindowEx( 0, pszClassName, "MyWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, hMenu, g_hInst, NULL ); return hWnd; } void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } void Message( ) { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message( ); return 0; }
二 子控件
1 子控件
1.1 控件的类型
由Windows系统定义的窗口类型,可以提供相应的功能.包括按钮、文本编辑框、静态框等.
1.2 控件的创建
都可以使用CreateWindow/CreateWindowEx创建,并获取到控件HWND。创建时每个子控件都要有相应的ID号,ID号要求不重复。
1.3 控件的消息
子控件和父窗口(一般窗口或者对话框)的交互都是通过消息完成。如果子窗口状态发生变化,子窗口会向父窗口发送相应的消息。一般父窗口会使用SendMessage函数向子窗口发送消息。
父窗口响应子窗口发送的消息一般是在WM_COMMAND进行处理。
三 静态框
1 静态框创建
窗口类的名称 - STATIC, 用于窗口上
显示文字等, 如果使用SS_ICON/SS_BITMAP
风格可以显示ICON和BITMAP
文本静态框 - 创建时使用SS_SIMPLE即可
ICON静态框 - 创建时需要使用SS_ICON, 并且将静态框的窗口名称设置成对应ICON的资源ID号,例如"#101".
2 静态框的命令相应
2.1 静态框要添加SS_NOTIFY风格
2.2 WM_COMMAND进行处理
WPARAM - HIWORD 命令ID
LOWORD 静态框的窗口ID
LPARAM - 静态框的窗口句柄.
3 静态框的消息
调用SendMessage函数,向Static控件发送消息
例如: STM_SETICON消息修改控件显示图标
SendMessage( hStatic, STM_SETICON,
(WPARAM)hIcon, 0 );
其他:
GetDlgItem 从窗口中获取指定ID的子控件的窗口句柄
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// WinStatic.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" HINSTANCE g_hInst = NULL; void OnCreate( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { CreateWindow( "STATIC", "hello Static", WS_CHILD|WS_VISIBLE|SS_SUNKEN|SS_SIMPLE|SS_NOTIFY, 50, 50, 100, 100, hWnd, (HMENU)1001, g_hInst, NULL ); CreateWindow( "STATIC", "#101", //已经创建了一个icon图标,ID为101 WS_CHILD|WS_VISIBLE|SS_SUNKEN|SS_ICON|SS_NOTIFY, 200, 50, 100, 100, hWnd, (HMENU)1002, g_hInst, NULL ); } void OnCommand( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { int nCmdID = HIWORD( wParam ); int nCtrlID = LOWORD( wParam ); switch( nCtrlID ) { case 1001: case 1002: { switch( nCmdID ) { case STN_CLICKED: MessageBox( NULL, "Hello Static", "WinStatic", MB_OK ); break; } } break; } } LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_CREATE: OnCreate( hWnd, nMsg, wParam, lParam ); break; case WM_COMMAND: OnCommand( hWnd, nMsg, wParam, lParam ); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } BOOL RegisterWnd( LPSTR pszClassName ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = HBRUSH(COLOR_WINDOW); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInst; wce.lpfnWndProc = WndProc; wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW|CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if( 0 == nAtom ) { return FALSE; } return TRUE; } HWND CreateWnd( LPSTR pszClassName ) { HWND hWnd = CreateWindowEx( 0, pszClassName, "MyWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInst, NULL ); return hWnd; } void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } void Message( ) { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message( ); return 0; }
四 按钮
1 按钮的分类
根据创建按钮时使用的风格:
1.1 下压式按钮 PUSH BUTTON
正常下压式按钮 BS_PUSHBUTTON
三态下压按钮 BS_3STATE/BS_AUTO3STATE
1.2 分组按钮 Group Button
按钮样式:BS_GROUPBOX
1.3 多选按钮 Check Box
按钮样式:BS_CHECKBOX/BS_AUTOCHECKBOX
1.4 单选按钮 Radio Box
按钮样式:BS_RADIOBUTTON/
BS_AUTORADIOBUTTON
2 下压式按钮的使用
2.1 创建按钮
创建时使用BUTTON
2.2 命令消息
BN_CLICKED消息: 当点击按钮通过WM_COMMAND消息通知父窗口
2.3 通知消息
SendMessage
3 分组按钮
3.1 创建分组按钮
3.2 常用于对窗口内控件划分区域,增加界面的友好性
4 多选按钮 CheckBox
4.1 创建使用 BS_CHECKBOX/BS_AUTOCHECKBOX
BS_AUTOCHECKBOX 选择状态系统自动维护
BS_CHECKBOX 选择状态需要自己维护
4.2 命令响应
BN_CLICKED 通过WM_COMMAND消息
4.3 通知消息
BM_GETCHECK 获取当前按钮被选择的状态
BM_SETCHECK 设置当前按钮被选择的状态
wParam 用于设置状态标示
状态: BST_CHECKED 或 BST_UNCHECKED
5 单选按钮 RadioBox
5.1 创建使用 BS_RADIOBUTTON/BS_AUTORADIOBUTTON
BS_AUTORADIOBUTTON 选择状态系统自动维护
BS_RADIOBUTTON 选择状态需要自己维护
注意:
RadionBox分组 - 使用WS_GROUP样式,可以将
RadionBox分成多个组.
A CreateWindow ( ,,,WS_GROUP );
B CreateWindow...
C CreateWindow ( ,,,WS_GROUP );
D CreaetWindow...
A和B是一组, C和D是一组
5.2 命令响应
BN_CLICKED 通过WM_COMMAND消息
5.3 通知消息
与CheckBox类似
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// WinButton.cpp : Defines the entry point for the application. // #include "stdafx.h" HINSTANCE g_hInst = NULL; void OnCreate( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { //PUSH BUTTON CreateWindow( "BUTTON", "PushButton", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 100, 50, 100, 30, hWnd, (HMENU)1001, g_hInst, NULL ); //三态按钮 CreateWindow( "BUTTON", "Auto3State", WS_CHILD|WS_VISIBLE|BS_AUTO3STATE|BS_PUSHLIKE, 100, 100, 100, 30, hWnd, (HMENU)2001, g_hInst, NULL ); //分组按钮 CreateWindow( "BUTTON", "分组一", WS_CHILD|WS_VISIBLE|BS_GROUPBOX, 50, 30, 300, 300, hWnd, (HMENU)3001, g_hInst, NULL ); //多选按钮 CheckBox CreateWindow( "BUTTON", "选项一", WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX, 100, 150, 100, 30, hWnd, (HMENU)4001, g_hInst, NULL ); CreateWindow( "BUTTON", "选项二", WS_CHILD|WS_VISIBLE|BS_CHECKBOX, 100, 180, 100, 30, hWnd, (HMENU)4002, g_hInst, NULL ); // 单选按钮 CreateWindow( "BUTTON", "A", WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON|WS_GROUP, 100, 240, 100, 30, hWnd, (HMENU)5001, g_hInst, NULL ); CreateWindow( "BUTTON", "B", WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON, 100, 270, 100, 30, hWnd, (HMENU)5002, g_hInst, NULL ); CreateWindow( "BUTTON", "C", WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON|WS_GROUP, //遇到WS_GROUP,表示新的一组单选按钮 100, 300, 100, 30, hWnd, (HMENU)5003, g_hInst, NULL ); CreateWindow( "BUTTON", "D", WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON, 100, 330, 100, 30, hWnd, (HMENU)5004, g_hInst, NULL ); } void OnCommand( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { int nNotifyCode = HIWORD( wParam ); int nEventID = LOWORD( wParam ); switch( nEventID ) { case 1001: { switch( nNotifyCode ) { case BN_CLICKED: MessageBox( NULL, "Hello PushButton", "WinButton", MB_OK ); break; } } break; case 4002: { HWND hCtrl = GetDlgItem( hWnd, 4002 ); LRESULT nCheck = 0; switch( nNotifyCode ) { case BN_CLICKED: //获取当前CheckBox的状态 nCheck = SendMessage( hCtrl, BM_GETCHECK, 0, 0 ); //判断当前状态是否被选择 if( nCheck & BST_CHECKED ) { //设置为不选择状态 SendMessage( hCtrl,BM_SETCHECK, BST_UNCHECKED, 0 ); } else { //设置成选择状态 SendMessage( hCtrl,BM_SETCHECK, BST_CHECKED, 0 ); } break; } } break; } } LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_CREATE: OnCreate( hWnd, nMsg, wParam, lParam ); break; case WM_COMMAND: OnCommand( hWnd, nMsg, wParam, lParam ); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } BOOL RegisterWnd( LPSTR pszClassName ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = HBRUSH(COLOR_WINDOW); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInst; wce.lpfnWndProc = WndProc; wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW|CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if( 0 == nAtom ) { return FALSE; } return TRUE; } HWND CreateWnd( LPSTR pszClassName ) { HWND hWnd = CreateWindowEx( 0, pszClassName, "MyWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInst, NULL ); return hWnd; } void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } void Message( ) { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message( ); return 0; }
五 文本编辑框
1 文本编辑框的样式
1.1 单行文本编辑框 - 默认样式
1.2 多行文本编辑框 - ES_MULTILINE
1.3 密码文本编辑框 - ES_PASSWORD
2 文本编辑框的使用
2.1 创建
窗口类名称EDIT.
2.2 Notify消息
EN_CHANGE 当编辑框内容发生变化时,会通知父窗口
2.3 EDIT窗口消息
EM_SETSEL 选择消息 WPARAM是起始字符位置
LPARAM是终止字符位置
窗口消息: WM_COPY, WM_PASET,WM_CUT等消息
WM_SETTEXT, WM_GETTEXT, WM_GETTEXTLEN
SetWindowText
GetWindowText/GetWindowTextLen
一个简单记事本的程序:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// WinEdit.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "stdlib.h" #include "stdio.h" #include "resource.h" HINSTANCE g_hInst = NULL; HWND g_hEdit = NULL; void OnCreate( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { g_hEdit = CreateWindow( "EDIT", "Hello EDIT", WS_CHILD|WS_VISIBLE|WS_HSCROLL| WS_VSCROLL|ES_AUTOHSCROLL| ES_AUTOVSCROLL|ES_MULTILINE, 0, 0, 100, 200, hWnd, (HMENU)1001, g_hInst, NULL ); } void OnSize( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { int nWidth = LOWORD( lParam ); int nHeight= HIWORD( lParam ); if( NULL != g_hEdit ) { //将EDIT窗口填满整个客户区 MoveWindow( g_hEdit, 0, 0, nWidth, nHeight, TRUE ); } } void OnSave( ) { //获取文字长度 LRESULT nTextLen = SendMessage( g_hEdit, WM_GETTEXTLENGTH, 0, 0 ); //获取文字 CHAR * pszBuff = NULL; pszBuff = (CHAR *)malloc( nTextLen + 1 ); memset( pszBuff, 0, nTextLen + 1 ); SendMessage( g_hEdit, WM_GETTEXT, nTextLen + 1, (LPARAM)pszBuff ); //MessageBox( NULL, pszBuff, "WinEdit", MB_OK ); //保存文件 FILE * pFile = fopen( "C:\\1.txt", "w+" ); fwrite( pszBuff, nTextLen, 1, pFile ); fclose( pFile ); //释放内存 free( pszBuff ); } void OnOpen( ) { //打开文件读取数据 FILE * pFile = fopen( "C:\\1.txt", "r" ); //获取文件长度 fseek( pFile, 0, SEEK_END ); long nFileLen = ftell( pFile ); fseek( pFile, 0, SEEK_SET ); //读取文件数据 CHAR * pszBuf = (CHAR *) malloc( nFileLen + 1 ); memset( pszBuf, 0, nFileLen + 1 ); fread( pszBuf, nFileLen, 1, pFile ); //关闭文件 fclose( pFile ); //将字符显示在EDIT窗口 SendMessage( g_hEdit, WM_SETTEXT, 0, (LPARAM)pszBuf ); //释放内存 free( pszBuf ); } void OnCommand( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { int nNotifyCode = HIWORD( wParam ); int nEventID = LOWORD( wParam ); switch( nEventID ) { case 1001: { switch( nNotifyCode ) { case EN_CHANGE: //MessageBox( NULL, "EN_CHANGE", // "WinEdit", MB_OK ); break; } } break; case ID_SELALL: SendMessage( g_hEdit, EM_SETSEL, 0, -1 ); break; case ID_COPY: SendMessage( g_hEdit, WM_COPY, 0, 0 ); break; case ID_PASTE: SendMessage( g_hEdit, WM_PASTE, 0, 0 ); break; case ID_SAVE: OnSave( ); break; case ID_OPEN: OnOpen( ); break; } } LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_CREATE: OnCreate( hWnd, nMsg, wParam, lParam ); break; case WM_SIZE: OnSize( hWnd, nMsg, wParam, lParam ); break; case WM_COMMAND: OnCommand( hWnd, nMsg, wParam, lParam ); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } BOOL RegisterWnd( LPSTR pszClassName ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = HBRUSH(COLOR_WINDOW); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInst; wce.lpfnWndProc = WndProc; wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW|CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if( 0 == nAtom ) { return FALSE; } return TRUE; } HWND CreateWnd( LPSTR pszClassName ) { HMENU hMenu = LoadMenu( g_hInst, MAKEINTRESOURCE(IDR_MAIN) ); HWND hWnd = CreateWindowEx( WS_EX_CLIENTEDGE, pszClassName, "我的记事本", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, hMenu, g_hInst, NULL ); return hWnd; } void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } void Message( ) { MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message( ); return 0; }