home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / addins / cmdwnd / msdevcmd / msdevcmddlg.cpp < prev    next >
C/C++ Source or Header  |  1998-04-08  |  6KB  |  241 lines

  1. // msdevcmdDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "msdevcmd.h"
  6. #include "msdevcmdDlg.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAboutDlg dialog used for App About
  16.  
  17. class CAboutDlg : public CDialog
  18. {
  19. public:
  20.     CAboutDlg();
  21.  
  22. // Dialog Data
  23.     //{{AFX_DATA(CAboutDlg)
  24.     enum { IDD = IDD_ABOUTBOX };
  25.     //}}AFX_DATA
  26.  
  27.     // ClassWizard generated virtual function overrides
  28.     //{{AFX_VIRTUAL(CAboutDlg)
  29.     protected:
  30.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  31.     //}}AFX_VIRTUAL
  32.  
  33. // Implementation
  34. protected:
  35.     //{{AFX_MSG(CAboutDlg)
  36.     //}}AFX_MSG
  37.     DECLARE_MESSAGE_MAP()
  38. };
  39.  
  40. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  41. {
  42.     //{{AFX_DATA_INIT(CAboutDlg)
  43.     //}}AFX_DATA_INIT
  44. }
  45.  
  46. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  47. {
  48.     CDialog::DoDataExchange(pDX);
  49.     //{{AFX_DATA_MAP(CAboutDlg)
  50.     //}}AFX_DATA_MAP
  51. }
  52.  
  53. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  54.     //{{AFX_MSG_MAP(CAboutDlg)
  55.         // No message handlers
  56.     //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CMsdevcmdDlg dialog
  61.  
  62. CMsdevcmdDlg::CMsdevcmdDlg(CWnd* pParent /*=NULL*/)
  63.     : CDialog(CMsdevcmdDlg::IDD, pParent)
  64. {
  65.     //{{AFX_DATA_INIT(CMsdevcmdDlg)
  66.         // NOTE: the ClassWizard will add member initialization here
  67.     //}}AFX_DATA_INIT
  68.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  69.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  70.     m_bmPinned.LoadBitmap(IDB_BM_PINNED);
  71.     m_bmNotPinned.LoadBitmap(IDB_BM_NOTPINNED);
  72. }
  73.  
  74. CMsdevcmdDlg::~CMsdevcmdDlg()
  75. {
  76. }
  77.  
  78. void CMsdevcmdDlg::DoDataExchange(CDataExchange* pDX)
  79. {
  80.     CDialog::DoDataExchange(pDX);
  81.     //{{AFX_DATA_MAP(CMsdevcmdDlg)
  82.     DDX_Control(pDX, IDC_BTN_PIN, m_ctlBtnPinned);
  83.     DDX_Control(pDX, IDC_EDITCMD, m_ctlEditCmd);
  84.     //}}AFX_DATA_MAP
  85. }
  86.  
  87. BEGIN_MESSAGE_MAP(CMsdevcmdDlg, CDialog)
  88.     //{{AFX_MSG_MAP(CMsdevcmdDlg)
  89.     ON_WM_SYSCOMMAND()
  90.     ON_WM_PAINT()
  91.     ON_WM_QUERYDRAGICON()
  92.     ON_WM_CHAR()
  93.     ON_WM_SIZE()
  94.     ON_BN_CLICKED(IDC_BTN_PIN, OnBtnPin)
  95.     //}}AFX_MSG_MAP
  96. END_MESSAGE_MAP()
  97.  
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CMsdevcmdDlg message handlers
  100.  
  101. BOOL CMsdevcmdDlg::OnInitDialog()
  102. {
  103.     CDialog::OnInitDialog();
  104.  
  105.     // Add "About..." menu item to system menu.
  106.  
  107.     // IDM_ABOUTBOX must be in the system command range.
  108.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  109.     ASSERT(IDM_ABOUTBOX < 0xF000);
  110.  
  111.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  112.     if (pSysMenu != NULL)
  113.     {
  114.         CString strAboutMenu;
  115.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  116.         if (!strAboutMenu.IsEmpty())
  117.         {
  118.             pSysMenu->AppendMenu(MF_SEPARATOR);
  119.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  120.         }
  121.     }
  122.  
  123.     // Set the icon for this dialog.  The framework does this automatically
  124.     //  when the application's main window is not a dialog
  125.     SetIcon(m_hIcon, TRUE);            // Set big icon
  126.     SetIcon(m_hIcon, FALSE);        // Set small icon
  127.     
  128.     m_fTopMost = FALSE;
  129.     m_ctlBtnPinned.SetBitmap(m_bmNotPinned);
  130.     m_ctlEditCmd.SetParent(this);
  131.     m_ctlEditCmd.LimitText();
  132.     return TRUE;  // return TRUE  unless you set the focus to a control
  133. }
  134.  
  135. void CMsdevcmdDlg::OnSysCommand(UINT nID, LPARAM lParam)
  136. {
  137.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  138.     {
  139.         CAboutDlg dlgAbout;
  140.         dlgAbout.DoModal();
  141.     }
  142.     else
  143.     {
  144.         CDialog::OnSysCommand(nID, lParam);
  145.     }
  146. }
  147.  
  148. // If you add a minimize button to your dialog, you will need the code below
  149. //  to draw the icon.  For MFC applications using the document/view model,
  150. //  this is automatically done for you by the framework.
  151.  
  152. void CMsdevcmdDlg::OnPaint() 
  153. {
  154.     if (IsIconic())
  155.     {
  156.         CPaintDC dc(this); // device context for painting
  157.  
  158.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  159.  
  160.         // Center icon in client rectangle
  161.         int cxIcon = GetSystemMetrics(SM_CXICON);
  162.         int cyIcon = GetSystemMetrics(SM_CYICON);
  163.         CRect rect;
  164.         GetClientRect(&rect);
  165.         int x = (rect.Width() - cxIcon + 1) / 2;
  166.         int y = (rect.Height() - cyIcon + 1) / 2;
  167.  
  168.         // Draw the icon
  169.         dc.DrawIcon(x, y, m_hIcon);
  170.     }
  171.     else
  172.     {
  173.         CDialog::OnPaint();
  174.     }
  175. }
  176.  
  177. // The system calls this to obtain the cursor to display while the user drags
  178. //  the minimized window.
  179. HCURSOR CMsdevcmdDlg::OnQueryDragIcon()
  180. {
  181.     return (HCURSOR) m_hIcon;
  182. }
  183.  
  184. void CMsdevcmdDlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
  185. {
  186.     if (nChar == _T('\n'))
  187.     {
  188.         //process command.
  189.     }
  190.     CDialog::OnChar(nChar, nRepCnt, nFlags);
  191. }
  192.  
  193. void CMsdevcmdDlg::OnSize(UINT nType, int cx, int cy) 
  194. {
  195.     CRect rect, rectButton;
  196.  
  197.     if (m_ctlBtnPinned.GetSafeHwnd())
  198.     {
  199.         m_ctlBtnPinned.GetClientRect(rectButton);
  200.         if (cx > rectButton.Width() * 3 && cy > rectButton.Height() * 3)
  201.         {
  202.             CDialog::OnSize(nType, cx, cy);
  203.             
  204.             m_ctlEditCmd.GetClientRect(rect);
  205.             rect.top += rectButton.Height() + 5;
  206.             m_ctlEditCmd.MoveWindow(rect.left, rect.top, cx, cy - 5 - 2);
  207.         }
  208.         else
  209.         {
  210.             GetWindowRect(rect);
  211.             if (cy < rectButton.Height() * 3)
  212.                 rect.bottom = rect.top + rectButton.Height() * 3;
  213.             if (cx < rectButton.Width() * 3)
  214.                 rect.right = rect.left + rectButton.Width() * 3;
  215.             MoveWindow(rect);
  216.         }
  217.     }
  218. }
  219.  
  220. void CMsdevcmdDlg::Pin(BOOL fPin, BOOL fShow /* = TRUE*/) 
  221. {
  222.     if (fPin)
  223.     {
  224.         SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOREDRAW);
  225.         if (fShow)
  226.             m_ctlBtnPinned.SetBitmap(m_bmPinned);
  227.     }
  228.     else
  229.     { 
  230.         SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOREDRAW);
  231.         if (fShow)
  232.             m_ctlBtnPinned.SetBitmap(m_bmNotPinned);
  233.     }
  234.     m_fTopMost = fPin;
  235. }
  236.  
  237. void CMsdevcmdDlg::OnBtnPin() 
  238. {
  239.     Pin(~m_fTopMost);
  240. }
  241.