在vb的屬性檢視中,還有乙個有意思的功能是將屬性列在不同的類別,如,外觀,行為,資料等等。這種功能是怎麼來的,我自己的控制項中的自定義的屬性是不是也能列在這些類別中,我能不能建立自己的類別呢。
很簡單,在你的控制項中實現乙個介面icategorizeproperties就可以了,這個介面只有兩個方法
hresult getcategoryname(propcat propcat, lcid lcid, bstr* pbstrname);
propcat實際上就是一int,表示某種類別編號
getcategoryname應是用來設定你自定義的類別(編號為propcat)的顯示字串的
看起來似乎挺簡單的,可惜的是,這個介面沒有定義,colecontrol也沒有幫我們做任何的工作。幸好,對於新增介面,我們已經有了經驗了,前面的文章有過許多例子了。
我們有上一文中的例子tppb
1.在.odl上新增新介面定義
// icategorizeproperties介面定義
[ uuid(4d07fc10-f931-11ce-b001-00aa006884e5),
helpstring("icategorizeproperties inte***ce"), pointer_default (unique) ]
inte***ce icategorizeproperties : iunknown ;
2.新增巢狀類,作為icategorizeproperties介面的實現類
a.在tppbctl.h中新增宣告
......
declare_event_map()
public:
begin_inte***ce_part(categorizeproperties, icategorizeproperties)
stdmethod(getcategoryname)(propcat propcat, lcid lcid, bstr* pbstrname);
end_inte***ce_part(categorizeproperties)
declare_inte***ce_map()
// dispatch and event ids
public:
enum }afx_disp_id };
......
b.在tppbctl.cpp中新增實現**
stdmethodimp_(ulong) ctppbctrl::xcategorizeproperties::addref()
stdmethodimp_(ulong) ctppbctrl::xcategorizeproperties::release()
stdmethodimp ctppbctrl::xcategorizeproperties::queryinte***ce(refiid iid, lpvoid* ppvobj)
else if(dispid == dispidparam)
return e_fail; }
stdmethodimp ctppbctrl::xcategorizeproperties::getcategoryname(propcat propcat, lcid lcid, bstr* pbstrname)
return e_fail; }
注:這裡將week屬性列入了資料類別(propcat_data)中,將param屬性列入自定義的類別"引數"中,類別編號為1,對於自定義的類別編號,必須大於0
3.將icategorizeproperties介面新增到控制項的介面對映表中
a.在tppbctl.h中新增介面對映宣告
public:
begin_inte***ce_part(categorizeproperties, icategorizeproperties)
stdmethod(getcategoryname)(propcat propcat, lcid lcid, bstr* pbstrname);
end_inte***ce_part(categorizeproperties)
declare_inte***ce_map()
b.在tppbctl.cpp中新增icategorizeproperties到對映表中
begin_inte***ce_map(ctppbctrl, colecontrol)
inte***ce_part(ctppbctrl, iid_idispatch, dispatch)
inte***ce_part(ctppbctrl, diid__dtppb, dispatch)
inte***ce_part(ctppbctrl, iid_icategorizeproperties, categorizeproperties)
end_inte***ce_map()
4.現在編譯的話,會發現少了很多的定義,需要從.odl中生成相應的介面定義檔案,在檔案列表中找到tppb.odl,點右鍵,選擇settings,在output header file name中輸入tppbuuid.h(不存在的任何檔名都可),確定後,再點右鍵,選擇compile tppb.odl,即生成tppbuuid.h檔案了。
5.在tppbctl.h檔案中#include "tppbuuid.h"
6.新建一initiids.cpp檔案,輸入**如下:
#include
#include
#include "tppbuuid.h"
在檔案列表中,找到initiids.cpp,點右鍵,在c/c++項中,選擇category為precompiled headers,並設定為not using precompiled headers
7.編譯,如果鏈結出現dllmain之類的錯誤,請移除initiids.cpp,然後再加入,並重新設定precompiled headers,再編譯即可。
8.好了,在vb下測試就可以了
ActiveX控制項的MFC設計之旅
在csdn中碰到了好幾個問使用mfc設計activex控制項的朋友,聯想到自己以前也幹過這事,突然想寫些文章,介紹介紹我所知道的一些設計技巧。無奈,千頭萬緒,不知如何著手,最後,決定乾脆設計一控制項,將在控制項中碰到的一些問題,一些方法寫出來。想一想,就設計一網格控制項吧,取名為litegrid。忘...
MFC中ActiveX控制項的使用
windows系的開發做的很少,也暈乎暈乎的。在mfc中新增上mapcontrol的控制項之後。手動新增乙個imapcontrol2ptr型別的變數,然後用手動新增的變數再去手動的與控制項關聯,才能進一步呼叫或者設定控制項的方法和屬性。我上一次使用mfc是什麼時候來著?貌似很久遠了。不過我依稀記得,...
ActiveX控制項的MFC設計之旅 第4步
那麼這回就來看一下網上說的挺多的傳遞自定義結構的問題吧,這個問題在早期不支援dcom的作業系統中是沒辦法解決的,不過就目前的流行作業系統來說,應該是不成問題的。第乙個是微軟的msdn,應該蠻正宗的吧,http windowssdk.msdn.microsoft.com library default...