锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久久这里都是精品,久久精品国产福利国产琪琪,久久中文字幕人妻熟av女http://www.shnenglu.com/pjqblues/zh-cnSun, 11 May 2025 07:54:25 GMTSun, 11 May 2025 07:54:25 GMT60浣跨敤鏍囧噯C++鐨勭被鍨嬭漿鎹㈢錛歴tatic_cast銆乨ynamic_cast銆乺einterdivt_cast銆佸拰const_casthttp://www.shnenglu.com/pjqblues/archive/2007/10/23/34945.htmlbluesbluesTue, 23 Oct 2007 12:28:00 GMThttp://www.shnenglu.com/pjqblues/archive/2007/10/23/34945.htmlhttp://www.shnenglu.com/pjqblues/comments/34945.htmlhttp://www.shnenglu.com/pjqblues/archive/2007/10/23/34945.html#Feedback0http://www.shnenglu.com/pjqblues/comments/commentRss/34945.htmlhttp://www.shnenglu.com/pjqblues/services/trackbacks/34945.html浣跨敤鏍囧噯C++鐨勭被鍨嬭漿鎹㈢錛?strong style="color: black; background-color: #ffff66;">static_cast銆乨ynamic_cast銆乺einterdivt_cast銆佸拰const_cast銆?/p>

3.1 static_cast
鐢ㄦ硶錛?strong style="color: black; background-color: #ffff66;">static_cast < type-id > ( exdivssion )
璇ヨ繍綆楃鎶奺xdivssion杞崲涓簍ype-id綾誨瀷錛屼絾娌℃湁榪愯鏃剁被鍨嬫鏌ユ潵淇濊瘉杞崲鐨勫畨鍏ㄦс傚畠涓昏鏈夊涓嬪嚑縐嶇敤娉曪細(xì)
鈶犵敤浜庣被灞傛緇撴瀯涓熀綾誨拰瀛愮被涔嬮棿鎸囬拡鎴栧紩鐢ㄧ殑杞崲銆?br>銆銆榪涜涓婅杞崲錛堟妸瀛愮被鐨勬寚閽堟垨寮曠敤杞崲鎴愬熀綾昏〃紺猴級(jí)鏄畨鍏ㄧ殑錛?br>銆銆榪涜涓嬭杞崲錛堟妸鍩虹被鎸囬拡鎴栧紩鐢ㄨ漿鎹㈡垚瀛愮被琛ㄧず錛夋椂錛岀敱浜庢病鏈夊姩鎬佺被鍨嬫鏌ワ紝鎵浠ユ槸涓嶅畨鍏ㄧ殑銆?br>鈶$敤浜庡熀鏈暟鎹被鍨嬩箣闂寸殑杞崲錛屽鎶奿nt杞崲鎴恈har錛屾妸int杞崲鎴恊num銆傝繖縐嶈漿鎹㈢殑瀹夊叏鎬т篃瑕佸紑鍙戜漢鍛樻潵淇濊瘉銆?br>鈶㈡妸絀烘寚閽堣漿鎹㈡垚鐩爣綾誨瀷鐨勭┖鎸囬拡銆?br>鈶f妸浠諱綍綾誨瀷鐨勮〃杈懼紡杞崲鎴恦oid綾誨瀷銆?/p>

娉ㄦ剰錛?strong style="color: black; background-color: #ffff66;">static_cast涓嶈兘杞崲鎺塭xdivssion鐨刢onst銆乿olitale銆佹垨鑰卂_unaligned灞炴с?/p>


3.2 dynamic_cast
鐢ㄦ硶錛歞ynamic_cast < type-id > ( exdivssion )
璇ヨ繍綆楃鎶奺xdivssion杞崲鎴恡ype-id綾誨瀷鐨勫璞°俆ype-id蹇呴』鏄被鐨勬寚閽堛佺被鐨勫紩鐢ㄦ垨鑰卾oid *錛?br>濡傛灉type-id鏄被鎸囬拡綾誨瀷錛岄偅涔坋xdivssion涔熷繀欏繪槸涓涓寚閽堬紝濡傛灉type-id鏄竴涓紩鐢紝閭d箞exdivssion涔熷繀欏繪槸涓涓紩鐢ㄣ?/p>

dynamic_cast涓昏鐢ㄤ簬綾誨眰嬈¢棿鐨勪笂琛岃漿鎹㈠拰涓嬭杞崲錛岃繕鍙互鐢ㄤ簬綾諱箣闂寸殑浜ゅ弶杞崲銆?br>鍦ㄧ被灞傛闂磋繘琛屼笂琛岃漿鎹㈡椂錛宒ynamic_cast鍜?strong style="color: black; background-color: #ffff66;">static_cast鐨勬晥鏋滄槸涓鏍風(fēng)殑錛?br>鍦ㄨ繘琛屼笅琛岃漿鎹㈡椂錛宒ynamic_cast鍏鋒湁綾誨瀷媯(gè)鏌ョ殑鍔熻兘錛屾瘮static_cast鏇村畨鍏ㄣ?br>class B{
public:
int m_iNum;
virtual void foo();
};

class D:public B{
public:
char *m_szName[100];
};

void func(B *pb){
D *pd1 = static_cast(pb);
D *pd2 = dynamic_cast(pb);
}

