PropPageCntrDlg.cpp
上传用户:lc8096
上传日期:2007-01-02
资源大小:40k
文件大小:8k
源码类别:

ActiveX/DCOM/ATL

开发平台:

Visual C++

  1. // PropPageCntrDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "PropPageCntr.h"
  5. #include "PropPageCntrDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CPropPageCntrDlg dialog
  13. CPropPageCntrDlg::CPropPageCntrDlg(CWnd* pParent /*=NULL*/)
  14. : CDialog(CPropPageCntrDlg::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CPropPageCntrDlg)
  17. // NOTE: the ClassWizard will add member initialization here
  18. //}}AFX_DATA_INIT
  19. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  20. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  21. }
  22. void CPropPageCntrDlg::DoDataExchange(CDataExchange* pDX)
  23. {
  24. CDialog::DoDataExchange(pDX);
  25. //{{AFX_DATA_MAP(CPropPageCntrDlg)
  26. DDX_Control(pDX, IDC_RESULTS, m_wndListResults);
  27. DDX_Control(pDX, IDC_CONTROL, m_wndComboControl);
  28. DDX_Control(pDX, IDC_STATIC_PROPPAGE, m_wndStaticPropPage);
  29. //}}AFX_DATA_MAP
  30. }
  31. BEGIN_MESSAGE_MAP(CPropPageCntrDlg, CDialog)
  32. //{{AFX_MSG_MAP(CPropPageCntrDlg)
  33. ON_WM_PAINT()
  34. ON_WM_QUERYDRAGICON()
  35. ON_WM_DESTROY()
  36. ON_BN_CLICKED(IDC_SHOW, OnShow)
  37. ON_CBN_SELENDOK(IDC_CONTROL, OnSelendokControl)
  38. //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40. BEGIN_INTERFACE_MAP(CPropPageCntrDlg, CDialog)
  41.     INTERFACE_PART(CPropPageCntrDlg, IID_IPropertyPageSite, PropertyPageSite)
  42. END_INTERFACE_MAP()
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CPropPageCntrDlg message handlers
  45. BOOL CPropPageCntrDlg::OnInitDialog()
  46. {
  47. CDialog::OnInitDialog();
  48. // Set the icon for this dialog.  The framework does this automatically
  49. //  when the application's main window is not a dialog
  50. SetIcon(m_hIcon, TRUE); // Set big icon
  51. SetIcon(m_hIcon, FALSE); // Set small icon
  52. // TODO: Add extra initialization here
  53. CWnd *pWndControl = NULL;
  54. int nIndex = 0;
  55. pWndControl = new CWnd();
  56. pWndControl->CreateControl( _T("{A56A23D3-79AF-11D2-8DC4-C36D152E46FE}"), NULL, 0, CRect( 0, 0, 0, 0 ), this, -1 );
  57. nIndex = m_wndComboControl.AddString( _T("MFC Control") );
  58. m_wndComboControl.SetItemDataPtr( nIndex, pWndControl );
  59. pWndControl = new CWnd();
  60. pWndControl->CreateControl( _T("{A56A23F2-79AF-11D2-8DC4-C36D152E46FE}"), NULL, 0, CRect( 0, 0, 0, 0 ), this, -1 );
  61. nIndex = m_wndComboControl.AddString( _T("ATL Control") );
  62. m_wndComboControl.SetItemDataPtr( nIndex, pWndControl );
  63. m_wndComboControl.SetCurSel(0);
  64. OnSelendokControl();
  65. return TRUE;  // return TRUE  unless you set the focus to a control
  66. }
  67. // If you add a minimize button to your dialog, you will need the code below
  68. //  to draw the icon.  For MFC applications using the document/view model,
  69. //  this is automatically done for you by the framework.
  70. void CPropPageCntrDlg::OnPaint() 
  71. {
  72. if (IsIconic())
  73. {
  74. CPaintDC dc(this); // device context for painting
  75. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  76. // Center icon in client rectangle
  77. int cxIcon = GetSystemMetrics(SM_CXICON);
  78. int cyIcon = GetSystemMetrics(SM_CYICON);
  79. CRect rect;
  80. GetClientRect(&rect);
  81. int x = (rect.Width() - cxIcon + 1) / 2;
  82. int y = (rect.Height() - cyIcon + 1) / 2;
  83. // Draw the icon
  84. dc.DrawIcon(x, y, m_hIcon);
  85. }
  86. else
  87. {
  88. CDialog::OnPaint();
  89. }
  90. }
  91. // The system calls this to obtain the cursor to display while the user drags
  92. //  the minimized window.
  93. HCURSOR CPropPageCntrDlg::OnQueryDragIcon()
  94. {
  95. return (HCURSOR) m_hIcon;
  96. }
  97. BOOL CPropPageCntrDlg::SetCurrentPage( IUnknown * pUnknown, LPRECT rc )
  98. {
  99. HRESULT hr = NOERROR;
  100. try
  101. {
  102. // hide previous page and release page pointer
  103. if( m_pCurrentPage != NULL )
  104. {
  105. m_pCurrentPage->Show( SW_HIDE );
  106. m_pCurrentPage->Deactivate();
  107. m_pCurrentPage = NULL;
  108. if( pUnknown == NULL ) return TRUE;
  109. }
  110. ISpecifyPropertyPagesPtr pSpecifyPropertyPages = pUnknown;
  111. CAUUID pages;
  112. hr = pSpecifyPropertyPages->GetPages( &pages );
  113. if( FAILED( hr ) ) throw _com_error( hr );
  114. ASSERT( pages.cElems > 0 && pages.pElems != NULL );
  115. IPropertyPagePtr pPropPage;
  116. hr = CoCreateInstance( pages.pElems[0], NULL, CLSCTX_INPROC, IID_IPropertyPage, (void**)&pPropPage );
  117. if( FAILED( hr ) ) throw _com_error( hr );
  118. hr = pPropPage->SetPageSite( (IPropertyPageSite*) GetInterface( &IID_IPropertyPageSite ) );
  119. if( FAILED( hr ) ) throw _com_error( hr );
  120. hr = pPropPage->SetObjects( 1, &pUnknown );
  121. if( FAILED( hr ) ) throw _com_error( hr );
  122. hr = pPropPage->Activate( GetSafeHwnd(), rc, TRUE );
  123. if( FAILED( hr ) ) throw _com_error( hr );
  124. hr = pPropPage->Show( SW_SHOW );
  125. if( FAILED( hr ) ) throw _com_error( hr );
  126. m_pCurrentPage = pPropPage;
  127. }
  128. catch( _com_error &e )
  129. {
  130. hr = e.Error();
  131. ASSERT( SUCCEEDED( hr ) );
  132. }
  133. return SUCCEEDED( hr );
  134. }
  135. /////////////////////////////////////////////////////////////////////////////
  136. // CPropPageCntrDlg COM interface implementation
  137. STDMETHODIMP_( ULONG ) CPropPageCntrDlg::XPropertyPageSite::AddRef()
  138. {
  139.     METHOD_PROLOGUE( CPropPageCntrDlg, PropertyPageSite )
  140. return pThis->ExternalAddRef();
  141. }
  142. STDMETHODIMP_( ULONG ) CPropPageCntrDlg::XPropertyPageSite::Release()
  143. {
  144.     METHOD_PROLOGUE( CPropPageCntrDlg, PropertyPageSite )
  145. return pThis->ExternalRelease();
  146. }
  147. STDMETHODIMP CPropPageCntrDlg::XPropertyPageSite::QueryInterface( REFIID iid, void FAR* FAR* ppvObj)
  148. {
  149.     METHOD_PROLOGUE( CPropPageCntrDlg, PropertyPageSite )
  150.     return (HRESULT)pThis->ExternalQueryInterface( &iid, ppvObj );
  151. }
  152. STDMETHODIMP CPropPageCntrDlg::XPropertyPageSite::OnStatusChange( DWORD dwFlags )
  153. {
  154.     METHOD_PROLOGUE( CPropPageCntrDlg, PropertyPageSite )
  155. return NOERROR;
  156. }
  157. STDMETHODIMP CPropPageCntrDlg::XPropertyPageSite::GetLocaleID( LCID *pLocaleID )
  158. {
  159.     METHOD_PROLOGUE( CPropPageCntrDlg, PropertyPageSite )
  160. *pLocaleID = GetThreadLocale();
  161. return NOERROR;
  162. }
  163. STDMETHODIMP CPropPageCntrDlg::XPropertyPageSite::GetPageContainer( IUnknown **ppUnk )
  164. {
  165.     METHOD_PROLOGUE( CPropPageCntrDlg, PropertyPageSite )
  166. return E_FAIL;
  167. }
  168. STDMETHODIMP CPropPageCntrDlg::XPropertyPageSite::TranslateAccelerator( MSG *pMsg )
  169. {
  170.     METHOD_PROLOGUE( CPropPageCntrDlg, PropertyPageSite )
  171. return pThis->PreTranslateMessage( pMsg ) ? S_OK : S_FALSE;
  172. }
  173. void CPropPageCntrDlg::OnDestroy() 
  174. {
  175. // TODO: Add your message handler code here
  176. SetCurrentPage( NULL, NULL );
  177. for( int nIndex = 0; nIndex < m_wndComboControl.GetCount(); nIndex++ )
  178. {
  179. CWnd *pWndControl = (CWnd*) m_wndComboControl.GetItemDataPtr( nIndex );
  180. pWndControl->DestroyWindow();
  181. delete pWndControl;
  182. }
  183. CDialog::OnDestroy();
  184. }
  185. void CPropPageCntrDlg::OnShow() 
  186. {
  187. // TODO: Add your control notification handler code here
  188. int nIndex = m_wndComboControl.GetCurSel();
  189. if( nIndex == -1 ) return;
  190. CWnd *pWndControl = (CWnd*) m_wndComboControl.GetItemDataPtr( nIndex );
  191. if( pWndControl == NULL ) return;
  192. ASSERT( m_pCurrentPage != NULL );
  193. HRESULT hr = m_pCurrentPage->Apply();
  194. if( FAILED( hr ) ) return;
  195. pWndControl->InvokeHelper( 1, DISPATCH_METHOD, VT_EMPTY, NULL, NULL ); 
  196. }
  197. void CPropPageCntrDlg::OnSelendokControl() 
  198. {
  199. // TODO: Add your control notification handler code here
  200. int nIndex = m_wndComboControl.GetCurSel();
  201. if( nIndex == -1 ) return;
  202. CWnd *pWndControl = (CWnd*) m_wndComboControl.GetItemDataPtr( nIndex );
  203. if( pWndControl == NULL ) return;
  204. CRect rtPropPage;
  205. m_wndStaticPropPage.GetWindowRect( &rtPropPage );
  206. ScreenToClient( &rtPropPage );
  207.    SetCurrentPage( pWndControl->GetControlUnknown(), rtPropPage );
  208. m_wndListResults.ResetContent();
  209. }
  210. BEGIN_EVENTSINK_MAP(CPropPageCntrDlg, CDialog)
  211.     //{{AFX_EVENTSINK_MAP(CPropPageCntrDlg)
  212. ON_EVENT(CPropPageCntrDlg, -1, 1 /* ObjectFound */, OnObjectFound, VTS_BSTR)
  213. //}}AFX_EVENTSINK_MAP
  214. END_EVENTSINK_MAP()
  215. void CPropPageCntrDlg::OnObjectFound(LPCTSTR szObjectID) 
  216. {
  217. // TODO: Add your control notification handler code here
  218. m_wndListResults.AddString( szObjectID ); 
  219. }