testDlg.cpp
上传用户:weisheen
上传日期:2022-07-09
资源大小:19390k
文件大小:6k
源码类别:

ActiveX/DCOM/ATL

开发平台:

Visual C++

  1. // testDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "test.h"
  5. #include "testDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CTestDlg dialog
  50. CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CTestDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CTestDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. }
  59. void CTestDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61. CDialog::DoDataExchange(pDX);
  62. //{{AFX_DATA_MAP(CTestDlg)
  63. // NOTE: the ClassWizard will add DDX and DDV calls here
  64. //}}AFX_DATA_MAP
  65. }
  66. BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
  67. //{{AFX_MSG_MAP(CTestDlg)
  68. ON_WM_SYSCOMMAND()
  69. ON_WM_PAINT()
  70. ON_WM_QUERYDRAGICON()
  71. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  72. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  73. ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  74. ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
  75. //}}AFX_MSG_MAP
  76. END_MESSAGE_MAP()
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CTestDlg message handlers
  79. BOOL CTestDlg::OnInitDialog()
  80. {
  81. CDialog::OnInitDialog();
  82. // Add "About..." menu item to system menu.
  83. // IDM_ABOUTBOX must be in the system command range.
  84. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  85. ASSERT(IDM_ABOUTBOX < 0xF000);
  86. CMenu* pSysMenu = GetSystemMenu(FALSE);
  87. if (pSysMenu != NULL)
  88. {
  89. CString strAboutMenu;
  90. strAboutMenu.LoadString(IDS_ABOUTBOX);
  91. if (!strAboutMenu.IsEmpty())
  92. {
  93. pSysMenu->AppendMenu(MF_SEPARATOR);
  94. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  95. }
  96. }
  97. // Set the icon for this dialog.  The framework does this automatically
  98. //  when the application's main window is not a dialog
  99. SetIcon(m_hIcon, TRUE); // Set big icon
  100. SetIcon(m_hIcon, FALSE); // Set small icon
  101. // TODO: Add extra initialization here
  102. return TRUE;  // return TRUE  unless you set the focus to a control
  103. }
  104. void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
  105. {
  106. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  107. {
  108. CAboutDlg dlgAbout;
  109. dlgAbout.DoModal();
  110. }
  111. else
  112. {
  113. CDialog::OnSysCommand(nID, lParam);
  114. }
  115. }
  116. // If you add a minimize button to your dialog, you will need the code below
  117. //  to draw the icon.  For MFC applications using the document/view model,
  118. //  this is automatically done for you by the framework.
  119. void CTestDlg::OnPaint() 
  120. {
  121. if (IsIconic())
  122. {
  123. CPaintDC dc(this); // device context for painting
  124. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  125. // Center icon in client rectangle
  126. int cxIcon = GetSystemMetrics(SM_CXICON);
  127. int cyIcon = GetSystemMetrics(SM_CYICON);
  128. CRect rect;
  129. GetClientRect(&rect);
  130. int x = (rect.Width() - cxIcon + 1) / 2;
  131. int y = (rect.Height() - cyIcon + 1) / 2;
  132. // Draw the icon
  133. dc.DrawIcon(x, y, m_hIcon);
  134. }
  135. else
  136. {
  137. CDialog::OnPaint();
  138. }
  139. }
  140. // The system calls this to obtain the cursor to display while the user drags
  141. //  the minimized window.
  142. HCURSOR CTestDlg::OnQueryDragIcon()
  143. {
  144. return (HCURSOR) m_hIcon;
  145. }
  146. #include "../StructDemo.h"
  147. #include "../StructDemo_i.c"
  148. void CTestDlg::OnButton1() 
  149. {
  150. // TODO: Add your control notification handler code here
  151. ::CoInitialize(NULL);
  152. IMyStruct* pTools=NULL;
  153. ::CoCreateInstance(CLSID_MyStruct,NULL,CLSCTX_INPROC_SERVER,IID_IMyStruct,(void **)&pTools);
  154. StPerson* pPer=NULL;
  155. pTools->GetPersonById(1,&pPer);
  156. CString strName(pPer->name);
  157. ::SysFreeString(pPer->name);
  158. CString strSex(pPer->sex);
  159. ::SysFreeString(pPer->sex);
  160. ::CoTaskMemFree(pPer);
  161. ::CoUninitialize();
  162. }
  163. void CTestDlg::OnButton2() 
  164. {
  165. // TODO: Add your control notification handler code here
  166. ::CoInitialize(NULL);
  167. IMyStruct* pTools=NULL;
  168. ::CoCreateInstance(CLSID_MyStruct,NULL,CLSCTX_INPROC_SERVER,IID_IMyStruct,(void **)&pTools);
  169. StPerson* pPer=NULL;
  170. UINT iSize=0;
  171. pTools->GetAllPerson(&iSize,&pPer);
  172. for(int i=0;i<iSize;i++)
  173. {
  174. CString strTemp((pPer+i)->name);
  175. ::SysFreeString((pPer+i)->name);
  176. MessageBox(strTemp);
  177. }
  178. ::CoTaskMemFree(pPer);
  179. ::CoUninitialize();
  180. }
  181. void CTestDlg::OnButton3() 
  182. {
  183. // TODO: Add your control notification handler code here
  184. ::CoInitialize(NULL);
  185. IMyStruct* pTools=NULL;
  186. ::CoCreateInstance(CLSID_MyStruct,NULL,CLSCTX_INPROC_SERVER,IID_IMyStruct,(void **)&pTools);
  187.     StPerson* Ref=new StPerson[2];
  188. Ref[0].age=12;
  189. Ref[1].age=13;
  190. UINT iResult=0;
  191. pTools->SavePerson(2,Ref,&iResult);
  192. }
  193. void CTestDlg::OnButton5() 
  194. {
  195. // TODO: Add your control notification handler code here
  196. ::CoInitialize(NULL);
  197. IMyStruct* pTools=NULL;
  198. ::CoCreateInstance(CLSID_MyStruct,NULL,CLSCTX_INPROC_SERVER,IID_IMyStruct,(void **)&pTools);
  199. CString str("zhangsan");
  200. pTools->put_UID(str.AllocSysString());
  201. BSTR bt;
  202. pTools->get_UID(&bt);
  203. CString strName(bt);
  204. ::SysFreeString(bt);
  205. MessageBox(strName);
  206. }