// DemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Demo.h"
#include "DemoDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CDemoDlg dialog
CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDemoDlg)
m_text1 = _T("0");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_col = RGB(255, 0, 0);
m_setCol = false;
m_font.CreatePointFont(200, "华文楷体");
m_brush.CreateSolidBrush(RGB(0, 255, 0));
}
void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDemoDlg)
DDX_Control(pDX, IDC_BUTTON1, m_Button1);
DDX_Control(pDX, IDC_BT_UPANDDOWN, m_BtnUpAndDown);
DDX_Text(pDX, IDC_EDIT1, m_text1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
//{{AFX_MSG_MAP(CDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_BN_CLICKED(IDC_BUTTON1, OnColor)
ON_BN_CLICKED(IDC_BUTTON2, OnSetFont)
ON_WM_MOUSEMOVE()
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDemoDlg message handlers
BOOL CDemoDlg::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
GetDlgItem(IDC_ST_Pic)->GetWindowRect(m_rect);
m_rect.top += 3;
m_rect.left += 3;
m_rect.right -= 3;
m_rect.bottom -=3;
ScreenToClient(&m_rect);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDemoDlg::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 CDemoDlg::OnPaint()
{
CPaintDC dc(this);
CBrush br(m_col);
dc.FillRect(m_rect, &br);
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 CDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDemoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default IDI_BT_DOWN
if (::ChildWindowFromPoint(m_hWnd,point) == m_BtnUpAndDown.m_hWnd)
{
SetCapture();
SetCursor(AfxGetApp()->LoadCursor(IDC_BT_DOWN));
m_BtnUpAndDown.SetIcon(AfxGetApp()->LoadIcon(IDI_BT_DOWN));
m_setCol = TRUE;
}
CDialog::OnLButtonDown(nFlags, point);
}
void CDemoDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
ReleaseCapture();
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
m_BtnUpAndDown.SetIcon(AfxGetApp()->LoadIcon(IDI_BT_UP));
m_setCol = false;
CDialog::OnLButtonUp(nFlags, point);
}
void CDemoDlg::OnColor()
{
// TODO: Add your control notification handler code here
CColorDialog dlg;
//为了保留先前的设置而进行的操作
dlg.m_cc.Flags |= CC_RGBINIT;
dlg.m_cc.rgbResult = m_col;
if(IDOK == dlg.DoModal())
{
// m_col = dlg.m_cc.rgbResult; //这两种方式都可以
m_col = dlg.GetColor();
}
InvalidateRect(m_rect,true);
}
void CDemoDlg::OnSetFont()
{
// TODO: Add your control notification handler code here
CFontDialog dlg;
if(IDOK == dlg.DoModal())
{
if(m_font.m_hObject)
m_font.DeleteObject();
m_font.CreateFontIndirect(dlg.m_cf.lpLogFont);
Invalidate();
}
}
void CDemoDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_setCol)
{
//坐标转换
ClientToScreen(&point);
//获得桌面上鼠标移到的任意一点的颜色 ::GetDC(0) 用0表示全局即桌面
m_col=::GetPixel(::GetDC(0),point.x,point.y);
InvalidateRect(m_rect,true);
}
CDialog::OnMouseMove(nFlags, point);
}
HBRUSH CDemoDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(pWnd->GetDlgCtrlID() == IDC_ST_Font)
{
pDC->SelectObject(&m_font);
}
if(pWnd->GetDlgCtrlID() == IDC_EDIT1)
{
pDC->SetTextColor(RGB(255, 0, 0));
pDC->SetBkMode(TRANSPARENT);
return m_brush;
}
if(pWnd->GetDlgCtrlID() == IDC_BUTTON1)
{
pDC->SetTextColor(RGB(255, 0, 255));
}
// TODO: Return a different brush if the default is not desired
return hbr;
}