Scroll bar controls do with vc ++

  Generating a first guide with the application of scro based application dialog, the dialog editor reuse added as two horizontal scroll bar, the two scroll bars id were taken idc-scr1 and IDC-SCR2 , results as shown below , which as an example herein.

  While the dialog editor allows you to add a scroll bar control box, and the class data members allowed to join the wizard, but to make both the horizontal scroll bar work, we must also add some code. For this example program, when the user drags or thumb, a scroll bar with the mouse will be sent to the scroll box arrows WM_HSCROLL message, the control message box function must process the message, and then scroll to the appropriate positioning of the block position.
  In general, each control in the dialog box has its own independent message control function, but the scroll bar control is a bit different, because all of the dialog box, the horizontal scroll bar is only one WM_HSCROLL message control function, and all of the vertical scroll Article only one WM_HSCROLL message control function. If the dialog is only a horizontal (or vertical) scroll bar without any problems, the problem is a special about the present embodiment procedures, the author intends to set up two horizontal scroll bar, the both may use a WM_HSCROLL Message Control function, the program must be able to recognize which message transmission scrollbar. Here are the steps.

1. The definition of maximum and minimum values of the scrolling range.
  In scrodlg.h top class declaration by adding the following two lines.

  enum {nmin=0};
  enum {nmax=100};

2. Modify oninitdialog function, initial scrolling range, the decision to send a message that a scroll bar.

// todo: add extra initialization here
  cscrollbar* psb = (cscrollbar*) getdlgitem(idc—scr1);
  psb-〉setscrollrange(nmin, nmax);
  psb = (cscrollbar*) getdlgitem(idc_scr2);
  psb-〉setscrollrange(nmin, nmax);

3. use classwizard in cscrodlg addition of the scroll bar in the message control function, i.e. choice wm-hscroll message, then add onhscroll member functions, and adding the following code:

  void cscrodlg::onhscroll(uint nsbcode, uint npos, cscrollbar* pscrollbar) 
  {// todo: add your message handler code here and/or call default
    int ntemp1, ntemp2;
    ntemp1 = pscrollbar-〉getscrollpos();
    switch(nsbcode) 
    { 
      case sb—thumbposition:          pscrollbar-〉setscrollpos(npos);          break;       case sb—lineleft: // 左按钮          ntemp2 = (nmax - nmin) / 10; //划为10等份          if ((ntemp1 - ntemp2) 〉 nmin)
         {
            ntemp1 -= ntemp2;
         }          
else
         {
            ntemp1 = nmin;
         }          pscrollbar-〉setscrollpos(ntemp1);          
break;       case sb—lineright: // 右箭头按钮          ntemp2 = (nmax - nmin) / 10;          if ((ntemp1 + ntemp2) 〈 nmax)
         {
             ntemp1 += ntemp2;
         }          
else
         {
             ntemp1 = nmax;
         }          pscrollbar-〉setscrollpos(ntemp1);          
break;
    }     cdialog::onhscroll(nsbcode, npos, pscrollbar);}

4. You can now compile, test the program.

Reproduced in: https: //my.oschina.net/u/204616/blog/544978

Guess you like

Origin blog.csdn.net/weixin_34149796/article/details/91990163