home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / commap / ctlcomm / ctlcommdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  6.5 KB  |  258 lines

  1. // ctlcommDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "ctlcomm.h"
  6. #include "ctlcommDlg.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. // CCtlcommDlg dialog
  61.  
  62. CCtlcommDlg::CCtlcommDlg(CWnd* pParent /*=NULL*/)
  63.     : CDialog(CCtlcommDlg::IDD, pParent)
  64. {
  65.     //{{AFX_DATA_INIT(CCtlcommDlg)
  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. }
  71.  
  72. void CCtlcommDlg::DoDataExchange(CDataExchange* pDX)
  73. {
  74.     CDialog::DoDataExchange(pDX);
  75.     //{{AFX_DATA_MAP(CCtlcommDlg)
  76.         // NOTE: the ClassWizard will add DDX and DDV calls here
  77.     //}}AFX_DATA_MAP
  78. }
  79.  
  80. BEGIN_MESSAGE_MAP(CCtlcommDlg, CDialog)
  81.     //{{AFX_MSG_MAP(CCtlcommDlg)
  82.     ON_WM_SYSCOMMAND()
  83.     ON_WM_PAINT()
  84.     ON_WM_QUERYDRAGICON()
  85.     ON_BN_CLICKED(IDC_COM1, OnCom1)
  86.     ON_BN_CLICKED(IDC_COM2, OnCom2)
  87.     ON_BN_CLICKED(IDC_COM3, OnCom3)
  88.     ON_BN_CLICKED(IDC_COM4, OnCom4)
  89.     ON_BN_CLICKED(IDC_COM5, OnCom5)
  90.     ON_BN_CLICKED(IDC_COM6, OnCom6)
  91.     ON_BN_CLICKED(IDC_COM7, OnCom7)
  92.     ON_BN_CLICKED(IDC_COM8, OnCom8)
  93.     ON_WM_CLOSE()
  94.     ON_BN_CLICKED(IDC_RESET, OnReset)
  95.     //}}AFX_MSG_MAP
  96. END_MESSAGE_MAP()
  97.  
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CCtlcommDlg message handlers
  100.  
  101. BOOL CCtlcommDlg::OnInitDialog()
  102. {
  103.     CDialog::OnInitDialog();
  104.  
  105.     OnReset();
  106.  
  107.     CoInitialize(NULL);
  108.     HRESULT hr = pOuter.CreateInstance(__uuidof(COuter), NULL, CLSCTX_INPROC_SERVER);
  109.     if (FAILED(hr))
  110.     {
  111.         TCHAR buf[64];
  112.         wsprintf(buf, _T("CreateInstance failed(hr=0x%x)\n"), hr);
  113.         AfxMessageBox(buf);
  114.         if ((bool) pOuter)
  115.             pOuter.Release();
  116.         CoUninitialize();
  117.         CDialog::EndDialog(IDCANCEL);
  118.     }
  119.     
  120.     // Add "About..." menu item to system menu.
  121.  
  122.     // IDM_ABOUTBOX must be in the system command range.
  123.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  124.     ASSERT(IDM_ABOUTBOX < 0xF000);
  125.  
  126.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  127.     if (pSysMenu != NULL)
  128.     {
  129.         CString strAboutMenu;
  130.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  131.         if (!strAboutMenu.IsEmpty())
  132.         {
  133.             pSysMenu->AppendMenu(MF_SEPARATOR);
  134.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  135.         }
  136.     }
  137.  
  138.     // Set the icon for this dialog.  The framework does this automatically
  139.     //  when the application's main window is not a dialog
  140.     SetIcon(m_hIcon, TRUE);         // Set big icon
  141.     SetIcon(m_hIcon, FALSE);        // Set small icon
  142.     
  143.     // TODO: Add extra initialization here
  144.     
  145.     
  146.     return TRUE;  // return TRUE  unless you set the focus to a control
  147. }
  148.  
  149. void CCtlcommDlg::OnSysCommand(UINT nID, LPARAM lParam)
  150. {
  151.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  152.     {
  153.         CAboutDlg dlgAbout;
  154.         dlgAbout.DoModal();
  155.     }
  156.     else
  157.     {
  158.         CDialog::OnSysCommand(nID, lParam);
  159.     }
  160. }
  161.  
  162. // If you add a minimize button to your dialog, you will need the code below
  163. //  to draw the icon.  For MFC applications using the document/view model,
  164. //  this is automatically done for you by the framework.
  165.  
  166. void CCtlcommDlg::OnPaint() 
  167. {
  168.     if (IsIconic())
  169.     {
  170.         CPaintDC dc(this); // device context for painting
  171.  
  172.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  173.  
  174.         // Center icon in client rectangle
  175.         int cxIcon = GetSystemMetrics(SM_CXICON);
  176.         int cyIcon = GetSystemMetrics(SM_CYICON);
  177.         CRect rect;
  178.         GetClientRect(&rect);
  179.         int x = (rect.Width() - cxIcon + 1) / 2;
  180.         int y = (rect.Height() - cyIcon + 1) / 2;
  181.  
  182.         // Draw the icon
  183.         dc.DrawIcon(x, y, m_hIcon);
  184.     }
  185.     else
  186.     {
  187.         CDialog::OnPaint();
  188.     }
  189. }
  190.  
  191. // The system calls this to obtain the cursor to display while the user drags
  192. //  the minimized window.
  193. HCURSOR CCtlcommDlg::OnQueryDragIcon()
  194. {
  195.     return (HCURSOR) m_hIcon;
  196. }
  197.  
  198. void CCtlcommDlg::OnCom1() 
  199. {
  200.     SetDlgItemText(IDC_COM1, pOuter->GetName(0));
  201. }
  202.  
  203. void CCtlcommDlg::OnCom2() 
  204. {
  205.     SetDlgItemText(IDC_COM2, pOuter->GetName(1));
  206. }
  207.  
  208. void CCtlcommDlg::OnCom3() 
  209. {
  210.     SetDlgItemText(IDC_COM3, pOuter->GetName(2));
  211. }
  212.  
  213. void CCtlcommDlg::OnCom4() 
  214. {
  215.     SetDlgItemText(IDC_COM4, pOuter->GetName(3));
  216. }
  217.  
  218. void CCtlcommDlg::OnCom5() 
  219. {
  220.     SetDlgItemText(IDC_COM5, pOuter->GetName(4));
  221. }
  222.  
  223. void CCtlcommDlg::OnCom6() 
  224. {
  225.     SetDlgItemText(IDC_COM6, pOuter->GetName(5));
  226. }
  227.  
  228. void CCtlcommDlg::OnCom7() 
  229. {
  230.     SetDlgItemText(IDC_COM7, pOuter->GetName(6));
  231. }
  232.  
  233. void CCtlcommDlg::OnCom8() 
  234. {
  235.     SetDlgItemText(IDC_COM8, pOuter->GetName(7));
  236. }
  237.  
  238. void CCtlcommDlg::OnReset() 
  239. {
  240.     SetDlgItemText(IDC_COM1, "COM_INTERFACE_ENTRY");
  241.     SetDlgItemText(IDC_COM2, "COM_INTERFACE_ENTRY_TEAR_OFF");
  242.     SetDlgItemText(IDC_COM3, "COM_INTERFACE_ENTRY_CACHED_TEAR_OFF");
  243.     SetDlgItemText(IDC_COM4, "COM_INTERFACE_ENTRY_CHAIN");
  244.     SetDlgItemText(IDC_COM5, "COM_INTERFACE_ENTRY_AGGREGATE");
  245.     SetDlgItemText(IDC_COM6, "COM_INTERFACE_ENTRY_AUTOAGGREGATE");
  246.     SetDlgItemText(IDC_COM7, "COM_INTERFACE_ENTRY_AGGREGATE_BLIND");
  247.     SetDlgItemText(IDC_COM8, "COM_INTERFACE_ENTRY_AUTOAGGREGATE_BLIND");
  248. }
  249.  
  250. void CCtlcommDlg::OnClose() 
  251. {
  252.     if ((bool) pOuter)
  253.         pOuter.Release();
  254.     OleUninitialize();
  255.     CDialog::OnClose();
  256. }
  257.  
  258.