home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / controls / smiley / smileydg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  6.9 KB  |  260 lines

  1. // SmileyDg.cpp : implementation file
  2. //
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include "stdafx.h"
  15. #include "Smiley.h"
  16. #include "SmileyDg.h"
  17.  
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CAboutDlg dialog used for App About
  26.  
  27. class CAboutDlg : public CDialog
  28. {
  29. public:
  30.     CAboutDlg();
  31.  
  32. // Dialog Data
  33.     //{{AFX_DATA(CAboutDlg)
  34.     enum { IDD = IDD_ABOUTBOX };
  35.     //}}AFX_DATA
  36.  
  37.     // ClassWizard generated virtual function overrides
  38.     //{{AFX_VIRTUAL(CAboutDlg)
  39.     protected:
  40.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  41.     //}}AFX_VIRTUAL
  42.  
  43. // Implementation
  44. protected:
  45.     //{{AFX_MSG(CAboutDlg)
  46.     //}}AFX_MSG
  47.     DECLARE_MESSAGE_MAP()
  48. };
  49.  
  50. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  51. {
  52.     //{{AFX_DATA_INIT(CAboutDlg)
  53.     //}}AFX_DATA_INIT
  54. }
  55.  
  56. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  57. {
  58.     CDialog::DoDataExchange(pDX);
  59.     //{{AFX_DATA_MAP(CAboutDlg)
  60.     //}}AFX_DATA_MAP
  61. }
  62.  
  63. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  64.     //{{AFX_MSG_MAP(CAboutDlg)
  65.         // No message handlers
  66.     //}}AFX_MSG_MAP
  67. END_MESSAGE_MAP()
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CSmileyDlg dialog
  71.  
  72. CSmileyDlg::CSmileyDlg(CWnd* pParent /*=NULL*/)
  73.     : CDialog(CSmileyDlg::IDD, pParent)
  74. {
  75.     //{{AFX_DATA_INIT(CSmileyDlg)
  76.         // NOTE: the ClassWizard will add member initialization here
  77.     //}}AFX_DATA_INIT
  78.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  79.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  80. }
  81.  
  82. void CSmileyDlg::DoDataExchange(CDataExchange* pDX)
  83. {
  84.     CDialog::DoDataExchange(pDX);
  85.     //{{AFX_DATA_MAP(CSmileyDlg)
  86.     DDX_Control(pDX, IDC_SAD, m_ctlSad);
  87.     DDX_Control(pDX, IDC_SMILECTRL1, m_ctlSmile);
  88.     //}}AFX_DATA_MAP
  89. }
  90.  
  91. BEGIN_MESSAGE_MAP(CSmileyDlg, CDialog)
  92.     //{{AFX_MSG_MAP(CSmileyDlg)
  93.     ON_WM_SYSCOMMAND()
  94.     ON_WM_PAINT()
  95.     ON_WM_QUERYDRAGICON()
  96.     ON_WM_LBUTTONDBLCLK()
  97.     ON_CONTROL(BN_CLICKED, IDC_SAD, OnSad)
  98.     //}}AFX_MSG_MAP
  99. END_MESSAGE_MAP()
  100.  
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CSmileyDlg Event Map
  104.  
  105. BEGIN_EVENTSINK_MAP(CSmileyDlg, CDialog)
  106.     ON_EVENT(CSmileyDlg, IDC_SMILECTRL1, DISPID_CLICKIN, OnClickIn, VTS_XPOS_PIXELS VTS_YPOS_PIXELS)
  107.     ON_EVENT(CSmileyDlg, IDC_SMILECTRL1, DISPID_CLICKOUT, OnClickOut, VTS_NONE)
  108. END_EVENTSINK_MAP()
  109.  
  110.  
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CSmileyDlg message handlers
  113.  
  114. BOOL CSmileyDlg::OnInitDialog()
  115. {
  116.     CDialog::OnInitDialog();
  117.  
  118.     // Add "About..." menu item to system menu.
  119.  
  120.     // IDM_ABOUTBOX must be in the system command range.
  121.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  122.     ASSERT(IDM_ABOUTBOX < 0xF000);
  123.  
  124.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  125.     CString strAboutMenu;
  126.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  127.     if (!strAboutMenu.IsEmpty())
  128.     {
  129.         pSysMenu->AppendMenu(MF_SEPARATOR);
  130.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  131.     }
  132.  
  133.     // Set the icon for this dialog.  The framework does this automatically
  134.     //  when the application's main window is not a dialog
  135.     SetIcon(m_hIcon, TRUE);         // Set big icon
  136.     SetIcon(m_hIcon, FALSE);        // Set small icon
  137.  
  138.  
  139.     // Set the 'Caption' Property in the CSmileCtl OLE Control.  The
  140.     // SetWindowText API will take care of this for us.
  141.  
  142.     m_ctlSmile.SetWindowText(_T("Smile!"));
  143.  
  144.  
  145.     // Set the Value of the Sad Check Button based upon the value of
  146.     // the 'Sad' property in the CSmileCtl OLE Control.
  147.  
  148.     m_ctlSad.SetCheck(m_ctlSmile.GetSad());
  149.  
  150.  
  151.     // Use the Control's BorderStyle stock property to put a border around
  152.     // the control using the CWnd::ModifyStyle API.
  153.  
  154.     m_ctlSmile.ModifyStyle(0, WS_BORDER);
  155.  
  156.     return TRUE;  // return TRUE  unless you set the focus to a control
  157. }
  158.  
  159. void CSmileyDlg::OnSysCommand(UINT nID, LPARAM lParam)
  160. {
  161.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  162.     {
  163.         CAboutDlg dlgAbout;
  164.         dlgAbout.DoModal();
  165.     }
  166.     else
  167.     {
  168.         CDialog::OnSysCommand(nID, lParam);
  169.     }
  170. }
  171.  
  172. // If you add a minimize button to your dialog, you will need the code below
  173. //  to draw the icon.  For MFC applications using the document/view model,
  174. //  this is automatically done for you by the framework.
  175.  
  176. void CSmileyDlg::OnPaint()
  177. {
  178.     if (IsIconic())
  179.     {
  180.         CPaintDC dc(this); // device context for painting
  181.  
  182.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  183.  
  184.         // Center icon in client rectangle
  185.         int cxIcon = GetSystemMetrics(SM_CXICON);
  186.         int cyIcon = GetSystemMetrics(SM_CYICON);
  187.         CRect rect;
  188.         GetClientRect(&rect);
  189.         int x = (rect.Width() - cxIcon + 1) / 2;
  190.         int y = (rect.Height() - cyIcon + 1) / 2;
  191.  
  192.         // Draw the icon
  193.         dc.DrawIcon(x, y, m_hIcon);
  194.     }
  195.     else
  196.     {
  197.         CDialog::OnPaint();
  198.     }
  199. }
  200.  
  201. // The system calls this to obtain the cursor to display while the user drags
  202. //  the minimized window.
  203. HCURSOR CSmileyDlg::OnQueryDragIcon()
  204. {
  205.     return (HCURSOR) m_hIcon;
  206. }
  207.  
  208.  
  209. void CSmileyDlg::OnLButtonDblClk(UINT, CPoint)
  210. {
  211.     // Here the Container handles the WM_LBUTTONDBLCLK message where it can
  212.     // do its own processing.  In this case, it sets the BackColor and
  213.     // ForeColor properties in the control.  This handler is only called
  214.     // when double clicks occur OUTSIDE the control.  To handle double
  215.     // clicks inside the control, have the control fire the stock DblClk
  216.     // event and then process it in the CSmileyDlg EVENTSINK map.
  217.  
  218.     OLE_COLOR backClr = m_ctlSmile.GetBackColor();
  219.     OLE_COLOR foreClr = m_ctlSmile.GetForeColor();
  220.  
  221.     // Reverse the Colors!
  222.     m_ctlSmile.SetBackColor(foreClr);
  223.     m_ctlSmile.SetForeColor(backClr);
  224. }
  225.  
  226. /////////////////////////////////////////////////////////////////////////////
  227. // CSmileyDlg Command Handlers
  228.  
  229. void CSmileyDlg::OnSad()
  230. {
  231.     m_ctlSmile.SetSad((m_ctlSad.GetCheck() != 0));
  232. }
  233.  
  234.  
  235. /////////////////////////////////////////////////////////////////////////////
  236. // CSmileyDlg Event Handlers
  237.  
  238. // These handlers are called when the user clicks with the mouse inside and
  239. // outside the 'face' on the CSmileyCtl OLE Control.
  240.  
  241. BOOL CSmileyDlg::OnClickIn(OLE_XPOS_PIXELS, OLE_YPOS_PIXELS)
  242. {
  243.     // We don't make use of the X and Y coordinates of the mouse click
  244.     // passed to us by the event so we don't declare parameter variables
  245.     // for them.
  246.  
  247.     // Call the 'Wink' Method in the CSmileCtl OLE Control
  248.     m_ctlSmile.Wink(TRUE);
  249.  
  250.     return TRUE;    // We handled the event.  No need for futher processing
  251. }
  252.  
  253. BOOL CSmileyDlg::OnClickOut()
  254. {
  255.     // Call the 'Wink' Method in the CSmileCtl OLE Control
  256.     m_ctlSmile.Wink(FALSE);
  257.  
  258.     return TRUE;    // We handled the event.  No need for futher processing
  259. }
  260.