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

📄 rgbdlg.cpp

📁 小型正确显示各种颜色的数值
💻 CPP
字号:
// RGBDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CRGBDlg dialog

CRGBDlg::CRGBDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRGBDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRGBDlg)
		// 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 CRGBDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRGBDlg)
	DDX_Control(pDX, IDC_RED_SCROLLBAR, m_Red_Scroll);
	DDX_Control(pDX, IDC_GREEN_SCROLLBAR, m_Green_Scroll);
	DDX_Control(pDX, IDC_BLUE_SCROLLBAR, m_Blue_Scroll);
	DDX_Control(pDX, IDC_RED_EDIT, m_Red_Edit);
	DDX_Control(pDX, IDC_GREEN_EDIT, m_Green_Edit);
	DDX_Control(pDX, IDC_BLUE_EDIT, m_Blue_Edit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRGBDlg, CDialog)
	//{{AFX_MSG_MAP(CRGBDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_VSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRGBDlg message handlers

BOOL CRGBDlg::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_Red_Scroll.SetScrollRange(0,255);//设置滚动条滚动数值范围
	m_Green_Scroll.SetScrollRange(0,255);
	m_Blue_Scroll.SetScrollRange(0,255);
    m_Red_Scroll.SetScrollPos(100);//设置滚动条滑块起始位置值
    m_Green_Scroll.SetScrollPos(100);
    m_Blue_Scroll.SetScrollPos(100);
	char sPos[10];
	itoa(m_Red_Scroll.GetScrollPos(),sPos,10);//将滚动条滑块位置值转换为字符
	m_Red_Edit.SetWindowText(sPos);//在编辑框中显示滑块位置值
	itoa(m_Green_Scroll.GetScrollPos(),sPos,10);
	m_Green_Edit.SetWindowText(sPos);
	itoa(m_Blue_Scroll.GetScrollPos(),sPos,10);
	m_Blue_Edit.SetWindowText(sPos);
	return TRUE;  // return TRUE  unless you set the focus to a control
}//初始化函数结束

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

void CRGBDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	char sPos[10];
	int iRedNowPos,iGreenNowPos,iBlueNowPos;

	iRedNowPos=m_Red_Scroll.GetScrollPos();//获取红色滚动条滑块位置
	iGreenNowPos=m_Green_Scroll.GetScrollPos();//获取绿色滚动条滑块位置
	iBlueNowPos=m_Blue_Scroll.GetScrollPos();//获取蓝色滚动条滑块位置

	if(pScrollBar==&m_Red_Scroll)//如果移动红色滚动条
	{
		switch(nSBCode)
		{
		case SB_THUMBTRACK://拖动滚动条滑块
			m_Red_Scroll.SetScrollPos(nPos);//设置滑块位置
			iRedNowPos=nPos;//得到滚动条位置值
			break;
		case SB_LINEDOWN://按向下按钮
			iRedNowPos=m_Red_Scroll.GetScrollPos();//得到当前滚动条位置
			iRedNowPos=iRedNowPos+1;//位置值+1
			if(iRedNowPos>255)//若超出上限值
				iRedNowPos=255;//则置为最大值
			break;

		case SB_LINEUP://按向上按钮
			iRedNowPos=m_Red_Scroll.GetScrollPos();
			iRedNowPos=iRedNowPos-1;//位置值-1
			if(iRedNowPos<0)//若超出下限值
				iRedNowPos=0;//则置为最小值
			break;
 
		case SB_PAGEDOWN://按向上按钮与滑块之间的位置
			iRedNowPos=m_Red_Scroll.GetScrollPos();
			iRedNowPos=iRedNowPos+10;//位置值+10
			if(iRedNowPos>255)
				iRedNowPos=255;
			break;

		case SB_PAGEUP://按向下按钮与滑块之间的位置
			iRedNowPos=m_Red_Scroll.GetScrollPos();
			iRedNowPos=iRedNowPos-10;//位置值-10
			if(iRedNowPos<0)
				iRedNowPos=0;
			break;
		}
	}

	if(pScrollBar==&m_Green_Scroll)//如果移动绿色滚动条
	{
		switch(nSBCode)
		{
		case SB_THUMBTRACK:
			m_Green_Scroll.SetScrollPos(nPos);
			iGreenNowPos=nPos;
			break;
		case SB_LINEDOWN:
			iGreenNowPos=m_Green_Scroll.GetScrollPos();
			
			iGreenNowPos=iGreenNowPos+1;
			if(iGreenNowPos>255)
				iGreenNowPos=255;
			break;

		case SB_LINEUP:
			iGreenNowPos=m_Green_Scroll.GetScrollPos();
			
			iGreenNowPos=iGreenNowPos-1;
			if(iGreenNowPos<0)
				iGreenNowPos=0;
			break;
 
		case SB_PAGEDOWN:
			iGreenNowPos=m_Green_Scroll.GetScrollPos();
			
			iGreenNowPos=iGreenNowPos+10;
			if(iGreenNowPos>255)
				iGreenNowPos=255;
			break;

		case SB_PAGEUP:
			iGreenNowPos=m_Green_Scroll.GetScrollPos();
			
			iGreenNowPos=iGreenNowPos-10;
			if(iGreenNowPos<0)
				iGreenNowPos=0;
			break;
		}
	}

	if(pScrollBar==&m_Blue_Scroll)//如果移动蓝色滚动条
	{
		switch(nSBCode)
		{
		case SB_THUMBTRACK:
			m_Blue_Scroll.SetScrollPos(nPos);
			iBlueNowPos=nPos;
			break;
		case SB_LINEDOWN:
			iBlueNowPos=m_Blue_Scroll.GetScrollPos();
			
			iBlueNowPos=iBlueNowPos+1;
			if(iBlueNowPos>255)
				iBlueNowPos=255;
			break;

		case SB_LINEUP:
			iBlueNowPos=m_Blue_Scroll.GetScrollPos();
			
			iBlueNowPos=iBlueNowPos-1;
			if(iBlueNowPos<0)
				iBlueNowPos=0;
			break;
 
		case SB_PAGEDOWN:
			iBlueNowPos=m_Blue_Scroll.GetScrollPos();
			
			iBlueNowPos=iBlueNowPos+10;
			if(iBlueNowPos>255)
				iBlueNowPos=255;
			break;

		case SB_PAGEUP:
			iBlueNowPos=m_Blue_Scroll.GetScrollPos();
			
			iBlueNowPos=iBlueNowPos-10;
			if(iBlueNowPos<0)
				iBlueNowPos=0;
			break;
		}
	}

    //设定滑块位置并在编辑框中显示位置值
    m_Red_Scroll.SetScrollPos(iRedNowPos);
	itoa(iRedNowPos,sPos,10);
	m_Red_Edit.SetWindowText(sPos);
    m_Green_Scroll.SetScrollPos(iGreenNowPos);
	itoa(iGreenNowPos,sPos,10);
	m_Green_Edit.SetWindowText(sPos);
    m_Blue_Scroll.SetScrollPos(iBlueNowPos);
	itoa(iBlueNowPos,sPos,10);
	m_Blue_Edit.SetWindowText(sPos);
    //输出所设置颜色
	CDC *pDC=GetDC();//获得设备环境
    CPen newPen;//定义画笔对象
	CBrush newBrush;//定义画刷对象
	newPen.CreatePen(PS_SOLID,1,RGB(iRedNowPos,iGreenNowPos,iBlueNowPos));//设置画笔颜色
	newBrush.CreateSolidBrush(RGB(iRedNowPos,iGreenNowPos,iBlueNowPos));//设置画刷颜色
	pDC->SelectObject(&newPen);//将画笔选入当前对象
	pDC->SelectObject(&newBrush);//将画刷选入当前对象
	pDC->Rectangle(55,50,280,180);//画出所选颜色的矩形
    ReleaseDC(pDC);//释放设备环境
	// 代码结束
	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}

⌨️ 快捷键说明

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