📄 颜色控制dlg.cpp
字号:
// 颜色控制Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "颜色控制.h"
#include "颜色控制Dlg.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()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Control(pDX, IDC_RED_SCROLLBAR, m_Red_ScrollBar);
DDX_Control(pDX, IDC_RED_EDIT, m_Red_Edit);
DDX_Control(pDX, IDC_GREEN_SCROLLBAR, m_Green_ScrollBar);
DDX_Control(pDX, IDC_GREEN_EDIT, m_Green_Edit);
DDX_Control(pDX, IDC_BLUE_SCROLLBAR, m_Blue_ScrollBar);
DDX_Control(pDX, IDC_BLUE_EDIT, m_Blue_Edit);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_VSCROLL()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::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_ScrollBar.SetScrollRange(0,255);//设定滚动条的范围
m_Green_ScrollBar.SetScrollRange(0,255);
m_Blue_ScrollBar.SetScrollRange(0,255);
m_Red_ScrollBar.SetScrollPos(100);//设定滚动条的当前位置
m_Green_ScrollBar.SetScrollPos(100);
m_Blue_ScrollBar.SetScrollPos(100);
char sPos[10];//将当前滚动条的位置输出
itoa(m_Red_ScrollBar.GetScrollPos(),sPos,10);//当前位置位置为100
m_Red_Edit.SetWindowText(sPos);
itoa(m_Green_ScrollBar.GetScrollPos(),sPos,10);
m_Green_Edit.SetWindowText(sPos);
itoa(m_Blue_ScrollBar.GetScrollPos(),sPos,10);
m_Blue_Edit.SetWindowText(sPos);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
char sPos[10];//记录滚动条位置的字符数组
int iRedNowPos,iBlueNowPos,iGreenNowPos;//记录滚动条的整形数
iRedNowPos=m_Red_ScrollBar.GetScrollPos();//获得滚动条当前位置
iGreenNowPos=m_Green_ScrollBar.GetScrollPos();
iBlueNowPos=m_Blue_ScrollBar.GetScrollPos();
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
if(pScrollBar==&m_Red_ScrollBar)//对红色滚动条进行操作
{switch(nSBCode)
{case SB_THUMBTRACK://拖动滚动条
m_Red_ScrollBar.SetScrollPos(nPos);//设定滚动条位置
iRedNowPos=nPos;
break;
case SB_LINEDOWN://按向下按钮
iRedNowPos=m_Red_ScrollBar.GetScrollPos();//得到当前滚动条位置
iRedNowPos=iRedNowPos+1;//位置量+1
if(iRedNowPos>255)
iRedNowPos=255;
break;
case SB_LINEUP://按向上按钮
iRedNowPos=m_Red_ScrollBar.GetScrollPos();//得到当前滚动条位置
iRedNowPos=iRedNowPos-1;//位置量-1
if(iRedNowPos<0)
iRedNowPos=0;
break;
case SB_PAGEDOWN:
iRedNowPos=m_Red_ScrollBar.GetScrollPos();
iRedNowPos=iRedNowPos+10;
if(iRedNowPos>255)
iRedNowPos=255;
break;
case SB_PAGEUP:
iRedNowPos=m_Red_ScrollBar.GetScrollPos();
iRedNowPos=iRedNowPos-10;
if(iRedNowPos<0)
iRedNowPos=0;
break;
}}
if(pScrollBar==&m_Green_ScrollBar)
{switch(nSBCode)
{case SB_THUMBTRACK:
m_Green_ScrollBar.SetScrollPos(nPos);
iGreenNowPos=nPos;
break;
case SB_LINEDOWN:
iGreenNowPos=m_Green_ScrollBar.GetScrollPos();
iGreenNowPos=iGreenNowPos+1;
if(iGreenNowPos>255)
iGreenNowPos=255;
break;
case SB_LINEUP:
iGreenNowPos=m_Green_ScrollBar.GetScrollPos();
iGreenNowPos=iGreenNowPos-1;
if(iGreenNowPos<0)
iGreenNowPos=0;
break;
case SB_PAGEDOWN:
iGreenNowPos=m_Green_ScrollBar.GetScrollPos();
iGreenNowPos=iGreenNowPos+10;
if(iGreenNowPos>255)
iGreenNowPos=255;
break;
case SB_PAGEUP:
iGreenNowPos=m_Green_ScrollBar.GetScrollPos();
iGreenNowPos=iGreenNowPos-10;
if(iGreenNowPos<0)
iGreenNowPos=0;
break;
}}
if(pScrollBar==&m_Blue_ScrollBar)
{switch(nSBCode)
{case SB_THUMBTRACK:
m_Blue_ScrollBar.SetScrollPos(nPos);
iBlueNowPos=nPos;
break;
case SB_LINEDOWN:
iBlueNowPos=m_Blue_ScrollBar.GetScrollPos();
iBlueNowPos=iBlueNowPos+1;
if(iBlueNowPos>255)
iBlueNowPos=255;
break;
case SB_LINEUP:
iBlueNowPos=m_Blue_ScrollBar.GetScrollPos();
iBlueNowPos=iBlueNowPos-1;
if(iBlueNowPos<0)
iBlueNowPos=0;
break;
case SB_PAGEDOWN:
iBlueNowPos=m_Blue_ScrollBar.GetScrollPos();
iBlueNowPos=iBlueNowPos+10;
if(iBlueNowPos>255)
iBlueNowPos=255;
break;
case SB_PAGEUP:
iBlueNowPos=m_Blue_ScrollBar.GetScrollPos();
iBlueNowPos=iBlueNowPos-10;
if(iBlueNowPos<0)
iBlueNowPos=0;
break;
}}
//
m_Red_ScrollBar.SetScrollPos(iRedNowPos);
itoa(iRedNowPos,sPos,10);
m_Red_Edit.SetWindowText(sPos);
m_Green_ScrollBar.SetScrollPos(iGreenNowPos);
itoa(iGreenNowPos,sPos,10);
m_Green_Edit.SetWindowText(sPos);
m_Blue_ScrollBar.SetScrollPos(iBlueNowPos);
itoa(iBlueNowPos,sPos,10);
m_Blue_Edit.SetWindowText(sPos);
//
CDC *pDC=GetDC();
CBrush newBrush;
CPen newPen;
newBrush.CreateSolidBrush(RGB(iRedNowPos,iGreenNowPos,iBlueNowPos));
newPen.CreatePen(PS_SOLID,1,RGB(iRedNowPos,iGreenNowPos,iBlueNowPos));
pDC->SelectObject(&newPen);
pDC->SelectObject(&newBrush);
pDC->Ellipse(70,30,170,200);
ReleaseDC(pDC);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -