⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 controldemodlg.cpp

📁 VC++6开发指南的源代码第7章-第11章
💻 CPP
字号:
// ControlDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ControlDemo.h"
#include "ControlDemoDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CControlDemoDlg dialog

CControlDemoDlg::CControlDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CControlDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CControlDemoDlg)
	m_radio = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CControlDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CControlDemoDlg)
	DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
	DDX_Control(pDX, IDC_SCROLLBARRED, m_CScrollR);
	DDX_Control(pDX, IDC_SCROLLBARGREEN, m_CScrollG);
	DDX_Control(pDX, IDC_SCROLLBARBLUE, m_CScrollB);
	DDX_Control(pDX, IDC_SLIDERRED, m_CSliderR);
	DDX_Control(pDX, IDC_SLIDERGREEN, m_CSliderG);
	DDX_Control(pDX, IDC_SLIDERBLUE, m_CSliderB);
	DDX_Control(pDX, IDC_SPINRED, m_spinR);
	DDX_Control(pDX, IDC_SPINGREEN, m_spinG);
	DDX_Control(pDX, IDC_SPINBLUE, m_spinB);
	DDX_Radio(pDX, IDC_RADIO1, m_radio);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CControlDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CControlDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
	ON_BN_CLICKED(IDC_CHECKIN, OnCheckin)
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CControlDemoDlg message handlers

BOOL CControlDemoDlg::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_spinR.SetRange(0,255);
  //设置微调控件的调节范围,与编辑控件数据验证的范围一致即可
	m_spinG.SetRange(0,255);
	m_spinB.SetRange(0,255);
	m_spinR.SetPos(0);  //设置其伴随窗口的初始值
	m_spinG.SetPos(0);
	m_spinB.SetPos(0);

	m_CSliderR.SetRange(0,255);  //设置滑动控件的数据范围
	m_CSliderG.SetRange(0,255);
	m_CSliderB.SetRange(0,255);
	m_CSliderR.SetPos(0);  //设置滑块的初始位置
	m_CSliderG.SetPos(0);
	m_CSliderB.SetPos(0);
	m_CSliderR.EnableWindow(false);//设置滑块控件初始无效
	m_CSliderG.EnableWindow(false);
	m_CSliderB.EnableWindow(false);
	
	m_CScrollR.SetScrollRange(0,255);//设置滚动范围
	m_CScrollG.SetScrollRange(0,255);
	m_CScrollB.SetScrollRange(0,255);  
	m_CScrollR.SetScrollPos(0);//设置滚动控件初始值
	m_CScrollG.SetScrollPos(0);
	m_CScrollB.SetScrollPos(0);  
	m_CScrollR.EnableWindow(false);//设置滚动控件初始无效
	m_CScrollG.EnableWindow(false);//设置滚动控件初始无效
	m_CScrollB.EnableWindow(false);//设置滚动控件初始无效

	m_Progress.SetRange(0,255*3);//设置进度条的范围




	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CControlDemoDlg::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 CControlDemoDlg::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 CControlDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



void CControlDemoDlg::OnRadio1() 
{
	// TODO: Add your control notification handler code here
	m_radio = 0;
	m_CSliderR.EnableWindow(false);//设置滑块控件初始无效
	m_CSliderG.EnableWindow(false);
	m_CSliderB.EnableWindow(false);
	m_CScrollR.EnableWindow(false);//设置滚动控件初始无效
	m_CScrollG.EnableWindow(false);
	m_CScrollB.EnableWindow(false);
	GetDlgItem(IDC_EDITRED)->EnableWindow(true);//微调按钮生效
	GetDlgItem(IDC_EDITGREEN)->EnableWindow(true);//微调按钮生效
	GetDlgItem(IDC_EDITBLUE)->EnableWindow(true);//微调按钮生效	
}

void CControlDemoDlg::OnRadio2() 
{
	// TODO: Add your control notification handler code here
	m_radio = 1;
	m_CSliderR.EnableWindow(true);//设置滑块控件初始生效
	m_CSliderG.EnableWindow(true);
	m_CSliderB.EnableWindow(true);
	m_CScrollR.EnableWindow(false);//设置滚动控件初始无效
	m_CScrollG.EnableWindow(false);
	m_CScrollB.EnableWindow(false);
	GetDlgItem(IDC_EDITRED)->EnableWindow(false);//微调按钮无效
	GetDlgItem(IDC_EDITGREEN)->EnableWindow(false);
	GetDlgItem(IDC_EDITBLUE)->EnableWindow(false);	
}

void CControlDemoDlg::OnRadio3() 
{
	// TODO: Add your control notification handler code here
	m_radio = 2;
	m_CScrollR.EnableWindow(true);//设置滚动控件初始生效
	m_CScrollG.EnableWindow(true);
	m_CScrollB.EnableWindow(true);
	m_CSliderR.EnableWindow(false);//设置滑块控件初始无效
	m_CSliderG.EnableWindow(false);
	m_CSliderB.EnableWindow(false);
	GetDlgItem(IDC_EDITRED)->EnableWindow(false);//微调按钮无效
	GetDlgItem(IDC_EDITGREEN)->EnableWindow(false);
	GetDlgItem(IDC_EDITBLUE)->EnableWindow(false);
	
}

void CControlDemoDlg::OnCheckin() 
{
	// TODO: Add your control notification handler code here
	int nRed,nGreen,nBlue;
	switch(m_radio)
	{
		case 0:											//第一个单选项被选中
			nRed=m_spinR.GetPos();//获取微调控件的值
			nGreen=m_spinG.GetPos();
			nBlue=m_spinB.GetPos();
			m_CSliderR.SetPos(nRed);//设置滑块控件的值
			m_CSliderG.SetPos(nGreen);
			m_CSliderB.SetPos(nBlue);
			m_CScrollR.SetScrollPos(nRed);//设置滚动条的值
			m_CScrollG.SetScrollPos(nGreen);
			m_CScrollB.SetScrollPos(nBlue);
			m_Progress.SetPos(nRed+nGreen+nBlue);//设置进度条的值
		
			
			break;
		case 1:											//第二个单选项被选中
			nRed=m_CSliderR.GetPos();//获取滑块控件的值
			nGreen=m_CSliderG.GetPos();
			nBlue=m_CSliderB.GetPos();
			m_spinR.SetPos(nRed);//设置微调控件的值
			m_spinG.SetPos(nGreen);
			m_spinB.SetPos(nBlue);
			m_CScrollR.SetScrollPos(nRed);//设置滚动条的值
			m_CScrollG.SetScrollPos(nGreen);
			m_CScrollB.SetScrollPos(nBlue);
			m_Progress.SetPos(nRed+nGreen+nBlue);//设置进度条的值			
			break;
		case 2:										//第三个单选项被选中
			nRed=m_CScrollR.GetScrollPos();//获取滚动条的值
			nGreen=m_CScrollG.GetScrollPos();
			nBlue=m_CScrollB.GetScrollPos();
			m_spinR.SetPos(nRed);//设置微调控件的值
			m_spinG.SetPos(nGreen);
			m_spinB.SetPos(nBlue);
			m_CSliderR.SetPos(nRed);//设置滑块控件的值
			m_CSliderG.SetPos(nGreen);
			m_CSliderB.SetPos(nBlue);
			m_Progress.SetPos(nRed+nGreen+nBlue);//设置进度条的值		
			break;
	}
	CString str;
	str.Format("RGB(%d,%d,%d)",nRed,nGreen,nBlue);
	GetDlgItem(IDC_EDITCOLOR)->SetWindowText(str);//显示选择的颜色值	
}

void CControlDemoDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	int pos=pScrollBar->GetScrollPos();
  //GetScrollPos()函数得到滚动块的前一位置
	switch(nSBCode)  //判断对滚动条的操作
	{
	case SB_LINERIGHT: pos+=1;  //单击滚动条的右箭头
		break;
	case SB_LINELEFT: pos-=10;  //单击滚动条的左箭头
		break;
	case SB_PAGERIGHT: pos+=10;  //单击滚动块右侧的滚动框
		break;
	case SB_PAGELEFT: pos-=10;  //单击滚动块左侧的滚动框
		break;
	case SB_THUMBTRACK: pos=nPos;  //拖动滚动块
	default: break;
	}
	pScrollBar->SetScrollPos(pos);  //重新设置滚动块的位置
	
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -