ITPub博客

首页 > Linux操作系统 > Linux操作系统 > VC 包含按钮控制的滚动条创建

VC 包含按钮控制的滚动条创建

原创 Linux操作系统 作者:czhaii 时间:2013-11-04 05:35:47 0 删除 编辑
#include "stdafx.h"
#include "Application of ScollBar.h"
#include "Application of ScollBarDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CApplicationofScollBarDlg dialog

CApplicationofScollBarDlg::CApplicationofScollBarDlg(CWnd* pParent /*=NULL*/)
: CDialog(CApplicationofScollBarDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CApplicationofScollBarDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CApplicationofScollBarDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CApplicationofScollBarDlg)
DDX_Control(pDX, IDC_UP_BUTTON, m_up);
DDX_Control(pDX, IDC_RESET_BUTTON, m_reset);
DDX_Control(pDX, IDC_EXIT_BUTTON, m_exit);
DDX_Control(pDX, IDC_DOWN_BUTTON, m_down);
DDX_Control(pDX, IDC_EDIT1, m_Edit);
DDX_Control(pDX, IDC_SCROLLBAR, m_Scrollbar);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CApplicationofScollBarDlg, CDialog)
//{{AFX_MSG_MAP(CApplicationofScollBarDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_VSCROLL()
ON_BN_CLICKED(IDC_DOWN_BUTTON, OnDownButton)
ON_BN_CLICKED(IDC_EXIT_BUTTON, OnExitButton)
ON_BN_CLICKED(IDC_RESET_BUTTON, OnResetButton)
ON_BN_CLICKED(IDC_UP_BUTTON, OnUpButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CApplicationofScollBarDlg message handlers

BOOL CApplicationofScollBarDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_Scrollbar.SetScrollRange(0,20);     //滑块移动的范围
    m_Scrollbar.SetScrollPos(10);         //初始化位置
char sPos[10];                        //数组用于存放当前的位置
itoa(m_Scrollbar.GetScrollPos(),sPos,10);
    m_Edit.SetSel(0,-1);                  //编辑框内容全部取出
    m_Edit.ReplaceSel(sPos);              //换成sPos,更新滑块的位置
UpdateData(FALSE);                    //更新
return TRUE;  // return TRUE  unless you set the focus to a control
}

void CApplicationofScollBarDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CApplicationofScollBarDlg::OnPaint() 
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CApplicationofScollBarDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CApplicationofScollBarDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
//参数1表示滚动条发生的哪一个事件。2滚动块当前的位置。3与事件相关联的哪一个滚动块
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);  //
  char sPos[10];
     int iNowPos;
     switch(nSBCode)                           //接收scrollbar的消息
     {
         if(pScrollBar==&m_Scrollbar)         //找到相对应的scrollbar
         {
     case SB_THUMBTRACK:                       //滚动条拖动到某一位置,参数nPos指明当前位置
         m_Scrollbar.SetScrollPos(nPos);
         itoa(nPos,sPos,10);
         m_Edit.SetSel(0,-1);
         m_Edit.ReplaceSel(sPos);
     case SB_LINEDOWN:                         //向下滚动一行
         iNowPos=m_Scrollbar.GetScrollPos();
         iNowPos++;
         if(iNowPos>20)
              iNowPos=20;
         m_Scrollbar.SetScrollPos(iNowPos);
         itoa(m_Scrollbar.GetScrollPos(),sPos,10);
         m_Edit.SetSel(0,-1);
         m_Edit.ReplaceSel(sPos);
         break;
     case SB_LINEUP:                           //向上滚动一行
         iNowPos=m_Scrollbar.GetScrollPos();
         iNowPos--;
         if(iNowPos<0)
              iNowPos=0;
         m_Scrollbar.SetScrollPos(iNowPos);
         itoa(m_Scrollbar.GetScrollPos(),sPos,10);
         m_Edit.SetSel(0,-1);
         m_Edit.ReplaceSel(sPos);
         break;
     case SB_PAGEDOWN:                         //向下滚动一页
         iNowPos=m_Scrollbar.GetScrollPos();
         iNowPos+=3;
         if(iNowPos>20)
              iNowPos=20;
         m_Scrollbar.SetScrollPos(iNowPos);
         itoa(m_Scrollbar.GetScrollPos(),sPos,10);
         m_Edit.SetSel(0,-1);
         m_Edit.ReplaceSel(sPos);
         break;
     case SB_PAGEUP:                           //向上滚动一页
         iNowPos=m_Scrollbar.GetScrollPos();
         iNowPos-=3;
         if(iNowPos<0)
              iNowPos=0;
         m_Scrollbar.SetScrollPos(iNowPos);
         itoa(m_Scrollbar.GetScrollPos(),sPos,10);
         m_Edit.SetSel(0,-1);
         m_Edit.ReplaceSel(sPos);
     //   m_edit.Clear();
         break;
 }
 }
}

void CApplicationofScollBarDlg::OnDownButton() 
{
// TODO: Add your control notification handler code here
m_Scrollbar.SetScrollPos(20);//
    m_Edit.SetSel(0,-1);
    m_Edit.ReplaceSel("20");
}

void CApplicationofScollBarDlg::OnExitButton() 
{
// TODO: Add your control notification handler code here
OnOK();
}

void CApplicationofScollBarDlg::OnResetButton() 
{
// TODO: Add your control notification handler code here
m_Scrollbar.SetScrollPos(10);//
    m_Edit.SetSel(0,-1);
    m_Edit.ReplaceSel("10");
}

void CApplicationofScollBarDlg::OnUpButton() 
{
// TODO: Add your control notification handler code here
m_Scrollbar.SetScrollPos(0);//
    m_Edit.SetSel(0,-1);
    m_Edit.ReplaceSel("0");
}


滚动块.JPG

滚动条映射.JPG

成员变量.JPG

2.JPG

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27660822/viewspace-775591/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2012-08-22

  • 博文量
    22
  • 访问量
    48237