MFC(ActiveX編程,孫鑫C++第十八講筆記整理)

一:VB中調用X控件,添加方法 project->Add components。另外能夠用Object Browser來查看控件函數

 

二:VC中建立X控件工具

1.新建一個X工程名爲Clock,注意一個文件中能夠包含多個控件。測試

2.保持缺省設置,完成。注意它生成的三個類,以及相關的接口。ui

3.運行它。選擇TSTCON32.exe做爲容器。spa

4.選擇Insert Control,此時咱們能夠看到,它畫了一個橢圓。也能夠在VB中測試。.net

5.刪除註冊信息。用regsvr32 /u +文件名。也能夠在菜單選擇反註冊命令。設計

6.重寫代碼。在CClockCtrl::OnDraw()中畫了一個橢圓,此時咱們在其中獲得系統時間,並顯示它。爲此咱們在OnCreate()設置了一個定時器,每隔必定時間發出一個Invalidate()消息,使窗口重繪。code

7.如何改變控件的背景色和前景色?ClassWizard->AutoMation->Add Property->BackColor,還須要在OnDraw()中加上相應的代碼component

CBrush brush(TranslateColor(GetBackColor()));orm

pdc->FillRect(rcBounds, &brush);

pdc->SetBkMode(TRANSPARENT);

pdc->SetTextColor(TranslateColor(GetForeColor()));

8.增長屬性頁。在

BEGIN_PROPPAGEIDS(CClockCtrl, 2)此時數目也得改爲相應的數目

PROPPAGEID(CClockPropPage::guid)

PROPPAGEID(CLSID_CColorPropPage)

END_PROPPAGEIDS(CClockCtrl)  OK~

9.增長自定義屬性:ClassWizard->AutoMation->Add Property加上一個變量m_interval,類型爲short,對應外部變量爲Interval。在CClockCtrl中增長OnIntervalChanged方法。添加以下代碼:

   if(m_interval<0 || m_interval>6000)

 {

  m_interval=1000;

 }

 else

 {

  m_interval=m_interval/1000*1000;

  KillTimer(1);

  SetTimer(1,m_interval,NULL);

  BoundPropertyChanged(0x1);

 }

 10.測試:Control->Invoke Methods

 11.將時間間隔加到屬性頁中,在資源視圖中加入一文本框和編輯框。爲EditBox關聯成員變量,加入屬性interval

 12.增長方法:ClassWizard->AutoMation->Add Method->Hello加入代碼 OK!在VB中能夠調用此方法!

   void CClockCtrl::Hello()

{

 // TOD Add your dispatch handler code here

 MessageBox("Hello world!");

}

 13.增長事件:ClassWizard->AutoMation->Add Events->Click

 14.增長自定義事件:ClassWizard->AutoMation->Add Events->NewMinute

      在新的一分鐘到達時發出這個通知,在OnDraw()中寫代碼:

       CTime time=CTime::GetCurrentTime();

 if(0==time.GetSecond())

 {

  FireNewMinute();

 }

15.Interval屬性具備持久性。在CClockCtrl::DoPropExchange()中調用PX_short()方法,OK

    PX_Short(pPX,"Interval",m_interval,1000);

16.Property PageProperty屬性中的interval保持一致的方法:在OnIntervalChanged()中調用BoundPropertyChanged(0x1);

17.但願控件在設計時間內不走動的方法:在OnTimer()中,if(AmbientUserMode())InvalidateControl();巧妙!

 

 

三:VC中調用X控件

1.新建ClockTest對話框應用程序

2.點擊右鍵->插入X控件->時鐘控件

3.Project->Add Component會生成CClock類。

4.CCLockTestDlg中增長CClock類的成員變量m_clock,而後能夠動態建立一個這樣的東東!

5.試驗Click(),NewMinute(),SetBkColor(),SetForeColor()方法和屬性

6.如何爲動態建立的控件作事件響應呢?首先你得知道它的ID號,而後參考非動態的控件事件代碼。

 

下面是具體的代碼:

1建立一個MFC ActiveX ControWizard項目,什麼都不用作,直接運行,就能有一個Activex控件了,其做用是生成一個橢圓

運行,直接註冊到了系統,因此在別的地方可以找到生成的ActiveX控件

這裏在VS2008中的VB來測試。

右鍵---->選擇項---->

 

此時工具箱就多了一個控件

