资源描述
几年没用vc写代码了。偶尔发现之前整理的一些知识点,删除吧又觉得可惜,就拿出来与大家分享下!若发现有神马错误的地方,请留言,不胜感激!
邮箱: yuewguo@
Win32 SDK常用API函数总结(一)
1、 创建模态对话框:
int DialogBox( HINSTANCE hInstance, LPCTSTR lpTemplate,
HWND hWndParent, DLGPROC lpDialogFunc );
(注:IDD_MAIN为资源ID,MainDlgProc为回调函数,详细参数参考MSDN)
2、 对话框常用消息:(不全面,仅供参考)
1) 对话框显示时,会触发一个WM_INITDIALOG消息。
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_INITDIALOG
WPARAM wParam, // handle to control (HWND)
LPARAM lParam // initialization parameter
);
2) 对话框上的滚动条或滑动条控件等被操作时,触发一个WM_HSCROLL消息。
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_HSCROLL
WPARAM wParam, // request and position
LPARAM lParam // handle to scroll bar (HWND)
);
3) 对话框上的按钮、组合框、列表框等被操作时,触发出一个WM_COMMAND消息。LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_COMMAND
WPARAM wParam, // notification code and identifier
LPARAM lParam // handle to control (HWND)
);
wParam
The high-order word specifies the notification code if the message is from a ontrol. If the message is from an accelerator, this value is 1. If the message is from a menu, this value is zero.
The low-order word specifies the identifier of the menu item, control, or accelerator.
lParam
Handle to the control sending the message if the message is from a control. Otherwise, this parameter is NULL.
4) 点击最大化或最小化,会触发一个WM_SIZE消息。
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_SIZE
WPARAM wParam, // resizing flag
LPARAM lParam // client area
);
wParam
Specifies the type of resizing requested. This parameter can be one of the following values.
Value
Meaning
SIZE_MAXHIDE
Message is sent to all pop-up windows when some other window is maximized.
SIZE_MAXIMIZED
The window has been maximized.
SIZE_MAXSHOW
Message is sent to all pop-up windows when some other window has been restored to its former size.
SIZE_MINIMIZED
The window has been minimized.
SIZE_RESTORED
The window has been resized, but neither the SIZE_MINIMIZED nor SIZE_MAXIMIZED value applies.
lParam
The low-order word of lParam specifies the new width of the client area.
The high-order word of lParam specifies the new height of the client area.
5) 点击关闭,触发一个WM_CLOSE:消息
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_CLOSE
WPARAM wParam, // not used
LPARAM lParam // not used
);
Parameters
This message has no parameters.
3、 常用的API:(不全面,仅供参考)
(1)对话框
1) 创建对话框:
INT_PTR DialogBox(
HINSTANCE hInstance, // handle to module
LPCTSTR lpTemplate, // dialog box template
HWND hWndParent, // handle to owner window
DLGPROC lpDialogFunc // dialog box procedure
);
2) 关闭对话框:
BOOL EndDialog(
HWND hDlg, // handle to dialog box
INT_PTR nResult // value to return
);
3) 获取对话框上控件的句柄:
HWND GetDlgItem(
HWND hDlg, // handle to dialog box
int nIDDlgItem // control identifier
);
4) 修改对话框的图标:
SendMessage(
(HWND) hWnd, // handle to destination window
WM_SETICON, // message to send
(WPARAM) wParam, // icon type
(LPARAM) lParam // handle to icon (HICON)
);
(2)组合框
1) 为组合框控件增加项:
SendMessage(
(HWND) hWnd, // handle to destination window
CB_ADDSTRING, // message to send
(WPARAM) wParam, // not used; must be zero
(LPARAM) lParam // string to add (LPCTSTR)
);
2) 设置组合框的当前选项:
SendMessage(
(HWND) hWnd, // handle to destination window
CB_SETCURSEL, // message to send
(WPARAM) wParam, // item index
(LPARAM) lParam // not used; must be zero
);
(注:建议组合框控件属性去掉sort ,依次添加组合框的项,那么索引号就为0,1,2,3….,这样便于控制)
3) 获得当前选中项的索引值:(返回值即为索引值)
SendMessage(
(HWND) hWnd, // handle to destination window
CB_GETCURSEL, // message to send
0,
0
);
(3)静态文本
1) 设置静态文本:BOOL SetWindowText(
HWND hWnd, // handle to window or control
LPCTSTR lpString // title or text
);
2)获取静态文本:int GetWindowText(
HWND hWnd, // handle to window or control
LPTSTR lpString, // text buffer
int nMaxCount // maximum number of characters to copy
);
4、 滑动条
1)设置范围:
SendMessage(
(HWND) hWnd, // handle to destination window
TBM_SETRANGE, // message to send
(WPARAM) TRUE,
(LPARAM) MAKELONG(lMinimum, lMaximum)
);
2) 设置当前值:
SendMessage(
(HWND) hWnd,
TBM_SETPOS,
(WPARAM) TRUE,
(LPARAM) Value
);
3) 获得当前值:(返回值为一个32位数,就是当前位置的值)
SendMessage(
(HWND) hWnd,
TBM_GETPOS,
0,
0
);
5、 窗口
1) 使窗口无效(有效):
BOOL EnableWindow(
HWND hWnd, // handle to window
BOOL bEnable // enable or disable input
);
6、 常用对话框
注:对于保存文件对话框、打开文件对话框、颜色对话框等网上资料较多,不做介绍。
1) 文件路径对话框
BOOL SHGetPathFromIDList(
LPCITEMIDLIST pidl,
LPSTR pszPath
);
注:其他类似的函数,可以参照MSDN以SH开头的函数
2013.2.21 于雁联
展开阅读全文