VC对话框中显示图片及滚动条的使用.docx
《VC对话框中显示图片及滚动条的使用.docx》由会员分享,可在线阅读,更多相关《VC对话框中显示图片及滚动条的使用.docx(14页珍藏版)》请在冰点文库上搜索。
VC对话框中显示图片及滚动条的使用
VC对话框中显示图片及滚动条的使用
<对话框控件显示图片>
在VC对话框程序中,图片的显示载体一般需要使用对话框控件。
在此总结下在控件上绘图的几点东西:
1、在对话框资源中放置图片控件,并对其类型属性选为Frame。
可在对话框的绘图消息响应函数OnPaint或其他函数中,用CWnd类的函数GetDlgItem:
CWnd*GetDlgItem(intnID)const;来获得图片控件的窗口对象,再用函数GetDC:
CDC*GetDC();由窗口对象得到DC,然后就可以用该DC在控件中画图。
2、在对话框资源中放置非Frame类静态控件,先按顺序依次调用CWnd类的Invalidate和UpdateWindow函数后,再开始用DC画图。
注:
I、为了使在运行时能够不断及时更新控件的显示(主要是自己加的显式代码),可以将自己绘制控件的所有代码都全部加入对话框类的消息响应函数OnPaint中。
在需要时(例如在绘图参数修改后),自己调用CWnd的Invalidate和UpdateWindow函数,请求系统刷新对话框和控件的显示。
因为控件也是窗口,控件类都是CWnd的派生类。
所以在对话框和控件中,可以像在视图类中一样,调用各种CWnd的成员函数;
II、为了在鼠标指向控件时,避免控件自己绘制的图形消去,需要设置控件的“OwnerDraw”属性为“True”;
III、如果希望非按钮控件(如图片控件和静态文本等),也可以响应鼠标消息(如单击、双击等),需要设置控件的“Notify”属性为“True”;
<使用对话框滚动条浏览控件上的图片>
当图片显示范围超过控件大小时,我们需要使用滚动图片以便浏览超出图片的其他部分。
对话框滚动条控件包括水平滚动条和垂直滚动条,使用步骤:
1、像对话框添加两种控件资源,分别添加实例变量;
2、添加控件的响应消息函数CWnd:
:
OnVScroll和CWnd:
:
OnHScroll,完成控件的滚动消息响应;
3、根据滚动位置更新图片的显示区域;
注:
---------------------------------------------------------------------------------------
CWnd:
:
OnHScroll
afx_msgvoidOnHScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar);
Parameters
nSBCode
Specifiesascroll-barcodethatindicatestheuser’sscrollingrequest.Thisparametercanbeoneofthefollowing:
∙SB_LEFT Scrolltofarleft.
∙SB_ENDSCROLL Endscroll.
∙SB_LINELEFT Scrollleft.
∙SB_LINERIGHT Scrollright.
∙SB_PAGELEFT Scrollonepageleft.
∙SB_PAGERIGHT Scrollonepageright.
∙SB_RIGHT Scrolltofarright.
∙SB_THUMBPOSITION Scrolltoabsoluteposition.ThecurrentpositionisspecifiedbythenPosparameter.
∙SB_THUMBTRACK Dragscrollboxtospecifiedposition.ThecurrentpositionisspecifiedbythenPosparameter.
nPos
Specifiesthescroll-boxpositionifthescroll-barcodeisSB_THUMBPOSITIONorSB_THUMBTRACK;otherwise,notused.Dependingontheinitialscrollrange,nPosmaybenegativeandshouldbecasttoanintifnecessary.
pScrollBar
Ifthescrollmessagecamefromascroll-barcontrol,containsapointertothecontrol.Iftheuserclickedawindow’sscrollbar,thisparameterisNULL.Thepointermaybetemporaryandshouldnotbestoredforlateruse.
Remarks
Theframeworkcallsthismemberfunctionwhentheuserclicksawindow’shorizontalscrollbar.
TheSB_THUMBTRACKscroll-barcodetypicallyisusedbyapplicationsthatgivesomefeedbackwhilethescrollboxisbeingdragged.
Ifanapplicationscrollsthecontentscontrolledbythescrollbar,itmustalsoresetthepositionofthescrollboxwiththeSetScrollPosmemberfunction.
Note ThismemberfunctioniscalledbytheframeworktoallowyourapplicationtohandleaWindowsmessage.Theparameterspassedtoyourfunctionreflecttheparametersreceivedbytheframeworkwhenthemessagewasreceived.Ifyoucallthebase-classimplementationofthisfunction,thatimplementationwillusetheparametersoriginallypassedwiththemessageandnottheparametersyousupplytothefunction.
Example
voidCMyView:
:
OnHScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar)
{
//Gettheminimumandmaximumscroll-barpositions.
intminpos;
intmaxpos;
pScrollBar->GetScrollRange(&minpos,&maxpos);
maxpos=pScrollBar->GetScrollLimit();
//Getthecurrentpositionofscrollbox.
intcurpos=pScrollBar->GetScrollPos();
//Determinethenewpositionofscrollbox.
switch(nSBCode)
{
caseSB_LEFT:
//Scrolltofarleft.
curpos=minpos;
break;
caseSB_RIGHT:
//Scrolltofarright.
curpos=maxpos;
break;
caseSB_ENDSCROLL:
//Endscroll.
break;
caseSB_LINELEFT:
//Scrollleft.
if(curpos>minpos)
curpos--;
break;
caseSB_LINERIGHT:
//Scrollright.
if(curposcurpos++;
break;
caseSB_PAGELEFT:
//Scrollonepageleft.
{
//Getthepagesize.
SCROLLINFOinfo;
pScrollBar->GetScrollInfo(&info,SIF_ALL);
if(curpos>minpos)
curpos=max(minpos,curpos-(int)info.nPage);
}
break;
caseSB_PAGERIGHT:
//Scrollonepageright.
{
//Getthepagesize.
SCROLLINFOinfo;
pScrollBar->GetScrollInfo(&info,SIF_ALL);
if(curposcurpos=min(maxpos,curpos+(int)info.nPage);
}
break;
caseSB_THUMBPOSITION:
//Scrolltoabsoluteposition.nPosistheposition
curpos=nPos; //ofthescrollboxattheendofthedragoperation.
break;
caseSB_THUMBTRACK:
//Dragscrollboxtospecifiedposition.nPosisthe
curpos=nPos; //positionthatthescrollboxhasbeendraggedto.
break;
}
//Setthenewpositionofthethumb(scrollbox).
pScrollBar->SetScrollPos(curpos);
CView:
:
OnHScroll(nSBCode,nPos,pScrollBar);
}
-----------------------------------------------------------------------------
CWnd:
:
OnVScroll
afx_msgvoidOnVScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar);
Parameters
nSBCode
Specifiesascroll-barcodethatindicatestheuser’sscrollingrequest.Thisparametercanbeoneofthefollowing:
∙SB_BOTTOM Scrolltobottom.
∙SB_ENDSCROLL Endscroll.
∙SB_LINEDOWN Scrollonelinedown.
∙SB_LINEUP Scrollonelineup.
∙SB_PAGEDOWN Scrollonepagedown.
∙SB_PAGEUP Scrollonepageup.
∙SB_THUMBPOSITION Scrolltotheabsoluteposition.ThecurrentpositionisprovidedinnPos.
∙SB_THUMBTRACK Dragscrollboxtospecifiedposition.ThecurrentpositionisprovidedinnPos.
∙SB_TOP Scrolltotop.
nPos
Containsthecurrentscroll-boxpositionifthescroll-barcodeisSB_THUMBPOSITIONorSB_THUMBTRACK;otherwisenotused.Dependingontheinitialscrollrange,nPosmaybenegativeandshouldbecasttoanintifnecessary.
pScrollBar
Ifthescrollmessagecamefromascroll-barcontrol,containsapointertothecontrol.Iftheuserclickedawindow’sscrollbar,thisparameterisNULL.Thepointermaybetemporaryandshouldnotbestoredforlateruse.
Remarks
Theframeworkcallsthismemberfunctionwhentheuserclicksthewindow’sverticalscrollbar.
OnVScrolltypicallyisusedbyapplicationsthatgivesomefeedbackwhilethescrollboxisbeingdragged.
IfOnVScrollscrollsthecontentsoftheCWndobject,itmustalsoresetthepositionofthescrollboxwiththeSetScrollPosmemberfunction.
Note ThismemberfunctioniscalledbytheframeworktoallowyourapplicationtohandleaWindowsmessage.Theparameterspassedtoyourfunctionreflecttheparametersreceivedbytheframeworkwhenthemessagewasreceived.Ifyoucallthebase-classimplementationofthisfunction,thatimplementationwillusetheparametersoriginallypassedwiththemessageandnottheparametersyousupplytothefunction.
<程序示例>
滚动消息响应:
根据图片的显示大小设置滚动条的滚动步长信息
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
voidCImageboxUI:
:
OnVScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar)
{
SCROLLINFOvSI;
m_vScrollBar.GetScrollInfo(&vSI);
switch(nSBCode)
{
caseSB_TOP:
vSI.nPos=0;
break;
caseSB_BOTTOM:
vSI.nPos=INT_MAX;
break;
caseSB_LINEUP:
{
vSI.nPos-=(int)((m_imageShow.Height()*m_dRatioY)/70);
vSI.nPos =vSI.nPosvSI.nMin:
vSI.nPos;
}
break;
caseSB_LINEDOWN:
{
vSI.nPos+=(int)((m_imageShow.Height()*m_dRatioY)/70);
vSI.nPos =vSI.nPos>vSI.nMax?
vSI.nMax:
vSI.nPos;
}
break;
caseSB_PAGEUP:
break;
caseSB_PAGEDOWN:
break;
caseSB_THUMBPOSITION:
break;
caseSB_THUMBTRACK:
vSI.nPos=nPos;
break;
caseSB_ENDSCROLL:
break;
}
m_vScrollBar.SetScrollInfo(&vSI);
SendMessage(WM_PAINT);
CDialog:
:
OnVScroll(nSBCode,nPos,pScrollBar);
}
voidCImageboxUI:
:
OnHScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar)
{
SCROLLINFOhSI;
m_hScrollBar.GetScrollInfo(&hSI);
switch(nSBCode)
{
caseSB_LEFT:
hSI.nPos=0;
break;
caseSB_RIGHT:
hSI.nPos=INT_MAX;
break;
caseSB_LINERIGHT:
{
hSI.nPos+=(int)((m_imageShow.Width()*m_dRatioX)/70);
hSI.nPos =hSI.nPoshSI.nMin:
hSI.nPos;
}
break;
caseSB_LINELEFT:
{
hSI.nPos-=(int)((m_imageShow.Width()*m_dRatioX)/70);
hSI.nPos =hSI.nPoshSI.nMin:
hSI.nPos;
}
break;
caseSB_PAGEUP:
break;
caseSB_PAGEDOWN:
break;
caseSB_THUMBPOSITION:
break;
caseSB_THUMBTRACK:
hSI.nPos=nPos;
break;
caseSB_ENDSCROLL:
break;
}
m_hScrollBar.SetScrollInfo(&hSI);
SendMessage(WM_PAINT);
CDialog:
:
OnHScroll(nSBCode,nPos,pScrollBar);
}
?
1
根据滚动信息进行图片绘制(双缓冲机制):
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
voidCImageboxUI:
:
OnPaint()
{
CPaintDCdc(this);//devicecontextforpainting
//TODO:
在此处添加消息处理程序代码
//不为绘图消息调用CDialog:
:
OnPaint()
//////////////////////////////////////////////////////////////////////////
//对话框Static控件双缓冲绘图
CWnd*pImageWnd=GetDlgItem(IDC_STATIC_PIC);
if(NULL==pImageWnd)return;
pImageWnd->ModifyStyle(0,BS_OWNERDRAW); //修改CStatic样式,改成自绘制(图形不被消去)
//绘图控件的DC
CDC* pDC=pImageWnd->GetDC();
if(NULL==pDC)return;
//绘图背景区域大小
pImageWnd->GetWindowRect(&m_backShow