鍦ㄤ笂闈㈢殑浠g爜孌典腑錛屽鏋減b鎸囧悜涓涓狣綾誨瀷鐨勫璞★紝pd1鍜宲d2鏄竴鏍風(fēng)殑錛屽茍涓斿榪欎袱涓寚閽堟墽琛孌綾誨瀷鐨勪換浣曟搷浣滈兘鏄畨鍏ㄧ殑錛?br>浣嗘槸錛屽鏋減b鎸囧悜鐨勬槸涓涓狟綾誨瀷鐨勫璞★紝閭d箞pd1灝嗘槸涓涓寚鍚戣瀵硅薄鐨勬寚閽堬紝瀵瑰畠榪涜D綾誨瀷鐨勬搷浣滃皢鏄笉瀹夊叏鐨勶紙濡傝闂甿_szName錛夛紝
鑰宲d2灝嗘槸涓涓┖鎸囬拡銆?/p>

鍙﹀瑕佹敞鎰忥細(xì)B瑕佹湁铏氬嚱鏁幫紝鍚﹀垯浼?xì)缂栬瘧鍑洪敊锛?strong style="color: black; background-color: #ffff66;">static_cast鍒欐病鏈夎繖涓檺鍒躲?br>榪欐槸鐢變簬榪愯鏃剁被鍨嬫鏌ラ渶瑕佽繍琛屾椂綾誨瀷淇℃伅錛岃岃繖涓俊鎭瓨鍌ㄥ湪綾葷殑铏氬嚱鏁拌〃錛?br>鍏充簬铏氬嚱鏁拌〃鐨勬蹇碉紝璇︾粏鍙錛変腑錛屽彧鏈夊畾涔変簡(jiǎn)铏氬嚱鏁扮殑綾繪墠鏈夎櫄鍑芥暟琛紝
娌℃湁瀹氫箟铏氬嚱鏁扮殑綾繪槸娌℃湁铏氬嚱鏁拌〃鐨勩?/p>

鍙﹀錛宒ynamic_cast榪樻敮鎸佷氦鍙夎漿鎹紙cross cast錛夈傚涓嬩唬鐮佹墍紺恒?br>class A{
public:
int m_iNum;
virtual void f(){}
};

class B:public A{
};

class D:public A{
};

void foo(){
B *pb = new B;
pb->m_iNum = 100;

D *pd1 = static_cast(pb); //compile error
D *pd2 = dynamic_cast(pb); //pd2 is NULL
delete pb;
}

鍦ㄥ嚱鏁癴oo涓紝浣跨敤static_cast榪涜杞崲鏄笉琚厑璁哥殑錛屽皢鍦ㄧ紪璇戞椂鍑洪敊錛涜屼嬌鐢?dynamic_cast鐨勮漿鎹㈠垯鏄厑璁哥殑錛岀粨鏋滄槸絀烘寚閽堛?/p>


3.3 reindivter_cast
鐢ㄦ硶錛歳eindivter_cast (exdivssion)
type-id蹇呴』鏄竴涓寚閽堛佸紩鐢ㄣ佺畻鏈被鍨嬨佸嚱鏁版寚閽堟垨鑰呮垚鍛樻寚閽堛?br>瀹冨彲浠ユ妸涓涓寚閽堣漿鎹㈡垚涓涓暣鏁幫紝涔熷彲浠ユ妸涓涓暣鏁拌漿鎹㈡垚涓涓寚閽堬紙鍏堟妸涓涓寚閽堣漿鎹㈡垚涓涓暣鏁幫紝
鍦ㄦ妸璇ユ暣鏁拌漿鎹㈡垚鍘熺被鍨嬬殑鎸囬拡錛岃繕鍙互寰楀埌鍘熷厛鐨勬寚閽堝鹼級(jí)銆?/p>

璇ヨ繍綆楃鐨勭敤娉曟瘮杈冨銆?/p>

3.4 const_cast
鐢ㄦ硶錛歝onst_cast (exdivssion)
璇ヨ繍綆楃鐢ㄦ潵淇敼綾誨瀷鐨刢onst鎴杤olatile灞炴с傞櫎浜?jiǎn)const 鎴杤olatile淇グ涔嬪錛?type_id鍜宔xdivssion鐨勭被鍨嬫槸涓鏍風(fēng)殑銆?br>甯擱噺鎸囬拡琚漿鍖栨垚闈炲父閲忔寚閽堬紝騫朵笖浠嶇劧鎸囧悜鍘熸潵鐨勫璞★紱
甯擱噺寮曠敤琚漿鎹㈡垚闈炲父閲忓紩鐢紝騫朵笖浠嶇劧鎸囧悜鍘熸潵鐨勫璞★紱甯擱噺瀵硅薄琚漿鎹㈡垚闈炲父閲忓璞°?/p>

Voiatile鍜宑onst綾昏瘯銆備婦濡備笅涓渚嬶細(xì)
class B{
public:
int m_iNum;
}
void foo(){
const B b1;
b1.m_iNum = 100; //comile error
B b2 = const_cast(b1);
b2. m_iNum = 200; //fine
}
涓婇潰鐨勪唬鐮佺紪璇戞椂浼?xì)鎶ラ敊锛屽洜湄?fù)b1鏄竴涓父閲忓璞★紝涓嶈兘瀵瑰畠榪涜鏀瑰彉錛?br>浣跨敤const_cast鎶婂畠杞崲鎴愪竴涓父閲忓璞★紝灝卞彲浠ュ瀹冪殑鏁版嵁鎴愬憳浠繪剰鏀瑰彉銆傛敞鎰忥細(xì)b1鍜宐2鏄袱涓笉鍚岀殑瀵硅薄銆?br>
 

== ===========================================

== dynamic_cast .vs. static_cast
== ===========================================

class B { ... };
class D : public B { ... };

void f(B* pb)
{

D* pd1 = dynamic_cast(pb);

D* pd2 = static_cast(pb);
}

If pb really points to an object of type D, then pd1 and pd2 will get the same value. They will also get the same value if pb == 0.

If pb points to an object of type B and not to the complete D class, then dynamic_cast will know enough to return zero. However, static_cast relies on the programmer’s assertion that pb points to an object of type D and simply returns a pointer to that supposed D object.

鍗?/span>dynamic_cast鍙敤浜庣戶鎵夸綋緋諱腑鐨勫悜涓嬭漿鍨嬶紝鍗沖皢鍩虹被鎸囬拡杞崲涓烘淳鐢熺被鎸囬拡錛屾瘮static_cast鏇翠弗鏍兼洿瀹夊叏銆?/span>dynamic_cast鍦ㄦ墽琛屾晥鐜囦笂姣?/span>static_cast瑕佸樊涓浜涳紝浣?/span>static_cast鍦ㄦ洿瀹戒笂鑼冨洿鍐呭彲浠ュ畬鎴愭槧灝勶紝榪欑涓嶅姞闄愬埗鐨勬槧灝勪即闅忕潃涓嶅畨鍏ㄦс?/span>static_cast瑕嗙洊鐨勫彉鎹㈢被鍨嬮櫎綾誨眰嬈$殑闈?rùn)鎬佸鑸互澶栵紝榪樺寘鎷棤鏄犲皠鍙樻崲銆佺獎(jiǎng)鍖栧彉鎹?/span>(榪欑鍙樻崲浼?xì)瀵艰嚧瀵硅薄鍒囩?/span>,涓㈠け淇℃伅)銆佺敤VOID*鐨勫己鍒跺彉鎹€侀殣寮忕被鍨嬪彉鎹㈢瓑...


== ===========================================
== static_cast .vs. reinterdivt_cast
== ================================================

reinterdivt_cast鏄負(fù)浜?jiǎn)鏄牉畡鍒颁竴涓畬鍏ㄤ笉鍚岀被鍨嬬殑鎰忔濓紝榪欎釜鍏抽敭璇嶅湪鎴戜滑闇瑕佹妸綾誨瀷鏄犲皠鍥炲師鏈夌被鍨嬫椂鐢ㄥ埌瀹冦傛垜浠槧灝勫埌鐨勭被鍨嬩粎浠呮槸涓轟簡(jiǎn)鏁呭紕鐜勮櫄鍜屽叾浠栫洰鐨勶紝榪欐槸鎵鏈夋槧灝勪腑鏈鍗遍櫓鐨勩?/span>(榪欏彞璇濇槸C++緙栫▼鎬濇兂涓殑鍘熻瘽)

static_cast 鍜?/span> reinterdivt_cast 鎿嶄綔絎︿慨鏀逛簡(jiǎn)鎿嶄綔鏁扮被鍨嬨傚畠浠笉鏄簰閫嗙殑錛?/span> static_cast 鍦ㄧ紪璇戞椂浣跨敤綾誨瀷淇℃伅鎵ц杞崲錛屽湪杞崲鎵ц蹇呰鐨勬嫻?/span>(璇稿鎸囬拡瓚婄晫璁$畻, 綾誨瀷媯(gè)鏌?/span>). 鍏舵搷浣滄暟鐩稿鏄畨鍏ㄧ殑銆傚彟涓鏂歸潰錛?/span>reinterdivt_cast 浠呬粎鏄噸鏂拌В閲婁簡(jiǎn)緇欏嚭鐨勫璞$殑姣旂壒妯″瀷鑰屾病鏈夎繘琛屼簩榪涘埗杞崲錛?/span> 渚嬪瓙濡備笅錛?/span>

int n=9; double d=static_cast < double > (n);

涓婇潰鐨勪緥瀛愪腑, 鎴戜滑灝嗕竴涓彉閲忎粠 int 杞崲鍒?/span> double銆?/span> 榪欎簺綾誨瀷鐨勪簩榪涘埗琛ㄨ揪寮忔槸涓嶅悓鐨勩?/span> 瑕佸皢鏁存暟 9 杞崲鍒?/span> 鍙岀簿搴︽暣鏁?/span> 9錛?/span>static_cast 闇瑕佹紜湴涓哄弻綺懼害鏁存暟 d 琛ヨ凍姣旂壒浣嶃傚叾緇撴灉涓?/span> 9.0銆傝?/span>reinterdivt_cast 鐨勮涓哄嵈涓嶅悓:

int n=9;

double d=reinterdivt_cast (n);

榪欐, 緇撴灉鏈夋墍涓嶅悓. 鍦ㄨ繘琛岃綆椾互鍚?/span>, d 鍖呭惈鏃犵敤鍊?/span>. 榪欐槸鍥犱負(fù) reinterdivt_cast 浠呬粎鏄鍒?/span> n 鐨勬瘮鐗逛綅鍒?/span> d, 娌℃湁榪涜蹇呰鐨勫垎鏋?/span>.

鍥犳, 浣犻渶瑕佽皚鎱庝嬌鐢?/span> reinterdivt_cast.



