TestCtrlDlg.cpp
上传用户:biuytresa
上传日期:2007-12-07
资源大小:721k
文件大小:8k
源码类别:

DNA

开发平台:

Visual C++

  1. // TestCtrlDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "TestCtrl.h"
  5. #include "TestCtrlDlg.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. // CTestCtrlDlg dialog
  50. CTestCtrlDlg::CTestCtrlDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CTestCtrlDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CTestCtrlDlg)
  54. m_Property = 0;
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. m_pDispatch = NULL;
  59. m_dwCookie = 0;
  60. }
  61. CTestCtrlDlg::~CTestCtrlDlg()
  62. {
  63. if (m_pDispatch != NULL) {
  64. if (m_dwCookie != 0)
  65. Disconnection();
  66. m_pDispatch->Release();
  67. }
  68. }
  69. void CTestCtrlDlg::DoDataExchange(CDataExchange* pDX)
  70. {
  71. CDialog::DoDataExchange(pDX);
  72. //{{AFX_DATA_MAP(CTestCtrlDlg)
  73. DDX_Text(pDX, IDC_EDIT1, m_Property);
  74. //}}AFX_DATA_MAP
  75. }
  76. BEGIN_MESSAGE_MAP(CTestCtrlDlg, CDialog)
  77. //{{AFX_MSG_MAP(CTestCtrlDlg)
  78. ON_WM_SYSCOMMAND()
  79. ON_WM_PAINT()
  80. ON_WM_QUERYDRAGICON()
  81. ON_BN_CLICKED(IDC_CONNECTION, OnConnection)
  82. ON_BN_CLICKED(IDC_DISCONNECTION, OnDisconnection)
  83. ON_BN_CLICKED(IDC_SETPROPERTY, OnSetproperty)
  84. //}}AFX_MSG_MAP
  85. END_MESSAGE_MAP()
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CTestCtrlDlg message handlers
  88. BOOL CTestCtrlDlg::OnInitDialog()
  89. {
  90. CDialog::OnInitDialog();
  91. // Add "About..." menu item to system menu.
  92. // IDM_ABOUTBOX must be in the system command range.
  93. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  94. ASSERT(IDM_ABOUTBOX < 0xF000);
  95. CMenu* pSysMenu = GetSystemMenu(FALSE);
  96. if (pSysMenu != NULL)
  97. {
  98. CString strAboutMenu;
  99. strAboutMenu.LoadString(IDS_ABOUTBOX);
  100. if (!strAboutMenu.IsEmpty())
  101. {
  102. pSysMenu->AppendMenu(MF_SEPARATOR);
  103. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  104. }
  105. }
  106. // Set the icon for this dialog.  The framework does this automatically
  107. //  when the application's main window is not a dialog
  108. SetIcon(m_hIcon, TRUE); // Set big icon
  109. SetIcon(m_hIcon, FALSE); // Set small icon
  110. // TODO: Add extra initialization here
  111. GUID sourceobjCLSID;
  112. HRESULT hResult = ::CLSIDFromProgID(L"SourceComp.SourceObj", &sourceobjCLSID);
  113. if (FAILED(hResult)) 
  114. {
  115. return FALSE;
  116. }
  117. hResult = CoCreateInstance(sourceobjCLSID, NULL, 
  118. CLSCTX_INPROC_SERVER, IID_IDispatch, (void **)&m_pDispatch);
  119. if (FAILED(hResult)) 
  120. {
  121. return FALSE;
  122. }
  123. return TRUE;  // return TRUE  unless you set the focus to a control
  124. }
  125. void CTestCtrlDlg::OnSysCommand(UINT nID, LPARAM lParam)
  126. {
  127. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  128. {
  129. CAboutDlg dlgAbout;
  130. dlgAbout.DoModal();
  131. }
  132. else
  133. {
  134. CDialog::OnSysCommand(nID, lParam);
  135. }
  136. }
  137. // If you add a minimize button to your dialog, you will need the code below
  138. //  to draw the icon.  For MFC applications using the document/view model,
  139. //  this is automatically done for you by the framework.
  140. void CTestCtrlDlg::OnPaint() 
  141. {
  142. if (IsIconic())
  143. {
  144. CPaintDC dc(this); // device context for painting
  145. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  146. // Center icon in client rectangle
  147. int cxIcon = GetSystemMetrics(SM_CXICON);
  148. int cyIcon = GetSystemMetrics(SM_CYICON);
  149. CRect rect;
  150. GetClientRect(&rect);
  151. int x = (rect.Width() - cxIcon + 1) / 2;
  152. int y = (rect.Height() - cyIcon + 1) / 2;
  153. // Draw the icon
  154. dc.DrawIcon(x, y, m_hIcon);
  155. }
  156. else
  157. {
  158. CDialog::OnPaint();
  159. }
  160. }
  161. // The system calls this to obtain the cursor to display while the user drags
  162. //  the minimized window.
  163. HCURSOR CTestCtrlDlg::OnQueryDragIcon()
  164. {
  165. return (HCURSOR) m_hIcon;
  166. }
  167. // {B77C2985-56DD-11CF-B355-00104B08CC22}
  168. static const IID IID_IEventSet =
  169. { 0xb77c2985, 0x56dd, 0x11cf, { 0xb3, 0x55, 0x0, 0x10, 0x4b, 0x8, 0xcc, 0x22 } };
  170. int CTestCtrlDlg::Connection() 
  171. {
  172. BOOL RetValue = 0;
  173. if (m_dwCookie != 0) {
  174. return 2;
  175. }
  176. LPCONNECTIONPOINTCONTAINER pConnPtCont;
  177. if ((m_pDispatch != NULL) &&
  178. SUCCEEDED(m_pDispatch->QueryInterface(IID_IConnectionPointContainer,
  179. (LPVOID*)&pConnPtCont)))
  180. {
  181. ASSERT(pConnPtCont != NULL);
  182. LPCONNECTIONPOINT pConnPt = NULL;
  183. DWORD dwCookie = 0;
  184. if (SUCCEEDED(pConnPtCont->FindConnectionPoint(IID_IEventSet, &pConnPt)))
  185. {
  186. ASSERT(pConnPt != NULL);
  187. pConnPt->Advise(&m_xEventSink, &dwCookie);
  188. m_dwCookie = dwCookie;
  189. RetValue = 1;
  190. pConnPt->Release();
  191. }
  192. pConnPtCont->Release();
  193. m_dwCookie = dwCookie;
  194. }
  195. return RetValue;
  196. }
  197. int CTestCtrlDlg::Disconnection() 
  198. {
  199. BOOL RetValue = 0;
  200. if (m_dwCookie == 0) {
  201. return 2;
  202. }
  203. LPCONNECTIONPOINTCONTAINER pConnPtCont;
  204. if ((m_pDispatch != NULL) &&
  205. SUCCEEDED(m_pDispatch->QueryInterface(IID_IConnectionPointContainer,
  206. (LPVOID*)&pConnPtCont)))
  207. {
  208. ASSERT(pConnPtCont != NULL);
  209. LPCONNECTIONPOINT pConnPt = NULL;
  210. if (SUCCEEDED(pConnPtCont->FindConnectionPoint(IID_IEventSet, &pConnPt)))
  211. {
  212. ASSERT(pConnPt != NULL);
  213. pConnPt->Unadvise(m_dwCookie);
  214. pConnPt->Release();
  215. m_dwCookie = 0;
  216. RetValue = 1;
  217. }
  218. pConnPtCont->Release();
  219. }
  220. return RetValue;
  221. }
  222. void CTestCtrlDlg::OnConnection() 
  223. {
  224. BOOL RetValue = Connection();
  225. if (RetValue == 0) {
  226. AfxMessageBox("Connecting Failed!");
  227. } else if (RetValue == 1) {
  228. AfxMessageBox("Connecting Successful!");
  229. GetDlgItem(IDC_STATIC1)->SetWindowText("连接:建立!");
  230. } else if (RetValue == 2) {
  231. AfxMessageBox("You have a Connection!");
  232. }
  233. return ;
  234. }
  235. void CTestCtrlDlg::OnDisconnection() 
  236. {
  237. BOOL RetValue = Disconnection();
  238. if (RetValue == 0) {
  239. AfxMessageBox("Disconnecting Failed!");
  240. } else if (RetValue == 1) {
  241. AfxMessageBox("Disconnecting Successful!");
  242. GetDlgItem(IDC_STATIC1)->SetWindowText("连接:未建立!");
  243. } else if (RetValue == 2) {
  244. AfxMessageBox("You have not a Connection!");
  245. }
  246. return ;
  247. }
  248. void CTestCtrlDlg::OnSetproperty() 
  249. {
  250. if (!UpdateData(TRUE))
  251. {
  252. TRACE0("UpdateData failed during dialog termination.n");
  253. // the UpdateData routine will set focus to correct item
  254. return;
  255. }
  256. COleDispatchDriver driver;
  257. driver.AttachDispatch(m_pDispatch, FALSE);
  258. TRY
  259. driver.SetProperty(0x1, VT_I4, m_Property);
  260. END_TRY
  261. driver.DetachDispatch();
  262. }
  263. /////////////////////////////////////////////////////////////////////////////
  264. // CTestCtrlDlg::XEventSink
  265. STDMETHODIMP_(ULONG) CTestCtrlDlg::XEventSink::AddRef()
  266. {
  267. return 1;
  268. }
  269. STDMETHODIMP_(ULONG) CTestCtrlDlg::XEventSink::Release()
  270. {
  271. return 0;
  272. }
  273. STDMETHODIMP CTestCtrlDlg::XEventSink::QueryInterface(
  274. REFIID iid, LPVOID* ppvObj)
  275. {
  276. METHOD_PROLOGUE_EX_(CTestCtrlDlg, EventSink)
  277. if (IsEqualIID(iid, IID_IUnknown) ||
  278. IsEqualIID(iid, IID_IDispatch) ||
  279. IsEqualIID(iid, IID_IEventSet))
  280. {
  281. *ppvObj = this;
  282. AddRef();
  283. return S_OK;
  284. }
  285. else
  286. {
  287. return E_NOINTERFACE;
  288. }
  289. }
  290. STDMETHODIMP CTestCtrlDlg::XEventSink::GetTypeInfoCount(
  291. unsigned int*)
  292. {
  293. return E_NOTIMPL;
  294. }
  295. STDMETHODIMP CTestCtrlDlg::XEventSink::GetTypeInfo(
  296. unsigned int, LCID, ITypeInfo**)
  297. {
  298. return E_NOTIMPL;
  299. }
  300. STDMETHODIMP CTestCtrlDlg::XEventSink::GetIDsOfNames(
  301. REFIID, LPOLESTR*, unsigned int, LCID, DISPID*)
  302. {
  303. return E_NOTIMPL;
  304. }
  305. STDMETHODIMP CTestCtrlDlg::XEventSink::Invoke(
  306. DISPID dispid, REFIID, LCID, unsigned short wFlags,
  307. DISPPARAMS* pDispParams, VARIANT* pvarResult,
  308. EXCEPINFO* pExcepInfo, unsigned int* puArgError)
  309. {
  310. if (dispid == 0) {
  311. AfxMessageBox("The Property has been changed!");
  312. } else {
  313. AfxMessageBox("I don't known the event!");
  314. }
  315. return S_OK;
  316. }