资源描述
几年没用vc写代码了。偶尔发现之前整理的一些知识点,删除吧又觉得可惜,就拿出来与大家分享下!若发现有神马错误的地方,请留言,不胜感激!
邮箱: yuewguo@
Win32 SDK常用API函数总结(二)
1、 获取显示器的尺寸和系统配置
int GetSystemMetrics(
int nIndex // system metric or configuration setting
);
(注:参数种类比较多,详情MSDN)
2、 为按钮添加图标
1) 载入图标使用函数,有两个函数可供选择
HICON LoadIcon(
HINSTANCE hInstance, // handle to application instance
LPCTSTR lpIconName // name string or resource identifier
);
或
HANDLE LoadImage(
HINSTANCE hinst, // handle to instance
LPCTSTR lpszName, // name or identifier of the image
UINT uType, // image type
int cxDesired, // desired width
int cyDesired, // desired height
UINT fuLoad // load options
);
2)调用下边的函数实现
SendMessage(
(HWND) hWnd, // handle to destination window
BM_SETIMAGE, // message to send
(WPARAM) wParam, // image type
(LPARAM) lParam // handle to the image (HANDLE)
);
Parameters
wParam
Specifies the type of image to associate with the button. This parameter can be one of the following values:
IMAGE_BITMAP
IMAGE_ICON
lParam
Handle to the image to associate with the button.
3、 设置指定控件(窗口)的位置、尺寸和Z序
BOOL SetWindowPos(
HWND hWnd, // handle to window
HWND hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
UINT uFlags // window-positioning options
);
(注:可以实现很多功能,例如常用的前端显示等)
4、 获取指定窗口的尺寸
BOOL GetWindowRect(
HWND hWnd, // handle to window
LPRECT lpRect // window coordinates
);
5、 滚动条
1) 使滚动条激活或者失效
BOOL EnableScrollBar(
HWND hWnd, // handle to window or scroll bar
UINT wSBflags, // scroll bar type
UINT wArrows // scroll bar arrow options
);
2) 获取滚动条当前信息(状态)
BOOL GetScrollInfo(
HWND hwnd, // handle to window
int fnBar, // scroll bar type
LPSCROLLINFO lpsi // scroll bar parameters
);
3)设置滚动条范围
BOOL SetScrollRange(
HWND hWnd, // handle to window
int nBar, // scroll bar
int nMinPos, // minimum scrolling position
int nMaxPos, // maximum scrolling position
BOOL bRedraw // redraw flag
);
4)获取滚动条的范围
BOOL GetScrollRange(
HWND hWnd, // handle to window
int nBar, // scroll bar options
LPINT lpMinPos, // receives minimum position
LPINT lpMaxPos // receives maximum position
);
5)获取滚动条当前位置
int GetScrollPos(
HWND hWnd, // handle to window
int nBar // scroll bar options
);
6)设置滚动条当前位置
int SetScrollPos(
HWND hWnd, // handle to window
int nBar, // scroll bar
int nPos, // new position of scroll box
BOOL bRedraw // redraw flag
);
6、 强制刷新窗口的方法
第1步:使指定窗口无效
BOOL InvalidateRect(
HWND hWnd, // handle to window
CONST RECT *lpRect, // rectangle coordinates
BOOL bErase // erase state
);
第2步:刷新窗口
BOOL UpdateWindow(
HWND hWnd // handle to window
);
7、 标签控件(TAB)
这个控件费了我好久才摸索出来,哎,不过总算弄出来了,虽然不知道这样做是不是正确的使用方法,下边将详细介绍使用的步骤,呵呵,仅供参考,错了可别找我算账哦。
第1步:在资源中创建一个TAB(标签)控件,以IDC_TAB为例。另外,资源中创建两个对话框,命名为IDC_DIALOG1和IDC_DIALOG2,并将Style属性都设置为Child,Border属性设置为None。
第2步:声明三个句柄hTab,hChild1,hChild2。并将hTab和 IDC_TAB相关联。代码如下:hTab = GetDlgItem(hdlg,IDC_TAB1);
第3步:添加标签项。利用的函数为: SendMessage(hTab, TCM_INSERTITEM, 0, (LPARAM)&ItemStruct);(注:ItemStruct结构体要自己填充哦,填充好了才能调用上面的函数。其中0为索引号,插入第2项时,需要把0改为1哦。)
(注:调用两次,创建两个标签项)
第4步:将 hChild1,hChild2分别和两个对话框资源IDC_DIALOG1和IDC_DIALOG相关联,将其父窗口设置为hTab,函数为: Child1hWnd = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_DIALOG2), hTab, (DLGPROC)NULL, 0);
第5步:利用MoveWindow()函数调整各窗口的位置,直到你感觉满意。
第6步:添加消息响应。在uMsg中截获WM_NOTIFY消息,消息的其他信息(消息来源于哪个标签?标签中的哪一项?和标签项改变的标志TCN_SELCHANGE)都包含在一个NMHDR类型的结构体中。LParam就是这个结构体的地址,(好难表达,不知道说明了没有)。
第7步:隐藏标签对应的对话框,利用ShowWindow(HWND ,SW_HIDE)。
显示当前选中的标签项有两种方法:
1) 第一种是利用NMHDR中的参数就可以知道要显示哪一项了,就不做解释了。
2) 使用SendMessage(hTab,TCM_GETCURSEL,0,0),函数的返回值就是对应标签项的索引。
2013.02.21 于雁联
展开阅读全文