blues 2007-10-23 20:28 鍙戣〃璇勮
]]>
offsetof 瑙f瀽http://www.shnenglu.com/pjqblues/archive/2007/10/22/34868.htmlbluesbluesMon, 22 Oct 2007 08:02:00 GMThttp://www.shnenglu.com/pjqblues/archive/2007/10/22/34868.htmlhttp://www.shnenglu.com/pjqblues/comments/34868.htmlhttp://www.shnenglu.com/pjqblues/archive/2007/10/22/34868.html#Feedback2http://www.shnenglu.com/pjqblues/comments/commentRss/34868.htmlhttp://www.shnenglu.com/pjqblues/services/trackbacks/34868.html 1offsetof(s,m)瑙f瀽 offsetof(s,m)瑙f瀽
 2 
 3浠婂ぉ鐪嬩唬鐮佹椂錛屽彂鐜頒竴涓湁鐢ㄧ殑涓滀笢錛宱ffsetof(s,m)錛岃繖鏄竴涓畯錛屽畾涔夊涓嬶細(xì)
 4
 5 #define offsetof(s,m) (size_t)&(((s *)0)->m)
 6 
 7 鐒跺悗鍒扮綉涓婃煡浜?jiǎn)涓涓嬶紝鍙戠幇榪樼湡鐨勬槸寰堟湁鐢紝闄勫甫涓浣嶅ぇ渚犵殑瑙h錛?br> 8
 9  struct   AAA   
10  {   
11  int   i;   
12  int   j;   
13  }
;   
14    
15  struct AAA *pAAA;   
16  pAAA=new AAA;   
17  榪欐椂錛宲AAA瀹為檯涓婃槸涓涓狿ointer, 鎸囧悜鏌愪竴紜畾鐨勫唴瀛樺湴鍧錛屾瘮濡?x1234;   
18  鑰?nbsp;pAAA->i 鏁翠綋鏄竴涓猧nt鍨嬪彉閲忥紝鍏跺湴鍧鏄?/span>&(pAAA->i) ,'&'涓哄彇鍧榪愮畻絎︼紱   
19  閭d箞&(pAAA->i)涓瀹氱瓑浜?x1234,鍥犱負(fù)i鏄粨鏋勪綋AAA鐨勭涓涓厓绱犮?nbsp;  
20  鑰?/span>&(pAAA->j)涓瀹氭槸0x1234 + 0x4 = 0x1238; 鍥犱負(fù)sizeof(int= 4;
21    
22  榪欎釜鍋氭硶鐨勫閥濡欎箣澶勫氨鏄細(xì)瀹冩妸“0”浣滀負(fù)涓婁緥涓殑pAAA,閭d箞 &(pAAA->j)灝辨槸j鐨?nbsp;  
23  offset鍟?br>24
25  瑙f瀽緇撴灉鏄細(xì)   
26  (s   *)0 ,灝?nbsp;0 寮哄埗杞崲涓篜ointer to   "s"     
27  鍙互璁?nbsp;pS = (s *)0 錛宲S鏄寚鍚憇鐨勬寚閽堬紝瀹冪殑鍊兼槸0;   
28  閭d箞pS->m灝辨槸m榪欎釜鍏冪礌浜?jiǎn)锛岃?/span>&(pS->m)灝辨槸m鐨勫湴鍧錛岃屽湪鏈緥涓氨鏄痮ffset鍟?nbsp;  
29    
30  鍐嶆妸緇撴灉寮哄埗杞崲涓簊ize_t鍨嬬殑灝監(jiān)K 浜?jiǎn)锛宻ize_t鍏跺疄涔熷氨鏄痠nt鍟︼紒錛?nbsp;   
31 
32 
33

