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

上传人:b****7 文档编号:16373559 上传时间:2023-07-13 格式:DOCX 页数:14 大小:296.96KB
下载 相关 举报
VC对话框中显示图片及滚动条的使用.docx_第1页
第1页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第2页
第2页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第3页
第3页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第4页
第4页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第5页
第5页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第6页
第6页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第7页
第7页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第8页
第8页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第9页
第9页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第10页
第10页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第11页
第11页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第12页
第12页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第13页
第13页 / 共14页
VC对话框中显示图片及滚动条的使用.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

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

《VC对话框中显示图片及滚动条的使用.docx》由会员分享,可在线阅读,更多相关《VC对话框中显示图片及滚动条的使用.docx(14页珍藏版)》请在冰点文库上搜索。

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

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(curpos

curpos++;

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(curpos

curpos=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.nPos

vSI.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.nPos

hSI.nMin:

hSI.nPos;

        }

        break;

    caseSB_LINELEFT:

        {

         hSI.nPos-=(int)((m_imageShow.Width()*m_dRatioX)/70);

            hSI.nPos =hSI.nPos

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2