111上位机VC MFC浏览大尺寸位图
111上位机VC MFC浏览大尺寸位图
功能展示
有时浏览大尺寸图片时,由于窗口过小,不能完全显示图片,就得设置滚动条,让用户拖动水平或垂直滚动条,进行滚动浏览图片,我们当前例程实现了这一功能,效果如图 要点提示 用户对滚动条的操作为水平滚动操作,垂直滚动操作,及鼠标中键垂直滚动操作,所以我们程序中得添加对应的处理函数OnMouseWheel(),OnHScroll(), OnVScroll(),我们举例OnHScroll() 它的函数原型为afx_msg void OnHScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar ); nSBCode为用户操作滚动条时的消息代码有如下值: SB_LEFT 滚动到左边, SB_ENDSCROLL 滚动结束 , LINELEFT 单击滚动条的左边按钮, SB_LINERIGHT 单击滚动条右边按钮, SB_PAGELEFT 在滚动韦伯 左边滚动区域按下鼠标按钮; SB_PAGERIGHT 滚动到右边缘; SB_RIGHT滚动到右边, SB_THUMBPOSITION 结束拖动滚动块, SB_THUMBTRACK 正在拖动滚动块; nPos滚动块的位置标识,仅在SB_THUMBPOSITION, SB_THUMBTRACK时用; pScrollBar 滚动条的控件指针;
实现功能 1.新建基于对话框的应用程序 2.新一对话框资源,关联一对话框类如例程class CBmpDlg : public CDialog ,用于加载及显示滚动图片; 拖拽一图片控件,关联变量CStatic m_Bmp,并添加OnInitDialog(),在里面初始化CRect rect; GetClientRect(rect);m_Bmp.MoveWindow(0,0,rect.Width(),rect.Height());
添加加载位图 垂直,水平,鼠标中键处理函数LoadBmp(), OnHScroll(), OnMouseWheel(), OnVScroll(); - void CBmpDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- int pos,min,max,thumbwidth;
- SCROLLINFO vinfo;
- GetScrollInfo(SB_HORZ,&vinfo);
-
- pos = vinfo.nPos;
- min = vinfo.nMin;
- max = vinfo.nMax;
- thumbwidth = vinfo.nPage;
- switch (nSBCode)
- {
复制代码- break;
- case SB_THUMBTRACK: //拖动滚动块
- ScrollWindow(-(nPos-pos),0);
- SetScrollPos(SB_HORZ,nPos);
-
- break;
- case SB_LINELEFT : //单击左箭头
- SetScrollPos(SB_HORZ,pos-1);
- if (pos !=0)
- ScrollWindow(1,0);
- break;
- case SB_LINERIGHT: //单击右箭头
- SetScrollPos(SB_HORZ,pos+1);
- if (pos+thumbwidth <max)
- ScrollWindow(-1,0);
- break;
- case SB_PAGELEFT: //在滚动块的左方空白滚动区域单击,增量为6
- SetScrollPos(SB_HORZ,pos-6);
- if (pos+thumbwidth >0)
- ScrollWindow(6,0);
- break;
- case SB_PAGERIGHT: //在滚动块的右方空白滚动区域单击,增量为6
- SetScrollPos(SB_HORZ,pos+6);
- if (pos+thumbwidth <max)
- ScrollWindow(-6,0);
- break;
- }
- CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
- }
复制代码- BOOL CBmpDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
- {
- SCROLLINFO vinfo;
- GetScrollInfo(SB_VERT,&vinfo);
- int min,max,thumbwidth;
- min = vinfo.nMin;
- max = vinfo.nMax;
- thumbwidth = vinfo.nPage;
- int pos = GetScrollPos(SB_VERT);
- if (zDelta>0)
- {
- if (pos==0) return TRUE;
- SetScrollPos(SB_VERT,pos-6);
- ScrollWindow(0,6);
- }
- else
- {
- if ((pos+thumbwidth>=max)) return TRUE;
- SetScrollPos(SB_VERT,pos+6);
- ScrollWindow(0,-6);
- }
- return TRUE;
- void CBmpDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- int pos,min,max,thumbwidth;
- SCROLLINFO vinfo;
- GetScrollInfo(SB_VERT,&vinfo);
-
- pos = vinfo.nPos;
- min = vinfo.nMin;
- max = vinfo.nMax;
- thumbwidth = vinfo.nPage;
- switch (nSBCode)
- {
- case SB_THUMBTRACK:
- pos = GetScrollPos(SB_VERT);
- ScrollWindow(0,-(nPos-pos));
- SetScrollPos(SB_VERT,nPos);
- break;
复制代码- CString CBmpDlg::LoadBmp()
- {
- CString filename;
- CFileDialog dlg(TRUE,"","",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"位图文件(BMP)|*.bmp",this);
- if (dlg.DoModal()==IDOK)
- {
- filename = dlg.GetPathName();
- HANDLE handle = LoadImage(NULL,filename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
- m_Bmp.SetBitmap((HBITMAP)handle);
-
- CRect bmprect;
- m_Bmp.GetClientRect(bmprect);
- //
- SCROLLINFO vinfo;
- vinfo.cbSize = sizeof(vinfo);
- vinfo.fMask = SIF_ALL;
- vinfo.nPage = 100;
- vinfo.nMax= bmprect.Height();
- vinfo.nMin = 0;
- vinfo.nTrackPos = 0;
- vinfo.nPos = 0;
- SetScrollInfo(SB_VERT,&vinfo);//设置垂直滚动条信息
- //
- vinfo.fMask = SIF_ALL;
- vinfo.nPage = 10;
- vinfo.nMax= bmprect.Width();
- vinfo.nMin = 0;
- vinfo.nPos = 0;
- vinfo.nTrackPos = 0;
- vinfo.cbSize = sizeof(vinfo);
- SetScrollInfo(SB_HORZ,&vinfo);//设置水平滚动条信息
- }
- return filename;
- }
复制代码
3.然后是新建类的使用,在主对话框中添加新类的变量CBmpDlg m_BmpDlg; 再拖拽一图片控件修改ID为IDC_RECT,拖拽一按钮,关联函数void CGkbc8Dlg::OnLoad() { m_BmpDlg.LoadBmp(); } 然后在主对话框的OnInitDialog()中初始化便可m_BmpDlg.Create(IDD_DIALOG1,this);m_BmpDlg.ShowWindow(SW_SHOW);
我们来演示整个工程实现过程
|