blues 2007-10-22 16:02 鍙戣〃璇勮
]]>
2006騫翠笘鐣岄《綰ф潃姣掕蔣浠舵帓鍚?/title><link>http://www.shnenglu.com/pjqblues/archive/2007/01/26/18011.html</link><dc:creator>blues</dc:creator><author>blues</author><pubDate>Fri, 26 Jan 2007 06:55:00 GMT</pubDate><guid>http://www.shnenglu.com/pjqblues/archive/2007/01/26/18011.html</guid><wfw:comment>http://www.shnenglu.com/pjqblues/comments/18011.html</wfw:comment><comments>http://www.shnenglu.com/pjqblues/archive/2007/01/26/18011.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/pjqblues/comments/commentRss/18011.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/pjqblues/services/trackbacks/18011.html</trackback:ping><description><![CDATA[<a >http://blog.cnetnews.com.cn/hand/keji/3658/</a><img src ="http://www.shnenglu.com/pjqblues/aggbug/18011.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/pjqblues/" target="_blank">blues</a> 2007-01-26 14:55 <a href="http://www.shnenglu.com/pjqblues/archive/2007/01/26/18011.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>com涓互緇撴瀯浣撲綔涓烘帴鍙e弬鏁?/title><link>http://www.shnenglu.com/pjqblues/archive/2006/12/11/16273.html</link><dc:creator>blues</dc:creator><author>blues</author><pubDate>Mon, 11 Dec 2006 07:39:00 GMT</pubDate><guid>http://www.shnenglu.com/pjqblues/archive/2006/12/11/16273.html</guid><wfw:comment>http://www.shnenglu.com/pjqblues/comments/16273.html</wfw:comment><comments>http://www.shnenglu.com/pjqblues/archive/2006/12/11/16273.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/pjqblues/comments/commentRss/16273.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/pjqblues/services/trackbacks/16273.html</trackback:ping><description><![CDATA[<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #008080">聽1</span> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #000000">閫氳繃VARIANT;聽聽聽<br /></span> <span style="COLOR: #008080">聽2</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽VARIANT聽聽聽varData;(鍑哄弬)聽聽聽<br /></span> <span style="COLOR: #008080">聽3</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽<br /></span> <span style="COLOR: #008080">聽4</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽MYSTRUCT聽聽聽</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">pMyData聽聽聽</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">聽聽聽NULL;聽聽聽<br /></span> <span style="COLOR: #008080">聽5</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽<br /></span> <span style="COLOR: #008080">聽6</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽pMyData聽聽聽</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">聽聽聽(MYSTRUCT</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">)CoTaskMemAlloc(</span> <span style="COLOR: #0000ff">sizeof</span> <span style="COLOR: #000000">(MYSTRUCT));聽聽聽<br /></span> <span style="COLOR: #008080">聽7</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽<img src="http://www.shnenglu.com/images/dot.gif" />.聽聽聽<br /></span> <span style="COLOR: #008080">聽8</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽<br /></span> <span style="COLOR: #008080">聽9</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽varData.byref聽聽聽</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">聽聽聽(LPVOID)pMyData;聽聽聽<br /></span> <span style="COLOR: #008080">10</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽<br /></span> <span style="COLOR: #008080">11</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽鍦ㄨ皟鐢ㄦ枃浠墮噷錛屽悓鏍峰畾涔壜犅犅?br /></span> <span style="COLOR: #008080">12</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽VARIANT聽聽聽varData(鍏ュ弬錛壜犅犅?br /></span> <span style="COLOR: #008080">13</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽<br /></span> <span style="COLOR: #008080">14</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽MYSTRUCT聽聽聽</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">pMyData聽聽聽</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">聽聽聽NULL;聽聽聽<br /></span> <span style="COLOR: #008080">15</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽<br /></span> <span style="COLOR: #008080">16</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽pMyData聽聽聽</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">聽聽聽(MYSTRUCT</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">)varData.byref;聽聽聽<br /></span> <span style="COLOR: #008080">17</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽聽聽<br /></span> <span style="COLOR: #008080">18</span> <span style="COLOR: #000000"> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" />聽聽CoTaskMemFree((LPVOID)pMyData);</span> </div><img src ="http://www.shnenglu.com/pjqblues/aggbug/16273.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/pjqblues/" target="_blank">blues</a> 2006-12-11 15:39 <a href="http://www.shnenglu.com/pjqblues/archive/2006/12/11/16273.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Getting an (ATL) ActiveX control to print from Office Applicationshttp://www.shnenglu.com/pjqblues/archive/2006/11/07/14804.htmlbluesbluesTue, 07 Nov 2006 09:38:00 GMThttp://www.shnenglu.com/pjqblues/archive/2006/11/07/14804.htmlhttp://www.shnenglu.com/pjqblues/comments/14804.htmlhttp://www.shnenglu.com/pjqblues/archive/2006/11/07/14804.html#Feedback0http://www.shnenglu.com/pjqblues/comments/commentRss/14804.htmlhttp://www.shnenglu.com/pjqblues/services/trackbacks/14804.html
13 votes for this article.
Popularity: 4.73. Rating: 4.25 out of 5.

Introduction

Seeing as this is my first ever post to CodeProject, let me do a quick introduction as to who I am and what I do. I have been working in one form of C and C++ or another for as long as I can remember (among the other myriad of languages that I've run into). Nowadays, most of my development is focused on Microsoft Windows platforms, and is done in VC6, VC2002.NET. I am heavily entrenched in BI (Business Intelligence) development, and in my spare time develop little ActiveX controls and games etc...

The plot

Having learnt COM a while ago, I made the obvious progression to ATL to ease the development of boilerplate code, and to leverage off Microsoft's template library. As my experience grew, I ventured into creating ActiveX controls using the ATL framework ... and life was good. I could spew out a fairly useful (albeit not overly complex) control within a short period of time. Recently, I was asked to create a KPI (Key Performance Indicator) control that could be embedded in a web page and an Excel document. Obviously based on my experience (which was obviously not vast) I thought that this would be no problem and off I went, creating code that would meet the functional spec (we all work to these don't we :)).

A couple of days later the control was finished and the final tests were being run when someone asked me to print a hardcopy of an example spreadsheet with the embedded control. This is where my nightmares began. Not only did my control not print, but there was no clear indication as to why it didn't print. And so my exploration into this apparent mystery began.

Have you ever tried to include 3rd party ActiveX controls into an Office document? They sure seem to work fine, but most (apart from the Microsoft controls) don't seem to render themselves when you request a Print Preview or a simple Print of the worksheet or document. So, if any of you have ever had this problem, or have never dabbled with this, but think that you may be heading this way, take note of this, cos it might save you hours of frustration and frantic searching on MSDN and Google.

So what now?

The first thing one needs to realize is that even though we have been blessed with Office 2000 and Office XP, the printing architecture still uses the old Windows-format metafile for its printing operations. This metafile format was used in 16-bit Windows-based applications (thinks back to Win3.1). Now, this becomes a major problem for ActiveX developers who wish their controls to be printable from within Office applications, because this old metafile format only supported a limited set of GDI functionality. The list of supported GDI functions can be found here.

