home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / GUIDGEN.PAK / GUIDGDLG.CPP next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.8 KB  |  278 lines

  1. // guidgdlg.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "guidgen.h"
  15. #include "guidgdlg.h"
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CAboutDlg dialog used for App About
  24.  
  25. class CAboutDlg : public CDialog
  26. {
  27. public:
  28.     CAboutDlg();
  29.  
  30. // Dialog Data
  31.     //{{AFX_DATA(CAboutDlg)
  32.     enum { IDD = IDD_ABOUTBOX };
  33.     //}}AFX_DATA
  34.  
  35. // Implementation
  36. protected:
  37.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  38.     //{{AFX_MSG(CAboutDlg)
  39.     virtual BOOL OnInitDialog();
  40.     //}}AFX_MSG
  41.     DECLARE_MESSAGE_MAP()
  42. };
  43.  
  44. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  45. {
  46.     //{{AFX_DATA_INIT(CAboutDlg)
  47.     //}}AFX_DATA_INIT
  48. }
  49.  
  50. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  51. {
  52.     CDialog::DoDataExchange(pDX);
  53.     //{{AFX_DATA_MAP(CAboutDlg)
  54.     //}}AFX_DATA_MAP
  55. }
  56.  
  57. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  58.     //{{AFX_MSG_MAP(CAboutDlg)
  59.         // No message handlers
  60.     //}}AFX_MSG_MAP
  61. END_MESSAGE_MAP()
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CAboutDlg message handlers
  65.  
  66. BOOL CAboutDlg::OnInitDialog()
  67. {
  68.     CDialog::OnInitDialog();
  69.     CenterWindow();
  70.     
  71.     return TRUE;  // return TRUE  unless you set the focus to a control
  72. }
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CGuidGenDlg dialog
  76.  
  77. CGuidGenDlg::CGuidGenDlg(CWnd* pParent /*=NULL*/)
  78.     : CDialog(CGuidGenDlg::IDD, pParent)
  79. {
  80.     //{{AFX_DATA_INIT(CGuidGenDlg)
  81.     m_nGuidType = -1;
  82.     //}}AFX_DATA_INIT
  83.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  84.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  85. }
  86.  
  87. void CGuidGenDlg::DoDataExchange(CDataExchange* pDX)
  88. {
  89.     CDialog::DoDataExchange(pDX);
  90.     //{{AFX_DATA_MAP(CGuidGenDlg)
  91.     DDX_Radio(pDX, IDC_RADIO1, m_nGuidType);
  92.     //}}AFX_DATA_MAP
  93. }
  94.  
  95. BEGIN_MESSAGE_MAP(CGuidGenDlg, CDialog)
  96.     //{{AFX_MSG_MAP(CGuidGenDlg)
  97.     ON_WM_SYSCOMMAND()
  98.     ON_WM_PAINT()
  99.     ON_WM_QUERYDRAGICON()
  100.     ON_BN_CLICKED(IDC_NEWGUID, OnNewguid)
  101.     //}}AFX_MSG_MAP
  102.     ON_CONTROL_RANGE(BN_CLICKED, IDC_RADIO1, IDC_RADIO4, OnChangedSel)
  103. END_MESSAGE_MAP()
  104.  
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CGuidGenDlg message handlers
  107.  
  108. static const TCHAR szOptions[] = _T("Options");
  109. static const TCHAR szGuidType[] = _T("GuidType");
  110.  
  111. BOOL CGuidGenDlg::OnInitDialog()
  112. {
  113.     // start with guid type selected last time
  114.     m_nGuidType = AfxGetApp()->GetProfileInt(szOptions, szGuidType, 0);
  115.  
  116.     // create random GUID
  117.     m_guid = GUID_NULL;
  118.     ::CoCreateGuid(&m_guid);
  119.     if (m_guid == GUID_NULL)
  120.     {
  121.         AfxMessageBox(IDP_ERR_CREATE_GUID);
  122.         EndDialog(IDABORT);
  123.     }
  124.  
  125.     CDialog::OnInitDialog();
  126.     OnChangedSel(IDC_RADIO1+m_nGuidType);
  127.  
  128.     if (GetParent() != NULL)
  129.         CenterWindow();
  130.     
  131.     // Add "About..." menu item to system menu.
  132.  
  133.     // IDM_ABOUTBOX must be in the system command range.
  134.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  135.     ASSERT(IDM_ABOUTBOX < 0xF000);
  136.  
  137.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  138.     CString strAboutMenu;
  139.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  140.     if (!strAboutMenu.IsEmpty())
  141.     {
  142.         pSysMenu->AppendMenu(MF_SEPARATOR);
  143.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  144.     }
  145.     
  146.     // TODO: Add extra initialization here
  147.     
  148.     return TRUE;  // return TRUE  unless you set the focus to a control
  149. }
  150.  
  151. void CGuidGenDlg::OnSysCommand(UINT nID, LPARAM lParam)
  152. {
  153.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  154.     {
  155.         CAboutDlg dlgAbout;
  156.         dlgAbout.DoModal();
  157.     }
  158.     else
  159.     {
  160.         CDialog::OnSysCommand(nID, lParam);
  161.     }
  162. }
  163.  
  164. // If you add a minimize button to your dialog, you will need the code below
  165. //  to draw the icon.  For MFC applications using the document/view model,
  166. //  this is automatically done for you by the framework.
  167.  
  168. void CGuidGenDlg::OnPaint() 
  169. {
  170.     if (IsIconic())
  171.     {
  172.         CPaintDC dc(this); // device context for painting
  173.  
  174.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  175.  
  176.         // Center icon in client rectangle
  177.         int cxIcon = GetSystemMetrics(SM_CXICON);
  178.         int cyIcon = GetSystemMetrics(SM_CYICON);
  179.         CRect rect;
  180.         GetClientRect(&rect);
  181.         int x = (rect.Width() - cxIcon + 1) / 2;
  182.         int y = (rect.Height() - cyIcon + 1) / 2;
  183.  
  184.         // Draw the icon
  185.         dc.DrawIcon(x, y, m_hIcon);
  186.     }
  187.     else
  188.     {
  189.         CDialog::OnPaint();
  190.     }
  191. }
  192.  
  193. // The system calls this to obtain the cursor to display while the user drags
  194. //  the minimized window.
  195. HCURSOR CGuidGenDlg::OnQueryDragIcon()
  196. {
  197.     return (HCURSOR)m_hIcon;
  198. }
  199.  
  200. void CGuidGenDlg::OnChangedSel(UINT /*nID*/)
  201. {
  202.     // update m_nGuidType
  203.     UpdateData(TRUE);
  204.     ASSERT(m_nGuidType >= 0 && m_nGuidType <= 3);
  205.  
  206.     // update the result text
  207.     CString strResult;
  208.     GetFormattedGuid(strResult);
  209.     SetDlgItemText(IDC_RESULTS, strResult);
  210. }
  211.  
  212. void CGuidGenDlg::OnOK() 
  213. {
  214.     UpdateData(TRUE);
  215.  
  216.     // save current type selected
  217.     AfxGetApp()->WriteProfileInt(szOptions, szGuidType, m_nGuidType);
  218.  
  219.     // copy the string to the clipboard
  220.     if (!OpenClipboard())
  221.     {
  222.         AfxMessageBox(IDP_ERR_OPEN_CLIP);
  223.         return;
  224.     }
  225.  
  226.     CString strResult;
  227.     GetFormattedGuid(strResult);
  228.     int nTextLen = (strResult.GetLength()+1)*sizeof(TCHAR);
  229.     HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nTextLen);
  230.     if (hGlobal != NULL)
  231.     {
  232.         LPVOID lpText = GlobalLock(hGlobal);
  233.         memcpy(lpText, strResult, nTextLen);
  234.  
  235.         EmptyClipboard();
  236.         GlobalUnlock(hGlobal);
  237. #ifdef _UNICODE
  238.         SetClipboardData(CF_UNICODETEXT, hGlobal);
  239. #else
  240.         SetClipboardData(CF_TEXT, hGlobal);
  241. #endif
  242.     }
  243.     CloseClipboard();
  244. }
  245.  
  246. void CGuidGenDlg::GetFormattedGuid(CString& rString)
  247. {
  248.     // load appropriate formatting string
  249.     CString strFormat;
  250.     strFormat.LoadString(IDS_FORMATS+m_nGuidType);
  251.  
  252.     // then format into destination
  253.     rString.Format(strFormat, 
  254.         // first copy...
  255.         m_guid.Data1, m_guid.Data2, m_guid.Data3, 
  256.         m_guid.Data4[0], m_guid.Data4[1], m_guid.Data4[2], m_guid.Data4[3],
  257.         m_guid.Data4[4], m_guid.Data4[5], m_guid.Data4[6], m_guid.Data4[7],
  258.         // second copy...
  259.         m_guid.Data1, m_guid.Data2, m_guid.Data3, 
  260.         m_guid.Data4[0], m_guid.Data4[1], m_guid.Data4[2], m_guid.Data4[3],
  261.         m_guid.Data4[4], m_guid.Data4[5], m_guid.Data4[6], m_guid.Data4[7]);
  262. }
  263.  
  264. void CGuidGenDlg::OnNewguid() 
  265. {
  266.     // create random GUID
  267.     m_guid = GUID_NULL;
  268.     ::CoCreateGuid(&m_guid);
  269.     if (m_guid == GUID_NULL)
  270.     {
  271.         AfxMessageBox(IDP_ERR_CREATE_GUID);
  272.         return;
  273.     }
  274.  
  275.     // update dialog to match m_guid
  276.     OnChangedSel(IDC_RADIO1+m_nGuidType);
  277. }
  278.