VC对话框中显示图片及滚动条的使用解析.doc
文本预览下载声明
VC对话框中显示图片及滚动条的使用
对话框控件显示图片
在VC对话框程序中,图片的显示载体一般需要使用对话框控件。在此总结下在控件上绘图的几点东西:
1、在对话框资源中放置图片控件,并对其类型属性选为Frame。可在对话框的绘图消 息响应函数OnPaint或其他函数中,用CWnd类的函数GetDlgItem:CWnd* GetDlgItem( int nID ) const;来获得图片控件的窗口对象,再用函数GetDC:CDC* GetDC( );由窗口对象得到DC,然后就可以用该DC在控件中画图。
2、在对话框资源中放置非Frame类静态控件,先按顺序依次调用CWnd类的Invalidate和UpdateWindow函数后,再开始用DC画图。
注:
I、为了使在运行时能够不断及时更新控件的显示(主要是自己加的显式代码),可以将自己绘制控件的所有 代码都全部加入对话框类的消息响应函数OnPaint中。在需要时(例如在绘图参数修改后),自己调用CWnd的Invalidate和 UpdateWindow函数,请求系统刷新对话框和控件的显示。因为控件也是窗口,控件类都是CWnd的派生类。所以在对话框和控件中,可以像在视图类 中一样,调用各种CWnd的成员函数;
II、为了在鼠标指向控件时,避免控件自己绘制的图形消去,需要设置控件的“Owner Draw”属性为“True”;
III、如果希望非按钮控件(如图片控件和静态文本等),也可以响应鼠标消息(如单击、双击等),需要设置控件的“Notify”属性为“True”;
?
使用对话框滚动条浏览控件上的图片
当图片显示范围超过控件大小时,我们需要使用滚动图片以便浏览超出图片的其他部分。对话框滚动条控件包括水平滚动条和垂直滚动条,使用步骤:
1、像对话框添加两种控件资源,分别添加实例变量;
2、添加控件的响应消息函数CWnd::OnVScroll 和CWnd::OnHScroll,完成控件的滚动消息响应;
3、根据滚动位置更新图片的显示区域;
注:
CWnd::OnHScroll?
afx_msg void OnHScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar );
Parameters
nSBCode
Specifies a scroll-bar code that indicates the user’s scrolling request. This parameter can be one of the following:
SB_LEFT?? Scroll to far left.
SB_ENDSCROLL?? End scroll.
SB_LINELEFT?? Scroll left.
SB_LINERIGHT?? Scroll right.
SB_PAGELEFT?? Scroll one page left.
SB_PAGERIGHT?? Scroll one page right.
SB_RIGHT?? Scroll to far right.
SB_THUMBPOSITION?? Scroll to absolute position. The current position is specified by the nPos parameter.
SB_THUMBTRACK?? Drag scroll box to specified position. The current position is specified by the nPos parameter.
nPos
Specifies the scroll-box position if the scroll-bar code is SB_THUMBPOSITION or SB_THUMBTRACK; otherwise, not used. Depending on the initial scroll range, nPos may be negative and should be cast to an int if necessary.
pScrollBar
If the scroll message came from a scroll-bar control, contains a pointer to the control. If the user clicked a window’s scroll bar, this parameter is NULL. The pointer may be temporary and should not be stored for later use.
显示全部