Now that you are armed with your limited function set, you cringe with the realization that you can no longer create memory DC's, you can no longer use your lovely DrawText() functions and you can definitely no longer call GetTextExtentPoint32() function. However, those realizations only hold true for the instance of when your control is being rendered to an old format metafile. So how do we empower our control to know that its being rendered to an old format metafile? Simple, we use the GetObjectType() function and check if the result is equal to OBJ_METADC (old metafile format):

HRESULT Cxxxxx::OnDraw(ATL_DRAWINFO& di)
{
    HDC hdc = di.hdcDraw;
    bool bMetaFile = false;

    //// lets check if we're drawing to an old// metafile format.. (like Office printing)//if ( GetObjectType(hdc) == OBJ_METADC )
        bOldMetaFile = true;

    //// the rest of your code...//
}

For interest, the opposite of OBJ_METADC is OBJ_ENHMETADC (refer to this MSDN document).

Now that we know if we're drawing to an old metafile format or not, we can write adaptive code to cater for each instance or we can just write all our drawing logic using the limited set of functionality that is supported by the old metafile DC.

What about fonts and text extents?

As any ATL ActiveX developer knows, using fonts in AX controls provides for limited amount of fun. The typical piece of code would probably look something like this:

																//
																// ... some code
																//
    CComQIPtr<IFont, &IID_IFont> pFont(m_pFont);
    TEXTMETRICOLE tm;if ( pFont != NULL )
    {
        pFont->get_hFont(&newFont);
        pFont->AddRefHfont(newFont);
        pFont->QueryTextMetrics(&tm);
        oldFont = (HFONT) SelectObject(dc, newFont);
    }

The Bolded lines of code are ones that I didn't use regularly, due to the fact that I didn't really need to know about the breakdown of my font's details because I had access to GetTextExtentPoint32() function. Unfortunately, in this scenario, we don't have access to that function to determine how wide (in pixels) our text is going to be. But there is another way to calculate this fairly accurately, as is demonstrated in the code below:

																//
																// assume that we have called QueryTextMetrics() and
																// have a filled TEXTMETRICOLE structure called tm
																//
CComBSTR strText(_T("Hello, world"));
SIZE sz;

sz.cx = strText.Length() * tm.tmAveCharWidth;
sz.cy = tm.tmHeight;

Having said this, there are many other functions that I use a lot that I can't use if I want my ActiveX control to be printable by Office, but as with GetTextExtentPoint32() and its respective replacement, there is always a way to replace these functions using Old-Metafile-Safe-Drawing-Code (OMSDC). *maybe that acronym will catch on*

Conclusion

When creating an ActiveX control that you know will be used inside Office applications, and will most probably be printed, remember to stick to these guidelines when developing your drawing logic. I was fairly shocked by how little information was available in the MSDN and online in general, while I was searching for information on how to enable my ActiveX control to print from within an Office application. There are hundreds of documents on ActiveX controls being printed from within Internet Explorer, but none address this particular issue. Perhaps I was looking in the wrong places. Hopefully this article will help one or more of you one day ;)

Acknowledgment

Many thanks to Igor Tandetnik for pointing me in the right direction on this.

About Peter Mares



