// buttonDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ImageToggleButton.h"
#include "ToggleButton.h"
#include "button.h"
#include "buttonDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CButtonDlg dialog
CButtonDlg::CButtonDlg(CWnd* pParent /*=NULL*/)
: CDialog(CButtonDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CButtonDlg)
// 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 CButtonDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CButtonDlg)
DDX_Control(pDX, IDC_TOGGLE, c_Toggle);
DDX_Control(pDX, IDC_DOWNUP, c_DownUp);
DDX_Control(pDX, IDC_AUTOGRAY, c_Autogray);
DDX_Control(pDX, IDC_FILL, c_Fill);
DDX_Control(pDX, IDC_PICTURE, c_Picture);
DDX_Control(pDX, IDC_SMILEY, c_Smiley);
DDX_Control(pDX, IDC_LIMIT, c_Limit);
DDX_Control(pDX, IDC_LEFT, c_Left);
DDX_Control(pDX, IDC_HCENTER, c_Center);
DDX_Control(pDX, IDC_RIGHT, c_Right);
DDX_Control(pDX, IDC_TOP, c_Top);
DDX_Control(pDX, IDC_VCENTER, c_VCenter);
DDX_Control(pDX, IDC_BOTTOM, c_Bottom);
DDX_Control(pDX, IDC_SPINWIDTH, c_SpinWidth);
DDX_Control(pDX, IDC_SPINHEIGHT, c_SpinHeight);
DDX_Control(pDX, IDC_ENABLE, c_Enabled);
DDX_Control(pDX, IDC_BUTTON, c_Button);
DDX_Control(pDX, IDC_BUTTON2, c_Button2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CButtonDlg, CDialog)
//{{AFX_MSG_MAP(CButtonDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BOTTOM, OnBottom)
ON_BN_CLICKED(IDC_ENABLE, OnEnable)
ON_BN_CLICKED(IDC_HCENTER, OnHcenter)
ON_BN_CLICKED(IDC_LEFT, OnLeft)
ON_BN_CLICKED(IDC_RIGHT, OnRight)
ON_BN_CLICKED(IDC_TOP, OnTop)
ON_BN_CLICKED(IDC_VCENTER, OnVcenter)
ON_EN_CHANGE(IDC_HEIGHT, OnChangeHeight)
ON_EN_CHANGE(IDC_WIDTH, OnChangeWidth)
ON_BN_CLICKED(IDC_SMILEY, OnSmiley)
ON_BN_CLICKED(IDC_PICTURE, OnPicture)
ON_BN_CLICKED(IDC_FILL, OnFill)
ON_BN_CLICKED(IDC_AUTOGRAY, OnAutogray)
ON_BN_CLICKED(IDC_DOWNUP, OnDownup)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButtonDlg message handlers
BOOL CButtonDlg::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);
}
}
c_Button2.m_toggle = TRUE;
// 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
CheckRadioButton(IDC_SMILEY, IDC_PICTURE, IDC_SMILEY);
c_Button.LoadBitmaps(IDB_UP, IDB_DOWN, IDB_DISABLED);
c_Button2.LoadBitmaps(IDB_UP, IDB_DOWN, IDB_DISABLED);
c_Enabled.SetCheck(BST_CHECKED);
CheckRadioButton(IDC_LEFT, IDC_RIGHT, IDC_HCENTER);
CheckRadioButton(IDC_TOP, IDC_BOTTOM, IDC_VCENTER);
CRect b;
CRect w;
c_Limit.GetWindowRect(&w);
c_Button.GetWindowRect(&b);
c_SpinWidth.SetRange(10, w.Width());
c_SpinHeight.SetRange(10, w.Height());
c_SpinWidth.SetPos(b.Width());
c_SpinHeight.SetPos(b.Height());
updateControls();
return TRUE; // return TRUE unless you set the focus to a control
}
void CButtonDlg::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 CButtonDlg::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 CButtonDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CButtonDlg::OnCancel()
{
CDialog::OnCancel();
}
void CButtonDlg::OnOK()
{
CDialog::OnOK();
}
void CButtonDlg::OnBottom()
{
c_Button.SetVPos(BS_BOTTOM);
c_Button2.SetVPos(BS_BOTTOM);
c_Toggle.SetVPos(BS_BOTTOM);
}
void CButtonDlg::OnEnable()
{
BOOL enable = c_Enabled.GetCheck() == BST_CHECKED;
c_Button.EnableWindow(enable);
c_Button2.EnableWindow(enable);
c_Toggle.EnableWindow(enable);
}
void CButtonDlg::OnHcenter()
{
c_Button.SetHPos(BS_CENTER);
c_Button2.SetHPos(BS_CENTER);
c_Toggle.SetHPos(BS_CENTER);
}
void CButtonDlg::OnLeft()
{
c_Button.SetHPos(BS_LEFT);
c_Button2.SetHPos(BS_LEFT);
c_Toggle.SetHPos(BS_LEFT);
}
void CButtonDlg::OnRight()
{
c_Button.SetHPos(BS_RIGHT);
c_Button2.SetHPos(BS_RIGHT);
c_Toggle.SetHPos(BS_RIGHT);
}
void CButtonDlg::OnTop()
{
c_Button.SetVPos(BS_TOP);
c_Button2.SetVPos(BS_TOP);
c_Toggle.SetVPos(BS_TOP);
}
void CButtonDlg::OnVcenter()
{
c_Button.SetVPos(BS_VCENTER);
c_Button2.SetVPos(BS_VCENTER);
c_Toggle.SetVPos(BS_VCENTER);
}
void CButtonDlg::setButtonPos(int width, int height)
{
CRect L;
c_Limit.GetWindowRect(&L);
ScreenToClient(&L);
int x = (L.Width() - width) / 2;
int y = (L.Height() - height) / 5;
c_Button.MoveWindow(x, y, width, height);
c_Button2.MoveWindow(x, 4*y, width, height);
}
void CButtonDlg::OnChangeHeight()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
if(::IsWindow(c_SpinWidth.m_hWnd))
setButtonPos(c_SpinWidth.GetPos(), c_SpinHeight.GetPos());
}
void CButtonDlg::OnChangeWidth()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
if(::IsWindow(c_SpinWidth.m_hWnd))
setButtonPos(c_SpinWidth.GetPos(), c_SpinHeight.GetPos());
}
void CButtonDlg::setImage(CImageToggleButton &button)
{
if(c_Smiley.GetCheck() == BST_CHECKED)
button.LoadBitmaps(IDB_UP,
c_DownUp.GetCheck() == BST_CHECKED ? NULL : IDB_DOWN,
c_Autogray.GetCheck() == BST_CHECKED ? NULL : IDB_DISABLED);
else
button.LoadBitmaps(IDB_PICTURE_UP,
c_DownUp.GetCheck() == BST_CHECKED ? NULL : IDB_PICTURE_DOWN,
c_Autogray.GetCheck() == BST_CHECKED ? NULL : IDB_PICTURE_DISABLED);
button.InvalidateRect(NULL);
}
void CButtonDlg::setImage()
{
setImage(c_Button);
setImage(c_Button2);
}
void CButtonDlg::OnSmiley()
{
setImage();
}
void CButtonDlg::OnPicture()
{
setImage();
}
void CButtonDlg::updateControls()
{
c_Left.EnableWindow(c_Fill.GetCheck() == BST_UNCHECKED);
c_Center.EnableWindow(c_Fill.GetCheck() == BST_UNCHECKED);
c_Right.EnableWindow(c_Fill.GetCheck() == BST_UNCHECKED);
c_Top.EnableWindow(c_Fill.GetCheck() == BST_UNCHECKED);
c_Bottom.EnableWindow(c_Fill.GetCheck() == BST_UNCHECKED);
c_VCenter.EnableWindow(c_Fill.GetCheck() == BST_UNCHECKED);
}
void CButtonDlg::OnFill()
{
updateControls();
c_Button.m_fill = c_Button2.m_fill = c_Fill.GetCheck() == BST_CHECKED;
c_Button.InvalidateRect(NULL);
c_Button2.InvalidateRect(NULL);
}
void CButtonDlg::OnAutogray()
{
setImage();
}
void CButtonDlg::OnDownup()
{
setImage();
}