½ñÌìµÃÏУ¬¸ÕºÃÓÖÒª´¦ÀíOCXÏòϼæÈÝÐÔµÄÎÊÌ⣬ÓÚÊÇ×Ðϸ²é¿´ÁËÒ»ÏÂÔì³ÉÎÊÌâµÄÔÒò£¬×öÁËЩ¼òµ¥²âÊÔ£¬²»´òËã´ÓÔÀí·ÖÎö£¬ÎÒÃÇÔÝÇÒ´Ó±íÏóÉÏ·ÖÎö¡£
[ÎÊÌâÃèÊö] ÔÚʹÓÃMFC¿ò¼ÜÖÆ×÷OCXʱ£¬´æÔÚÏòϼæÈÝÐÔÎÊÌâ¡£ ÔھɿؼþµÄij¸ö½Ó¿ÚÌí¼ÓPropertyʱ£¬ÖØбàÒë¡£Äã»á·¢ÏÖÔÓеĵ÷ÓÃexe³ÌÐò(VB±àÒëËùµÃ)ÔÚµ÷ÓøýӿڵÄMethodʱ»á³ö´í¡£ Why£¿°´Àí£¬Ìí¼ÓÊôÐÔ£¬²»Ó¦¸Ã³öÏÖ¼æÈÝÎÊÌâ¡£ ½«¾ÉÓе÷ÓóÌÐòÔ´Âëµ÷³ö£¬²ÉÓÃÐÂOcx(tlb)¹ØÁª£¬±àÒëºó£¬Ê¹ÓÃÕý³£¡£ µ±È»£¬ÉÏÊö×ö·¨ÏÔÈ»ÓÐÎÊÌ⣬ʵ¼ÊÉÏÈç¹ûµ÷ÓÃÕß²¢²»ÐèҪй¦ÄÜ£¬ËûÊÇûÓÐÒåÎñÀ´×ö´Ë²Ù×÷µÄ¡£ ËùÒÔ£¬ÎÒÃdzÆ֮ΪÏòϼæÈÝÐÔÎÊÌâ¡£ÔÒòºÎÔÚ£¿
[²é×ÊÁÏ] Ïà¹ØÊé½å²¢Î´ÄÜÖ±½ÓÕÒµ½´ð°¸¡£
[·ÖÎöocxÌØÐÔ] ActiveX ControlÊÇÒ»¸öºÜÌØÊâµÄCOM£¬ËüʹÓÃÁË×Ô¶¯»¯¼¼Êõ¡£»òÕߣ¬¸ü¼òµ¥µÄ˵£ºËüʵÏÖÁËIDispatch½Ó¿Ú¡£ÊôÐÔÒÔ¼°·½·¨Ò²µ±È»ÊÇÓÉIDispath¶øÀ´¡£ÎÒÃǵ±È»Ó¦¸Ã´Ó´Ë½Ó¿ÚµÄʵÏÖ×ÅÊÖ¡£IDispatchµÄ¾ßÌåÂÔ¹ý(ÊéÉ϶¼ÓÐ)£¬¿´¿´×î¹Ø¼üµÄInvoke£¬GetIDsOfName¡£ËüÒªÇóÿ¸ö·½·¨/ÊôÐÔ±àºÅ¶¼ÓÐÒ»¸öidºÅ£¬Í¨¹ýÃû³ÆÕÒµ½id£¬È»ºó²éÕÒÏàÓ¦µÄº¯Êý¡£ÕâÊÇIDispatchµÄ±ê×¼¡£ºÃ£¬ÎÒÃÇÔÝʱֻÐèÖªµÀÕâÒ»µã¡£ÆäËüÏȲ»¹Ü¡£
[·ÖÎö mfc¿ò¼Ü´úÂë] ¿´¿´MFC¿ò¼Ü×öÁËЩʲô¡£
µÚ1²½£º ÓÃClassWizardÌí¼ÓÒ»¸öÊôÐÔa(·ÇStock)£¬ àÅ£¬ÔÚʵÏÖÎļþµÄ·Ö·¢Ó³Éä±íÀï¼ÓÁËÈçÏ£º(Í·ÎļþµÄÉùÃ÷ÂÔ) BEGIN_DISPATCH_MAP(MyTest, CCmdTarget) //{{AFX_DISPATCH_MAP(MyTest) DISP_PROPERTY_EX(MyTest, "a", GetA, SetA, VT_I4) //}}AFX_DISPATCH_MAP END_DISPATCH_MAP() ÔÚodlÎļþÀïÌí¼ÓÁË£º properties: [id(1)] long a; ºÃÁË£¬?ÒÉÎÊÀ´ÁË£º ÔÚodlÀïÃæµÄidÖµ1£¬¶øÔÚ·Ö·¢Ó³Éä±íÀïûÓÐidÖµ£¬mfcÈçºÎ¶ÔÓ¦£¿
µÚ2²½£º Ìí¼ÓÒ»¸ö·½·¨m£¬µÃµ½£º DISP_PROPERTY_EX(MyTest, "a", GetA, SetA, VT_I4) DISP_FUNCTION(MyTest, "m", m, VT_EMPTY, VTS_NONE) methods: [id(2)] void m(); ͬÑùÒÉÎÊ£¬ÔÚoldÀïÓÐidÖµ2£¬¶øÔÚ·Ö·¢Ó³Éä±íÀïûÓС£
¶Ï¶¨£º ÎÒÃÇ¿ÉÒÔ³õ²½¶Ï¶¨£¬MFCΪÁË͵ÀÁ£¬½«Ó³Éä±íµÄÏȺó´ÎÐòÓëidÖµ½øÐÐÁËÒþÐÔ¹ØÁª£¬
µÚ3²½£º ΪÁ˽øÒ»²½Ö¤ÊµÕâÒ»µã£ºÎÒ°ÑodlÖеÄm·½·¨µÄid(2)¸ÄΪid(3)£¬·¢ÏÖµ÷ÓÃʧ°Ü£¬ ½«ÊµÏÖÎļþÓ³Éä±íÖÐaÓëmµ÷Ðò£¬»á³öÏÖÀàÐ͵÷ÓÃ´í¡£ Ôٴζ϶¨£º ÔÚOcxÄÚ²¿µÄDispatch idÖµÊÇ°´Ó³Éä±íÖеĴÎÐòÈ·¶¨¡£
µÚ4²½£º ½Ó×Å£¬ÎÒÃÇÀ´ÑéÖ¤ÔÙÌí¼ÓÊôÐÔ³ö´íµÄÎÊÌ⣺ ÔÙÌí¼ÓÊôÐÔb ÔÚʵÏÖÎļþÖеõ½£º DISP_PROPERTY_EX(MyTest, "a", GetA, SetA, VT_I4) DISP_PROPERTY_EX(MyTest, "b", GetB, SetB, VT_I4) DISP_FUNCTION(MyTest, "m", m, VT_EMPTY, VTS_NONE) ÎÒÃÇ¿ÉÒÔÏȲ²âÒ»ÏÂÔÚodlÖеıä¸ü£º aµÄidÓ¦¸ÃÊÇ1,bµÄidÓ¦¸ÃÊÇ2,¶ømµÄidÓ¦¸Ã¸ÄΪÁË3¡£ ´ò¿ªodl£¬¹ûÈ»£º properties: [id(1)] long a; [id(2)] long b; methods: [id(3)] void m(); Ôٵõ½½áÂÛ£º MFCµÄClassWizardÔÚÌí¼ÓocxµÄÊôÐÔ»ò·½·¨Ê±£¬Ê×Ïȱä¸üµÄÊÇËüµÄʵÏÖ(Ó³Éä±í)¼°Í·Îļþ£¬ ˳Ðò°´ÊôÐÔ»ò·½·¨½øÐдóÅÅÐò£¬ÐÂÊôÐÔ¼ÓÔÚÔÓÐÊôÐԵĺóÃ棬з½·¨¼ÓÔÚÔÓз½·¨ºóÃ档Ȼºó£¬ Ëü°´ÕÕÓ³Éä±íÖеÄλÖùØϵ£¬¸üÐÂÁËodlÖеÄÄÚÈÝÒÔ¼°idÖµ¡£ ÀûÓÃMFC¿ò¼ÜÌí¼ÓÊôÐÔ/·½·¨Ê±£¬»á¸Ä±äodlÖз½·¨µÄidÖµ¡£
×ÐϸÊÔÁËһϣº¾ßÌåµÄ´ÎÐòÓ¦¸ÃÊÇ£º °´Notify Property¡¢Property¡¢Function¡¢StockFunc¡¢StockProp˳Ðò
µÚ5²½£º ÎÒÃÇÔÙ¿´¿´StockÊôÐÔÊÇÈçºÎ´¦ÀíµÄ£º Ìí¼ÓÒ»¸öStock TEXTÊôÐÔ¡£ Ó³Éä±í£º DISP_STOCKPROP_TEXT() odl£º [id(DISPID_TEXT), bindable, requestedit] BSTR Text; ÕâËƺõºÍÉÏÊö½áÂÛÓгåÍ»£¬ÇÒÂý£¬ÎÒÃÇ°ÑDISP_STOCKPROP_TEXTÕ¹¿ª£º #define DISP_STOCKPROP_TEXT() \ DISP_PROPERTY_STOCK(COleControl, "Text", DISPID_TEXT, \ COleControl::GetText, COleControl::SetText, VT_BSTR) ¿´µ½Ã»ÓУºDISPID_TEXT£¬ËüÊÇÓÐidÖµµÄ£¬²¢ÇÒͬodlÀïÃæµÄÒ»Ñù¡£ ½áÂÛ£º ÀûÓÃMFC¿ò¼ÜÌí¼ÓStockÊôÐÔ/·½·¨Ê±£¬Ó³Éä±íÖн«´æÔÚÓëodlÏàͬµÄÔ¤¶¨ÒåidÖµ¡£²¢ÇÒ²»Ó°ÏìÔÓÐodlÖеÄidÖµ¡£ µÚ6²½£º ÄÇô£¬¸Ä±äidֵΪʲô»áÔì³ÉÔÓдúÂëÎÞ·¨Õý³£Ê¹ÓÃÄØ¡£ Õâ¸öÊéÉÏÊÇÓеģ¬VB´úÂëÔÚ±àÒ룬¶Ô×Ô¶¯»¯µÄIDispatch½Ó¿ÚÏÔÈ»²ÉÓÃÁËÔç°ó¶¨ÀàÐÍ¿âµÄ ·½Ê½£¬Ò²¾ÍÊÇÔÚ±àÒëʱ½«Ãû³Æת»»ÎªIDÖµ£¬Ò²¾ÍÊÇ˵ִÐÐÎļþÀïÒÑÈ϶¨mµÄµ÷ÓõÄidֵΪ2¡£¶ø ÔÚÐÂocxÖÐ,2ÒѾ²»ÊÇm£¬¶øÊÇÖ¸ÏòеÄÊôÐÔ£¬ÓÉÓÚÀàÐͲ»Æ¥Å䣬µ±È»»á³ö´í¡£ ÎÒÃÇ¿ÉÒÔ×ö¸öʵÀý£º Ìí¼ÓÊôÐÔlong a, ·½·¨ void test1(), void test2() (test1Êä³ö¶Ô»°¿ò"test1",test2Êä³ö¶Ô»°¿ò"test2"). ±àдVBÀý£¬µ÷Óà test2£¬±àÒëΪexe¡£ ÔËÐУ¬µÃµ½"test2"Êä³ö¡£ ÔÙÌí¼ÓÊôÐÔb£¬±àÒëocx¡£ ÔËÐÐÏÈÇ°µÄexe£¬µÃµ½ "test1"Êä³ö¡£ ÖØбàÒëÔ´³ÌÐò£¬µÃµ½¡±test2¡±Êä³ö¡£
½áÂÛ£º VBµÄÔç°ó¶¨£¬ÌáÔ罫idÖµÄÉÈëexeÖУ¬µ±ocxÄÚµÄid·¢Éú±ä¸üʱ£¬»áÔì³É´í룬¼´µ÷ÓôíÎó¡£ (Æäʵ£¬Èç¹ûÄãÓÃVCµÄCOleDipatchDriver·½Ê½µ÷Ó㬻áÓÐͬÑùÎÊÌâ,idÒÑдÈë´úÂë)
×ܽ᣺ ±¾À´£¬°´ÕÕCOM¡¢ Dispatch±ê×¼£¬Ö»ÒªÃû³Æ²»±ä£¬Ó¦¸Ã¾ÍÄÜÕÒµ½ÕýȷλÖã¬idÓ¦¸ÃÊǸöÒþº¬Öµ£¬²»Ó¦³ÉΪ¹Ø¼ü¡£µ«MFCµÄʵÏÖÏÔȻΪÁË͵ÀÁ£¬½«idÓëÓ³Éí±íµÄ´ÎÐò¹ÒÉÏÁ˹³£¬¶øÆäClassWizard£¬ÔÚÌí¼ÓÊôÐÔʱ£¬ÓÖ´íÎóµÄÈ¥±ä¸üÁËÔÓÐodlÖеÄidÖµ¡£ËùÒÔÔì³ÉÒÑÒÀÀµÓÚ¸ÃidÖµ±àÒëµÄ³ÌÐò²»¼æÈÝ¡£
½¨Ò飺 ÓÃMFC±àдOCXʱ£¬Ìí¼ÓÓû§ÊôÐÔ/·½·¨Ê±£¬²»ÒªÊ¹ÓÃClassWizard£¬¶øÊÇÊÖ¹¤ÔÚÓ³Éä±íÖÐÌí¼Ó£¬È»ºó¸ù¾ÝÆäÏà¶ÔλÖã¬ÔÚodlÖÐÌîдidÖµ¡£ Ìí¼ÓStockµÄÊôÐԺͷ½·¨Ò²ÒªÊÖ¹¤Ìí¼Ó£¬ÒòΪËüµÄ±ä¸ü£¬Í¬Ñù»á´¥·¢ClassWiizard¶ÔÓ³Éä±íµÄÅÅÐòºÍodlµÄͬ²½£¬¶øÔì³ÉÄãÒÔÍùµÄÊÖ¹¤±à¼¶ªÊ§¡£ ×îºÃÊǽ«odlÎļþ±äΪÊÜ¿Ø(ÒÔÀûÓÚ±¸·Ý)£¬×¢Òâ²»½ÓÊÜÌí¼ÓÊôÐÔ/·½·¨µÄClassWizardµÄ×Ô¶¯¼ì³ö¡£¼õÉÙÎó²Ù×÷¡£Èç¹û¾³£ÐèÒª±ä¶¯ÊôÐÔ/·½·¨£¬»òÕßÒ²¿ÉÒÔдһ¸ö¼òµ¥µÄshell³ÌÐò£¬À´Ìæ´úClassWizardµÄÏà¹Ø¹¦ÄÜ£¬×Ô¶¯Éú³ÉÏà¹Ø´úÂë¡£
ʯͷ ÓÚ2005-07-12 |