blues 2006-11-07 17:38 鍙戣〃璇勮
]]>
杞?==濡備綍鐢ˋTL鍒涘緩ActiveX鎺т歡錛堢墰浜虹炕璇戠殑錛?/title><link>http://www.shnenglu.com/pjqblues/archive/2006/11/07/14801.html</link><dc:creator>blues</dc:creator><author>blues</author><pubDate>Tue, 07 Nov 2006 08:53:00 GMT</pubDate><guid>http://www.shnenglu.com/pjqblues/archive/2006/11/07/14801.html</guid><wfw:comment>http://www.shnenglu.com/pjqblues/comments/14801.html</wfw:comment><comments>http://www.shnenglu.com/pjqblues/archive/2006/11/07/14801.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/pjqblues/comments/commentRss/14801.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/pjqblues/services/trackbacks/14801.html</trackback:ping><description><![CDATA[<p> <u> <a > <font color="#006400">http://www.czvc.com/down.asp?id=105</font> </a> </u> <a > </a> </p><img src ="http://www.shnenglu.com/pjqblues/aggbug/14801.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/pjqblues/" target="_blank">blues</a> 2006-11-07 16:53 <a href="http://www.shnenglu.com/pjqblues/archive/2006/11/07/14801.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>DB2鍏嶈垂鐗?/title><link>http://www.shnenglu.com/pjqblues/archive/2006/09/12/12342.html</link><dc:creator>blues</dc:creator><author>blues</author><pubDate>Tue, 12 Sep 2006 06:08:00 GMT</pubDate><guid>http://www.shnenglu.com/pjqblues/archive/2006/09/12/12342.html</guid><wfw:comment>http://www.shnenglu.com/pjqblues/comments/12342.html</wfw:comment><comments>http://www.shnenglu.com/pjqblues/archive/2006/09/12/12342.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/pjqblues/comments/commentRss/12342.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/pjqblues/services/trackbacks/12342.html</trackback:ping><description><![CDATA[http://www.ibm.com/developerworks/cn/downloads/im/udbexp/<img src ="http://www.shnenglu.com/pjqblues/aggbug/12342.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/pjqblues/" target="_blank">blues</a> 2006-09-12 14:08 <a href="http://www.shnenglu.com/pjqblues/archive/2006/09/12/12342.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>緹庡浗璁╀漢鍠烽キ鐨勬硶寰?/title><link>http://www.shnenglu.com/pjqblues/archive/2006/08/25/11702.html</link><dc:creator>blues</dc:creator><author>blues</author><pubDate>Fri, 25 Aug 2006 09:53:00 GMT</pubDate><guid>http://www.shnenglu.com/pjqblues/archive/2006/08/25/11702.html</guid><wfw:comment>http://www.shnenglu.com/pjqblues/comments/11702.html</wfw:comment><comments>http://www.shnenglu.com/pjqblues/archive/2006/08/25/11702.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/pjqblues/comments/commentRss/11702.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/pjqblues/services/trackbacks/11702.html</trackback:ping><description><![CDATA[ <h4 class="TextColor1" id="subjcns!D831FDBD876F27A2!167" style="MARGIN-BOTTOM: 0px">聽</h4> <div class="6116166" id="msgcns!D831FDBD876F27A2!167"> <div>榪欑編鍥借繕鏈夎繖絳夊柗楗殑娉曞緥錛屽疄灞炲ぇ寮鐪肩晫錛岀湅鏉ユ湁鏈轟細(xì)瑕佸ソ濂藉杽鍔犲埄鐢ㄤ簡(jiǎn)錛侊紒鐜頒笌鍚勪綅涓璧峰垎浜細(xì)聽 <br />緹庡浗鑱旈偊娉曞緥瑙勫畾錛毬?<br /><br />1錛変笉寰椾笌璞尓鍙戠敓鎬у叧緋匯傦紙*錛岃皝鏁㈠憖錛壜?<br />2錛夋瘡鍛ㄥ洓鏅?:00浠ュ悗涓嶅緱鏀綪銆傦紙浠ュ悗榪樼湡瑕佸皬蹇?jī)浜?jiǎn)錛屽埆涓涓嶇暀紲炲潗鐗簡(jiǎn)榪樹笉鐭ヤ負(fù)聽 <br />鍟ワ級(jí)聽 <br />3錛変換浣曚漢涓嶅緱閿鍞叾瀛愬コ銆傦紙濂借薄涓浗涔熶笉璁稿惂錛壜?<br /><br />闃挎媺宸撮┈宸烇細(xì)聽 <br />鏃犺浠諱綍鏃跺欙紝灝嗗啺嬋娣嬪嵎鏀懼湪鍙h閲屾槸榪濇硶鐨勩傦紙鏈夌梾涓殑錛壜?<br /><br />闃胯偗鑹插窞錛毬?<br />鐢鋒у彲浠ュ悎娉曟鎵撳叾閰嶅伓錛屼絾姣忔湀鏈澶氫竴嬈°傦紙浼拌寰堝涓滃寳鐨勫厔寮熺煡閬撲簡(jiǎn)涓瀹氭兂聽 <br />縐繪皯闃胯偗鑹蹭簡(jiǎn)錛屽彲涔熸湁渚嬪鍛錛屽厠鏋楅】灝辨槸闃胯偗鑹茬殑鍓嶅窞闀匡紝鍜嬭佽鍠滆幈鑾夋墎鍛錛壜?<br /><br />浜氬埄妗戠撼宸烇細(xì)聽 <br />浠諱綍鎴塊棿涓笉寰楁湁涓ゆ牴浠ヤ笂鐨勫亣****銆傦紙浼拌閭e窞鐨勬渶楂樻硶瀹樹斧鏄釜鍙樻佺媯!錛壜?<br /><br />澶忓▉澶峰窞錛毬?<br />涓嶅緱灝嗚胺鐗╂斁鍦ㄨ蟲湹閲屻傦紙紲炵粡鐥咃紝浠ヤ負(fù)鍋峰お絀虹瀛愬憖錛壜?<br /><br />鍗板紵瀹夌撼宸烇細(xì)聽 <br />1錛変換浣曞勾婊?8宀佺殑鐢鋒э紝鑻ヤ笌17宀佷互涓嬬殑濂蟲у彂鐢熸у叧緋伙紝鑰屼笖褰撴椂濂瑰張娌$┛闉嬄?<br />琚滐紝閭e皢璇鵑噸緗傦紙鍏勫紵浠崈涓囨敞鎰忎簡(jiǎn)鍛錛佸埆鍏ㄨ劚浜?jiǎn)锛?jí)聽 <br />2錛夊渾鍛ㄧ巼鍦ㄨ宸炴硶瀹氫負(fù)4銆傦紙媧繪椿姘旀鍜辯鍐蹭箣鍓嶈緢鍛錛侊級(jí)聽 <br /><br />鐖辮嵎鍗庡窞錛毬?<br />1錛変換浣曞彧鏈変竴鍙笂鑷傜殑閽㈢惔婕斿鑰呭繀欏誨厤璐規(guī)紨濂忋傦紙涓ラ噸姝ц孌嬬柧鑹烘湳琛ㄦ紨瀹訛級(jí)聽 <br />2錛変換浣曟湁鑳冪梾鐨勭敺鎬т笉寰楀湪鍏叡鍦烘墍涓庡コ鎬ф帴鍚匯傦紙鎺ュ惢鍜岃儍鏈夊叧緋誨悧錛熺敺鎬ц儍鐧屄?<br />鏅氭湡鎮(zhèn)h呯殑紱忛煶錛壜?<br /><br />綰界害宸烇細(xì)聽 <br />1錛変笉寰椾粎涓哄ū涔愯屽皢鐞冪牳鍚戜粬浜鴻剳琚嬨傦紙璋嬫潃鍙互涓嶏紵鐪熺殑鑴戝瓙榪涙按浜?jiǎn)锛?jí)聽 <br />2錛?0:00浠ュ悗涓嶅緱絀挎嫋闉嬨傦紙鍏夎剼鍚э級(jí)聽 <br /><br />鏂版辰瑗垮窞錛毬?<br />鍑¤皨鏉鏃朵笉寰楃┛闃插脊鑳屽績(jī)銆傦紙綆″緱鐫鍚楋紝璀﹀療榪欎箞娌¤嚜淇★紒錛壜?<br /><br />鍖楀崱宸烇細(xì)聽 <br />浠諱綍涓浣嶆湭濠氱敺鎬т笌涓涓烘湭濠氬コ鎬э紝濡傛灉鍦ㄤ換浣曟梾棣嗘垨姹借濺鏃呴鐧昏涓哄凡濠氾紝閭d箞聽 <br />浠栦滑鍗崇畻鍚堟硶澶浜?jiǎn)銆傦紙鎯沖甫灝忚湝寮鎴跨殑鍏勫紵浠崈涓囧埆鍘婚偅宸炲憖錛侊級(jí)聽 <br /><br />瀹捐タ娉曞凹浜氬窞錛毬?<br />涓嶅緱鍦ㄦ蕩瀹ゅ敱姝屻傦紙闅炬湪瀹懼ぇ鍟嗗闄㈢殑鍚岃優(yōu)閮戒笉浼?xì)K姝岋級(jí)聽 <br /><br />鍗楀崱宸烇細(xì)聽 <br />浠呭湪姣忓懆鍏紝鐢鋒ц鍏佽鍦ㄦ硶闄㈢殑闂ㄥ墠鍙伴樁涓婂悎娉曟鎵撳叾閰嶅伓銆傦紙榪欐槸鍟ヨ瀹氾紝閮伮?<br />闂稩NG錛壜?<br /><br />鐘逛粬宸烇細(xì)聽 <br />1錛変笉鍠濈墰濂惰繚娉曘傦紙鍠濅笉瀹屾彺鍔╅潪媧查毦姘戝憖錛屽共涔堜負(fù)闅捐嚜宸憋紒闅炬亢涓鍙鍠濈墰聽 <br />濂跺氨鎷夎倸瀛愮殑鏈嬪弸浠庣姽澶ц漿鍒扮航綰︿簡(jiǎn)錛屼繚鍛借绱у憖銆傦級(jí)聽 <br />2錛変笉寰楀湪姝e湪鎵ц鎬ユ晳浠誨姟鐨勬晳鎶よ濺鍚庡駭涓奙ake聽Love銆傦紙榪欏ソ鐞嗚В錛屾曠梾浜虹湅瑙佽綆÷?<br />鐖嗚涔堬紒鍝堝搱錛壜?/div> </div> <img src ="http://www.shnenglu.com/pjqblues/aggbug/11702.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/pjqblues/" target="_blank">blues</a> 2006-08-25 17:53 <a href="http://www.shnenglu.com/pjqblues/archive/2006/08/25/11702.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>涓婃搗鍗拌薄http://www.shnenglu.com/pjqblues/archive/2006/08/23/11610.htmlbluesbluesWed, 23 Aug 2006 07:21:00 GMThttp://www.shnenglu.com/pjqblues/archive/2006/08/23/11610.htmlhttp://www.shnenglu.com/pjqblues/comments/11610.htmlhttp://www.shnenglu.com/pjqblues/archive/2006/08/23/11610.html#Feedback1http://www.shnenglu.com/pjqblues/comments/commentRss/11610.htmlhttp://www.shnenglu.com/pjqblues/services/trackbacks/11610.html

