云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > VC对话框中显示图片及滚动条的使用

VC对话框中显示图片及滚动条的使用

  • 62 次阅读
  • 3 次下载
  • 2025/6/4 8:01:46

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. Remarks

The framework calls this member function when the user clicks a window’s horizontal scroll bar.

The SB_THUMBTRACK scroll-bar code typically is used by applications that give some feedback while the scroll box is being dragged.

If an application scrolls the contents controlled by the scroll bar, it must also reset the position of the scroll box with the SetScrollPos member function.

Note This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function. Example

void CMyView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { // Get the minimum and maximum scroll-bar positions. int minpos; int maxpos; pScrollBar->GetScrollRange(&minpos, &maxpos); maxpos = pScrollBar->GetScrollLimit(); // Get the current position of scroll box. int curpos = pScrollBar->GetScrollPos(); // Determine the new position of scroll box. switch (nSBCode) { case SB_LEFT: // Scroll to far left. curpos = minpos; break; case SB_RIGHT: // Scroll to far right. curpos = maxpos; break; case SB_ENDSCROLL: // End scroll. break; case SB_LINELEFT: // Scroll left. if (curpos > minpos) curpos--; break; case SB_LINERIGHT: // Scroll right. if (curpos < maxpos) curpos++; break; case SB_PAGELEFT: // Scroll one page left. { // Get the page size. SCROLLINFO info; pScrollBar->GetScrollInfo(&info, SIF_ALL); if (curpos > minpos) curpos = max(minpos, curpos - (int) info.nPage); } break; case SB_PAGERIGHT: // Scroll one page right. { // Get the page size. SCROLLINFO info; pScrollBar->GetScrollInfo(&info, SIF_ALL); if (curpos < maxpos) curpos = min(maxpos, curpos + (int) info.nPage); } break; case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position curpos = nPos; // of the scroll box at the end of the drag operation. break; case SB_THUMBTRACK: // Drag scroll box to specified position. nPos is the curpos = nPos; // position that the scroll box has been dragged to. break; } // Set the new position of the thumb (scroll box). pScrollBar->SetScrollPos(curpos); CView::OnHScroll(nSBCode, nPos, pScrollBar); }

----------------------------------------------------------------------------- CWnd::OnVScroll

afx_msg void OnVScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar ); Parameters

搜索更多关于: VC对话框中显示图片及滚动条的使用 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

VC对话框中显示图片及滚动条的使用 在VC对话框程序中,图片的显示载体一般需要使用对话框控件。在此总结下在控件上绘图的几点东西: 1、在对话框资源中放置图片控件,并对其类型属性选为Frame。可在对话框的绘图消 息响应函数OnPaint或其他函数中,用CWnd类的函数GetDlgItem:CWnd* GetDlgItem( int nID ) const;来获得图片控件的窗口对象,再用函数GetDC:CDC* GetDC( );由窗口对象得到DC,然后就可以用该DC在控件中画图。 2、在对话框资源中放置非Frame类静态控件,先按顺序依次调用CWnd类的Invalidate和UpdateWindow函数后,再开始用DC画图。 注: I、为了使在运行时能够不断及时更新控件的显示(主要是自己加

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com