拖動到窗口,效果以下:

項目文件Debug中含有:

 

在運行 輸入

而後把上面的ocx文件拖進去,按肯定,反編譯,使得從註冊表中移除

 

 

而後運行MFCXXX,找不到ActiveX找不到控件

 

使用Tools下的Register Contrl能夠註冊ActiveX

或者運行 regsvr32  再把ocx文件拖進來 就能夠註冊了,跟上面同樣,只是去掉了/u

 

下面開始作本身的|ActiveX了

[cpp] view plaincopy

  1. void CATestCtrl::OnDraw(  

  2.             CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)  

  3. {  

  4.     // TODO: Replace the following code with your own drawing code.  

  5.     //pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));  

  6.     //pdc->Ellipse(rcBounds);  

  7.     //上面兩句是默認的,畫橢圓  

  8.     CTime c=CTime::GetCurrentTime();  

  9.     CString cstring=c.Format("%H:%M:%S");  

  10.     pdc->TextOut(0,0,cstring);  

  11. }  


一個靜止的時間,要想它動態的顯示,還得加上SetTimer函數和wm_timer

在CXXctrl中添加一個WM_CREATE而後裏面Settimer

SetTimer(1,1000,NULL);

添加wm_timer

[cpp] view plaincopy

  1. void CATestCtrl::OnTimer(UINT nIDEvent)   

  2. {  

  3.     // TODO: Add your message handler code here and/or call default  

  4.       

  5.     //Invalidate();  

  6.     InvalidateControl();  

  7.     COleControl::OnTimer(nIDEvent);  

  8. }  


這個時候就能顯示一個動態顯示的時鐘了

 

在VB中使用剛剛新建的ActiveX,發現只有不多的屬性

這個時候  咱們能夠添加本身的屬性:

 

這個時候,VS中VB能夠顯示響應的屬性了

 

此時在OnDraw中 要相應的添加代碼,要否則,設置了背景色和前景色都不起做用

[cpp] view plaincopy

  1. CBrush cbrush(TranslateColor(GetBackColor()));  

  2.     pdc->FillRect(rcBounds,&cbrush);  

  3.     pdc->SetBkMode(TRANSPARENT);  

  4.     pdc->SetTextColor(TranslateColor(GetForeColor()));  


 

[cpp] view plaincopy

  1. void CATestCtrl::OnDraw(  

  2.             CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)  

  3. {  

  4.     // TODO: Replace the following code with your own drawing code.  

  5.     //pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));  

  6.     //pdc->Ellipse(rcBounds);  

  7.     //上面兩句是默認的,畫橢圓  

  8.   

  9.   

  10.     CBrush cbrush(TranslateColor(GetBackColor()));  

  11.     pdc->FillRect(rcBounds,&cbrush);  

  12.     pdc->SetBkMode(TRANSPARENT);  

  13.     pdc->SetTextColor(TranslateColor(GetForeColor()));  

  14.   

  15.     CTime c=CTime::GetCurrentTime();  

  16.     CString cstring=c.Format("%H:%M:%S");  

  17.     pdc->TextOut(0,0,cstring);  

  18. }  


手動添加屬性頁

[cpp] view plaincopy

  1. BEGIN_PROPPAGEIDS(CATestCtrl, 2)  

  2.     PROPPAGEID(CATestPropPage::guid)  

  3.     PROPPAGEID(CLSID_CColorPropPage)  

  4. END_PROPPAGEIDS(CATestCtrl)  


 

 

添加自定義屬性:

 

[cpp] view plaincopy

  1. void CATestCtrl::OnJianGeChanged()   

  2. {  

  3.     // TODO: Add notification handler code  

  4.   

  5.     if(m_jianGe<0||m_jianGe>6000)  

  6.     {  

  7.         m_jianGe=1000;  

  8.     }  

  9.     else  

  10.     {  

  11.         m_jianGe=m_jianGe/1000*1000;  

  12.         KillTimer(1);  

  13.         SetTimer(1,m_jianGe,NULL);  

  14.     }  

  15.     SetModifiedFlag();  

  16. }  


 

屬性面板的 有的能看間JianGe有的不能看到

將這個添加到屬性頁去

添加關聯

 

最後一項是屬性名

 

 能夠爲ActiveX添加(標準的和自定義的)事件和函數。

 

 MFC中添加該類

相關文章
相關標籤/搜索