blues 2006-08-23 15:21 鍙戣〃璇勮
]]>
狠狠色狠狠色综合久久| 久久只这里是精品66| 久久久久夜夜夜精品国产| 91精品国产91久久| 久久影院久久香蕉国产线看观看| 久久精品三级视频| 色播久久人人爽人人爽人人片AV| 久久综合九色综合网站| 国产一区二区三区久久精品| 久久精品国产精品亜洲毛片| 久久久久高潮综合影院| 狠狠色丁香婷综合久久| 久久综合视频网站| 精品久久久久久无码专区不卡| 国产福利电影一区二区三区,免费久久久久久久精 | 久久午夜福利电影| 新狼窝色AV性久久久久久| 国产成人精品久久亚洲| 伊人久久大香线蕉av不变影院 | 久久男人Av资源网站无码软件| 亚洲欧美日韩精品久久| 精品久久久一二三区| 国产韩国精品一区二区三区久久| 日产久久强奸免费的看| 久久精品a亚洲国产v高清不卡| 日韩AV毛片精品久久久| 久久久久久免费一区二区三区| 香蕉久久夜色精品国产尤物| 久久免费精品一区二区| 久久人人爽爽爽人久久久| 香蕉99久久国产综合精品宅男自| 久久久久国产精品| 无码国产69精品久久久久网站| 久久精品国产福利国产琪琪| 九九精品99久久久香蕉| 久久久久久久波多野结衣高潮| 国产—久久香蕉国产线看观看 | 无码人妻久久一区二区三区| 人人狠狠综合久久亚洲| 一级做a爱片久久毛片| 久久精品国产亚洲AV高清热|