WebBrowser Control的一个简单C++封装
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
想在自己的窗口中放上一个WebBrowser Contorl,但又不想用MFC;可惜网上除了MFC/ATL以外,使用WebBrowser的资料少得可怜。
在CodeGuru上找到Jeff Glatt 的一篇文章Display a Web Page in a Plain C Win32 Application,写成于2002年,作者的确够厚道,不但给了两份注释完整的代码(一个是exe的源文件,一个是Dll的源文件),还把如何用C一步一步实现虚函数表、接口、继承、多继承清清楚楚剖析了出来,整个一篇绝好的教你凭空(我是想说不依赖于MFC/ATL)把ActiveX控件嵌入自己窗口的指引。
Jeff Glatt的文章端的是精彩至极,但是用Pure C搞COM的确……我反正不是这块料。不过话说回来,有了这篇妙文的指引,做一个C++的包装就只需要医葫芦画瓢啦。下面就是我原样照搬的一个C++的包装类。我是把这个类做成一个Lib文件(Jeff Glatt也就lib文件没做啦),算是个“求同存异”吧。略为有点不一样的是,Jeff Glatt考虑了打开多个窗口的问题,而我的代码,一个类就一个窗体了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 |
下面是源代码: 类CWebBrowser: /******************************************************************** @created: 2006/01/16 @version: 0.1 @filename: WebBrowser.h @file base: WebBrowser @file ext: h @author: Pomelo Wu @purpose: Declaration for class CWebBrowser, a wrapper of IWebBrowser2. @remark: To use this class, include 'Exdisp.h' before this file. Besides, use OleInitialize instead of CoInitialize, and do not forget OleUninitialize. @revision history: -- Date Ver. Modifier Description-- 2006/01/16 0.1 Pomelo Create *********************************************************************/ #ifndef __WEBBROWSER_H__ #define __WEBBROWSER_H__ // This is for a simple C++ wrapper. There are lots more things we can control about the browser object, // but we don't do it all in this. _Many_ of the functions we provide for the browser to call, will // never actually be called by the browser in our sample. Why? Because we don't do certain things // with the browser that would require it to call those functions (even though we need to provide // at least some stub for all of the functions). // // So, for these "dummy functions" that we don't expect the browser to call, we'll just stick in some // assembly code that causes a debugger breakpoint and tells the browser object that we don't support // the functionality. That way, if we try to do more things with the browser object, and it starts // calling these "dummy functions", we'll know which ones we should add more meaningful code to. #define NOTIMPLEMENTED _ASSERT(0); return(E_NOTIMPL) //============================================================================= /* @class CWebBrowser @author Pomelo Wu @brief A wrapper class of IWebBrowser2 interface. */ //============================================================================= class CWebBrowser { public: // Constructor CWebBrowser(void); // Destructor ~CWebBrowser(void); private: // Data members IOleObject *m_pBrowserObj; public: // Interfaces /*************************** UnEmbedBrowserObject() ************************ * Called to detach the browser object from our host window, and free its * resources, right before we destroy our window. * * hwnd = Handle to the window hosting the browser object. * * NOTE: The pointer to the browser object must have been stored in the * window's USERDATA field. In other words, don't call UnEmbedBrowserObject(). * with a HWND that wasn't successfully passed to EmbedBrowserObject(). */ void UnEmbedBrowserObject(HWND /*hwnd*/); /***************************** EmbedBrowserObject() ************************** * Puts the browser object inside our host window, and save a pointer to this * window's browser object in the window's GWL_USERDATA field. * * hwnd = Handle of our window into which we embed the browser object. * * RETURNS: 0 if success, or non-zero if an error. * * NOTE: We tell the browser object to occupy the entire client area of the * window. * * NOTE: No HTML page will be displayed here. We can do that with a subsequent * call to either DisplayHTMLPage() or DisplayHTMLStr(). This is merely once-only * initialization for using the browser object. In a nutshell, what we do * here is get a pointer to the browser object in our window's GWL_USERDATA * so we can access that object's functions whenever we want. */ long EmbedBrowserObject(HWND hwnd); /******************************* DisplayHTMLPage() **************************** * Displays a URL, or HTML file on disk. * * hwnd = Handle to the window hosting the browser object. * webPageName = Pointer to nul-terminated name of the URL/file. * * RETURNS: 0 if success, or non-zero if an error. * * NOTE: EmbedBrowserObject() must have been successfully called once with the * specified window, prior to calling this function. You need call * EmbedBrowserObject() once only, and then you can make multiple calls to * this function to display numerous pages in the specified window. */ long DisplayHTMLPage(HWND /*hwnd*/, LPCTSTR webPageName); /******************************* DisplayHTMLStr() **************************** * Takes a string containing some HTML BODY, and displays it in the specified * window. For example, perhaps you want to display the HTML text of... * * <P>This is a picture.<P><IMG src="mypic.jpg"> * * hwnd = Handle to the window hosting the browser object. * string = Pointer to nul-terminated string containing the HTML BODY. * (NOTE: No <BODY></BODY> tags are required in the string). * * RETURNS: 0 if success, or non-zero if an error. * * NOTE: EmbedBrowserObject() must have been successfully called once with the * specified window, prior to calling this function. You need call * EmbedBrowserObject() once only, and then you can make multiple calls to * this function to display numerous pages in the specified window. */ long DisplayHTMLStr(HWND /*hwnd*/, LPCTSTR string); private: // Methods }; #endif //__WEBBROWSER_H__ /******************************************************************** @created: 2006/01/16 @version: 0.1 @filename: WebBrowser.cpp @file base: WebBrowser @file ext: cpp @author: Pomelo Wu @purpose: Implementation for class CWebBrowser, a wrapper of IWebBrowser2. @remark: NULL @revision history: -- Date Ver. Modifier Description-- 2006/01/16 0.1 Pomelo Create *********************************************************************/ #include <Exdisp.h> /* Defines of stuff like IWebBrowser2. This is an include file with Visual C 6 and above */ #include <Mshtml.h> /* Defines of stuff like IHTMLDocument2. This is an include file with Visual C 6 and above */ /* If you gets some error when compling under VS2005, move mshtml.h to top*/ #include <crtdbg.h> /* for _ASSERT() */ #include "WBStorage.h" #include "WBClientsite.h" #include "WebBrowser.h" #include "Global.h" /* This is used by DisplayHTMLStr(). It can be global because we never change it. */ static const SAFEARRAYBOUND ArrayBound = {1, 0}; CWebBrowser::CWebBrowser(void) { m_pBrowserObj = NULL; } CWebBrowser::~CWebBrowser(void) { } void CWebBrowser::UnEmbedBrowserObject(HWND /*hwnd*/) { ////////////////////////////////////////////////////////////////////////// // // Not implemented for multiple browsing... // //IOleObject **ppBrowserHandle; //IOleObject *pBrowserObject; // //// Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when //// we initially attached the browser object to this window. //if ((ppBrowserHandle = (IOleObject **)GetWindowLong(hwnd, GWL_USERDATA))) //{ // // Unembed the browser object, and release its resources. // pBrowserObject = *ppBrowserHandle; // pBrowserObject->Close(OLECLOSE_NOSAVE); // pBrowserObject->Release(); //} ////////////////////////////////////////////////////////////////////////// if (m_pBrowserObj) { // Unembed the browser object, and release its resources. m_pBrowserObj->Close(OLECLOSE_NOSAVE); m_pBrowserObj->Release(); return; } // You must have called this for a window that wasn't successfully passed to EmbedBrowserObject(). // Bad boy! _ASSERT(0); } long CWebBrowser::DisplayHTMLStr(HWND /*hwnd*/, LPCTSTR string) { IWebBrowser2 *pWebBrowser2; LPDISPATCH lpDispatch; IHTMLDocument2 *pHtmlDoc2; SAFEARRAY *psfArray; VARIANT varMyURL; VARIANT *pVar; BSTR bstr; // Assume an error. bstr = 0; ////////////////////////////////////////////////////////////////////////// // // Not implemented for multiple browsing... // //IOleObject **ppBrowserHandle; //IOleObject *pBrowserObject; // //// Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when //// we initially attached the browser object to this window. //if ((ppBrowserHandle = (IOleObject **)GetWindowLong(hwnd, GWL_USERDATA))) //{ // // Unembed the browser object, and release its resources. // pBrowserObject = *ppBrowserHandle; //} ////////////////////////////////////////////////////////////////////////// // We want to get the base address (ie, a pointer) to the IWebBrowser2 object embedded within the browser // object, so we can call some of the functions in the former's table. if (!m_pBrowserObj->QueryInterface(IID_IWebBrowser2, (void**)&pWebBrowser2)) { // Before we can get_Document(), we actually need to have some HTML page loaded in the browser. So, // let's load an empty HTML page. Then, once we have that empty page, we can get_Document() and // write() to stuff our HTML string into it. VariantInit(&varMyURL); varMyURL.vt = VT_BSTR; varMyURL.bstrVal = SysAllocString(L"about:blank"); // Call the Navigate2() function to actually display the page. pWebBrowser2->Navigate2(&varMyURL, 0, 0, 0, 0); // Free any resources (including the BSTR). VariantClear(&varMyURL); // Call the IWebBrowser2 object's get_Document so we can get its DISPATCH object. I don't know why you // don't get the DISPATCH object via the browser object's QueryInterface(), but you don't. if (!pWebBrowser2->get_Document(&lpDispatch)) { // We want to get a pointer to the IHTMLDocument2 object embedded within the DISPATCH // object, so we can call some of the functions in the former's table. if (!lpDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&pHtmlDoc2)) { // Our HTML must be in the form of a BSTR. And it must be passed to write() in an // array of "VARIENT" structures. So let's create all that. if ((psfArray = SafeArrayCreate(VT_VARIANT, 1, (SAFEARRAYBOUND *)&ArrayBound))) { if (!SafeArrayAccessData(psfArray, (void**)&pVar)) { pVar->vt = VT_BSTR; #ifndef UNICODE { wchar_t *buffer; DWORD size; size = MultiByteToWideChar(CP_ACP, 0, string, -1, 0, 0); if (!(buffer = (wchar_t *)GlobalAlloc(GMEM_FIXED, sizeof(wchar_t) * size))) goto bad; MultiByteToWideChar(CP_ACP, 0, string, -1, buffer, size); bstr = SysAllocString(buffer); GlobalFree(buffer); } #else bstr = SysAllocString(string); #endif // Store our BSTR pointer in the VARIENT. if ((pVar->bstrVal = bstr)) { // Pass the VARIENT with its BSTR to write() in order to shove our desired HTML string // into the body of that empty page we created above. pHtmlDoc2->write(psfArray); // Normally, we'd need to free our BSTR, but SafeArrayDestroy() does it for us // SysFreeString(bstr); } } // Free the array. This also frees the VARIENT that SafeArrayAccessData created for us, // and even frees the BSTR we allocated with SysAllocString SafeArrayDestroy(psfArray); } // Release the IHTMLDocument2 object. #ifndef UNICODE bad: #endif pHtmlDoc2->Release(); } // Release the DISPATCH object. lpDispatch->Release(); } // Release the IWebBrowser2 object. pWebBrowser2->Release(); } // No error? if (bstr) return 0; // An error return -1; } long CWebBrowser::DisplayHTMLPage(HWND /*hwnd*/, LPCTSTR webPageName) { IWebBrowser2 *pWebBrowser2; VARIANT varMyURL; ////////////////////////////////////////////////////////////////////////// // // Not implemented for multiple browsing... // //IOleObject **ppBrowserHandle; //IOleObject *pBrowserObject; // //// Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when //// we initially attached the browser object to this window. //if ((ppBrowserHandle = (IOleObject **)GetWindowLong(hwnd, GWL_USERDATA))) //{ // // Unembed the browser object, and release its resources. // pBrowserObject = *ppBrowserHandle; //} ////////////////////////////////////////////////////////////////////////// // We want to get the base address (ie, a pointer) to the IWebBrowser2 object embedded within the browser // object, so we can call some of the functions in the former's table. if (!m_pBrowserObj->QueryInterface(IID_IWebBrowser2, (void**)&pWebBrowser2)) { // Our URL (ie, web address, such as "http://www.microsoft.com" or an HTM filename on disk // such as "c:/myfile.htm") must be passed to the IWebBrowser2's Navigate2() function as a BSTR. // A BSTR is like a pascal version of a double-byte character string. In other words, the // first unsigned short is a count of how many characters are in the string, and then this // is followed by those characters, each expressed as an unsigned short (rather than a // char). The string is not nul-terminated. The OS function SysAllocString can allocate and // copy a UNICODE C string to a BSTR. Of course, we'll need to free that BSTR after we're done // with it. If we're not using UNICODE, we first have to convert to a UNICODE string. // // What's more, our BSTR needs to be stuffed into a VARIENT struct, and that VARIENT struct is // then passed to Navigate2(). Why? The VARIENT struct makes it possible to define generic // 'datatypes' that can be used with all languages. Not all languages support things like // nul-terminated C strings. So, by using a VARIENT, whose first field tells what sort of // data (ie, string, float, etc) is in the VARIENT, COM interfaces can be used by just about // any language. VariantInit(&varMyURL); varMyURL.vt = VT_BSTR; #ifndef UNICODE { wchar_t *buffer; DWORD size; size = MultiByteToWideChar(CP_ACP, 0, webPageName, -1, 0, 0); if (!(buffer = (wchar_t *)GlobalAlloc(GMEM_FIXED, sizeof(wchar_t) * size))) goto badalloc; MultiByteToWideChar(CP_ACP, 0, webPageName, -1, buffer, size); varMyURL.bstrVal = SysAllocString(buffer); GlobalFree(buffer); } #else varMyURL.bstrVal = SysAllocString(webPageName); #endif if (!varMyURL.bstrVal) { #ifndef UNICODE badalloc: #endif pWebBrowser2->Release(); return -6; } // Call the Navigate2() function to actually display the page. pWebBrowser2->Navigate2(&varMyURL, 0, 0, 0, 0); // Free any resources (including the BSTR we allocated above). VariantClear(&varMyURL); // We no longer need the IWebBrowser2 object (ie, we don't plan to call any more functions in it, // so we can release our hold on it). Note that we'll still maintain our hold on the browser // object. pWebBrowser2->Release(); // Success return 0; } // failed return -5; } long CWebBrowser::EmbedBrowserObject(HWND hwnd) { IWebBrowser2 *pWebBrowser2; RECT rect; CWBStorage Storage; CWBClientSite *pClientSite = new CWBClientSite(hwnd); // Get a pointer to the browser object and lock it down (so it doesn't "disappear" while we're using // it in this program). We do this by calling the OS function OleCreate(). // // NOTE: We need this pointer to interact with and control the browser. With normal WIN32 controls such as a // Static, Edit, Combobox, etc, you obtain an HWND and send messages to it with SendMessage(). Not so with // the browser object. You need to get a pointer to its base class (as returned by OleCreate()). // // For example, the browser object happens to have a SetHostNames() function we want to call. So, after we // retrieve the pointer to the browser object (in a local we'll name 'm_pBrowserObj'), then we can call that // function, and pass it args, as so: // // m_pBrowserObj->SetHostNames(SomeString, SomeString); if (!OleCreate(CLSID_WebBrowser, IID_IOleObject, OLERENDER_DRAW, 0, (IOleClientSite *)pClientSite, &Storage, (void**)&m_pBrowserObj)) { ////////////////////////////////////////////////////////////////////////// // // Not implemented for multiple browsing... // // Ok, we now have the pointer to the browser object in 'browserObject'. Let's save this in the // memory block we allocated above, and then save the pointer to that whole thing in our window's // USERDATA field. That way, if we need multiple windows each hosting its own browser object, we can // call EmbedBrowserObject() for each one, and easily associate the appropriate browser object with // its matching window and its own objects containing per-window data. //IOleObject **ppObj = &m_pBrowserObj; //SetWindowLong(hwnd, GWL_USERDATA, (LONG)ppObj); ////////////////////////////////////////////////////////////////////////// // We can now call the browser object's SetHostNames function. SetHostNames lets the browser object know our // application's name and the name of the document in which we're embedding the browser. (Since we have no // document name, we'll pass a 0 for the latter). When the browser object is opened for editing, it displays // these names in its titlebar. // // We are passing 2 args to SetHostNames. Oh yeah, the L is because we need UNICODE strings. // And BTW, the host and document names can be anything you want. m_pBrowserObj->SetHostNames(L"My Host Name", 0); GetClientRect(hwnd, &rect); // Let browser object know that it is embedded in an OLE container. if (!OleSetContainedObject((IUnknown *)m_pBrowserObj, TRUE)) { // Set the display area of our browser control the same as our window's size // and actually put the browser object into our window. if (!m_pBrowserObj->DoVerb(OLEIVERB_SHOW, NULL, (IOleClientSite *)pClientSite, -1, hwnd, &rect)) { // We want to get the base address (ie, a pointer) to the IWebBrowser2 object embedded within the browser // object, so we can call some of the functions in the former's table. For example, one IWebBrowser2 function // we intend to call below will be Navigate2(). So we call the browser object's QueryInterface to get our // pointer to the IWebBrowser2 object. if (!m_pBrowserObj->QueryInterface(IID_IWebBrowser2, (void**)&pWebBrowser2)) { // Let's call several functions in the IWebBrowser2 object to position the browser display area // in our window. The functions we call are put_Left(), put_Top(), put_Width(), and put_Height(). pWebBrowser2->put_Left(0); pWebBrowser2->put_Top(0); pWebBrowser2->put_Width(rect.right); pWebBrowser2->put_Height(rect.bottom); // We no longer need the IWebBrowser2 object (ie, we don't plan to call any more functions in it // right now, so we can release our hold on it). Note that we'll still maintain our hold on the // browser object until we're done with that object. pWebBrowser2->Release(); // Success return 0; } // Something went wrong! // QueryInterface failed } // DoVerb Failed } // OleSetContainedObject failed if (pClientSite) { delete pClientSite; } UnEmbedBrowserObject(hwnd); return -3; } return -2; } 类CWBStorage: /******************************************************************** @created: 2005/12/30 @version: 0.1 @filename: WBStorage.h @file base: WBStorage @file ext: h @author: Pomelo Wu @purpose: CWBStorage, a class of implementation of IStorage @remark: NULL @revision history: -- Date Ver. Modifier Description-- 2006/01/16 0.1 Pomelo Create *********************************************************************/ #ifndef __WBSTORAGE_H__ #define __WBSTORAGE_H__ #include <objidl.h> class CWBStorage : public IStorage { public: // IUnknown methods virtual HRESULT STDMETHODCALLTYPE QueryInterface( /* [in] */ REFIID riid, /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject); virtual ULONG STDMETHODCALLTYPE AddRef( void); virtual ULONG STDMETHODCALLTYPE Release( void); // IStorage methods virtual HRESULT STDMETHODCALLTYPE CreateStream( /* [string][in] */ const OLECHAR *pwcsName, /* [in] */ DWORD grfMode, /* [in] */ DWORD reserved1, /* [in] */ DWORD reserved2, /* [out] */ IStream **ppstm); virtual /* [local] */ HRESULT STDMETHODCALLTYPE OpenStream( /* [string][in] */ const OLECHAR *pwcsName, /* [unique][in] */ void *reserved1, /* [in] */ DWORD grfMode, /* [in] */ DWORD reserved2, /* [out] */ IStream **ppstm); virtual HRESULT STDMETHODCALLTYPE CreateStorage( /* [string][in] */ const OLECHAR *pwcsName, /* [in] */ DWORD grfMode, /* [in] */ DWORD reserved1, /* [in] */ DWORD reserved2, /* [out] */ IStorage **ppstg); virtual HRESULT STDMETHODCALLTYPE OpenStorage( /* [string][unique][in] */ const OLECHAR *pwcsName, /* [unique][in] */ IStorage *pstgPriority, /* [in] */ DWORD grfMode, /* [unique][in] */ SNB snbExclude, /* [in] */ DWORD reserved, /* [out] */ IStorage **ppstg); virtual HRESULT STDMETHODCALLTYPE CopyTo( /* [in] */ DWORD ciidExclude, /* [size_is][unique][in] */ const IID *rgiidExclude, /* [unique][in] */ SNB snbExclude, /* [unique][in] */ IStorage *pstgDest); virtual HRESULT STDMETHODCALLTYPE MoveElementTo( /* [string][in] */ const OLECHAR *pwcsName, /* [unique][in] */ IStorage *pstgDest, /* [string][in] */ const OLECHAR *pwcsNewName, /* [in] */ DWORD grfFlags); virtual HRESULT STDMETHODCALLTYPE Commit( /* [in] */ DWORD grfCommitFlags); virtual HRESULT STDMETHODCALLTYPE Revert( void); virtual /* [local] */ HRESULT STDMETHODCALLTYPE EnumElements( /* [in] */ DWORD reserved1, /* [size_is][unique][in] */ void *reserved2, /* [in] */ DWORD reserved3, /* [out] */ IEnumSTATSTG **ppenum); virtual HRESULT STDMETHODCALLTYPE DestroyElement( /* [string][in] */ const OLECHAR *pwcsName); virtual HRESULT STDMETHODCALLTYPE RenameElement( /* [string][in] */ const OLECHAR *pwcsOldName, /* [string][in] */ const OLECHAR *pwcsNewName); virtual HRESULT STDMETHODCALLTYPE SetElementTimes( /* [string][unique][in] */ const OLECHAR *pwcsName, /* [unique][in] */ const FILETIME *pctime, /* [unique][in] */ const FILETIME *patime, /* [unique][in] */ const FILETIME *pmtime); virtual HRESULT STDMETHODCALLTYPE SetClass( /* [in] */ REFCLSID clsid); virtual HRESULT STDMETHODCALLTYPE SetStateBits( /* [in] */ DWORD grfStateBits, /* [in] */ DWORD grfMask); virtual HRESULT STDMETHODCALLTYPE Stat( /* [out] */ STATSTG *pstatstg, /* [in] */ DWORD grfStatFlag); }; #endif //__WBSTORAGE_H__ /******************************************************************** @created: 2006/01/16 @version: 0.1 @filename: WBStorage.cpp @file base: WBStorage @file ext: cpp @author: Pomelo Wu @purpose: Implementation for class CWBStorage @remark: NULL @revision history: -- Date Ver. Modifier Description-- 2006/01/16 0.1 Pomelo Create *********************************************************************/ #include "WBStorage.h" ////////////////////////////////////// CWBStorage functions ///////////////////////////////////////// // NOTE: The browser object doesn't use the IStorage functions, so most of these are us just returning // E_NOTIMPL so that anyone who *does* call these functions knows nothing is being done here. HRESULT STDMETHODCALLTYPE CWBStorage::QueryInterface( /* [in] */ REFIID riid, /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject) { NOTIMPLEMENTED; } ULONG STDMETHODCALLTYPE CWBStorage::AddRef( void) { return 1; } ULONG STDMETHODCALLTYPE CWBStorage::Release( void) { return 0; } HRESULT STDMETHODCALLTYPE CWBStorage::CreateStream( /* [string][in] */ const OLECHAR *pwcsName, /* [in] */ DWORD grfMode, /* [in] */ DWORD reserved1, /* [in] */ DWORD reserved2, /* [out] */ IStream **ppstm) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::OpenStream( /* [string][in] */ const OLECHAR *pwcsName, /* [unique][in] */ void *reserved1, /* [in] */ DWORD grfMode, /* [in] */ DWORD reserved2, /* [out] */ IStream **ppstm) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::CreateStorage( /* [string][in] */ const OLECHAR *pwcsName, /* [in] */ DWORD grfMode, /* [in] */ DWORD reserved1, /* [in] */ DWORD reserved2, /* [out] */ IStorage **ppstg) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::OpenStorage( /* [string][unique][in] */ const OLECHAR *pwcsName, /* [unique][in] */ IStorage *pstgPriority, /* [in] */ DWORD grfMode, /* [unique][in] */ SNB snbExclude, /* [in] */ DWORD reserved, /* [out] */ IStorage **ppstg) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::CopyTo( /* [in] */ DWORD ciidExclude, /* [size_is][unique][in] */ const IID *rgiidExclude, /* [unique][in] */ SNB snbExclude, /* [unique][in] */ IStorage *pstgDest) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::MoveElementTo( /* [string][in] */ const OLECHAR *pwcsName, /* [unique][in] */ IStorage *pstgDest, /* [string][in] */ const OLECHAR *pwcsNewName, /* [in] */ DWORD grfFlags) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::Commit( /* [in] */ DWORD grfCommitFlags) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::Revert( void) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::EnumElements( /* [in] */ DWORD reserved1, /* [size_is][unique][in] */ void *reserved2, /* [in] */ DWORD reserved3, /* [out] */ IEnumSTATSTG **ppenum) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::DestroyElement( /* [string][in] */ const OLECHAR *pwcsName) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::RenameElement( /* [string][in] */ const OLECHAR *pwcsOldName, /* [string][in] */ const OLECHAR *pwcsNewName) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::SetElementTimes( /* [string][unique][in] */ const OLECHAR *pwcsName, /* [unique][in] */ const FILETIME *pctime, /* [unique][in] */ const FILETIME *patime, /* [unique][in] */ const FILETIME *pmtime) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::SetClass( /* [in] */ REFCLSID clsid) { //NOTIMPLEMENTED; return(S_OK); } HRESULT STDMETHODCALLTYPE CWBStorage::SetStateBits( /* [in] */ DWORD grfStateBits, /* [in] */ DWORD grfMask) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBStorage::Stat( /* [out] */ STATSTG *pstatstg, /* [in] */ DWORD grfStatFlag) { NOTIMPLEMENTED; } 类CWBClientSite: /******************************************************************** @created: 2006/01/16 @version: 0.1 @filename: WBClientSite.h @file base: WBClientSite @file ext: h @author: Pomelo Wu @purpose: Declaration for class CWBClientSite, a implemetation of interface IOleClientSite, IOleInPlaceFrame and IOleInPlaceSite @remark: NULL @revision history: -- Date Ver. Modifier Description-- 2006/01/16 0.1 Pomelo Create *********************************************************************/ #ifndef __WBCLIENTSITE_H__ #define __WBCLIENTSITE_H__ #include <oleidl.h> class CWBClientSite : public IOleClientSite, public IOleInPlaceFrame, public IOleInPlaceSite { public: // constructors CWBClientSite() {} CWBClientSite(HWND hwnd) { m_hWindow = hwnd; } // // IUnknown methods // virtual HRESULT STDMETHODCALLTYPE QueryInterface( /* [in] */ REFIID riid, /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject); virtual ULONG STDMETHODCALLTYPE AddRef( void); virtual ULONG STDMETHODCALLTYPE Release( void); // // IOleClientSite methods // virtual HRESULT STDMETHODCALLTYPE SaveObject( void); virtual HRESULT STDMETHODCALLTYPE GetMoniker( /* [in] */ DWORD dwAssign, /* [in] */ DWORD dwWhichMoniker, /* [out] */ IMoniker **ppmk); virtual HRESULT STDMETHODCALLTYPE GetContainer( /* [out] */ IOleContainer **ppContainer); virtual HRESULT STDMETHODCALLTYPE ShowObject( void); virtual HRESULT STDMETHODCALLTYPE OnShowWindow( /* [in] */ BOOL fShow); virtual HRESULT STDMETHODCALLTYPE RequestNewObjectLayout( void); // // IOleWindow methods // virtual /* [input_sync] */ HRESULT STDMETHODCALLTYPE GetWindow( /* [out] */ HWND *phwnd); virtual HRESULT STDMETHODCALLTYPE ContextSensitiveHelp( /* [in] */ BOOL fEnterMode); // // IOleInPlaceUIWindow methods // virtual /* [input_sync] */ HRESULT STDMETHODCALLTYPE GetBorder( /* [out] */ LPRECT lprectBorder); virtual /* [input_sync] */ HRESULT STDMETHODCALLTYPE RequestBorderSpace( /* [unique][in] */ LPCBORDERWIDTHS pborderwidths); virtual /* [input_sync] */ HRESULT STDMETHODCALLTYPE SetBorderSpace( /* [unique][in] */ LPCBORDERWIDTHS pborderwidths); virtual HRESULT STDMETHODCALLTYPE SetActiveObject( /* [unique][in] */ IOleInPlaceActiveObject *pActiveObject, /* [unique][string][in] */ LPCOLESTR pszObjName); // // IOleInPlaceFrame methods // virtual HRESULT STDMETHODCALLTYPE InsertMenus( /* [in] */ HMENU hmenuShared, /* [out][in] */ LPOLEMENUGROUPWIDTHS lpMenuWidths); virtual /* [input_sync] */ HRESULT STDMETHODCALLTYPE SetMenu( /* [in] */ HMENU hmenuShared, /* [in] */ HOLEMENU holemenu, /* [in] */ HWND hwndActiveObject); virtual HRESULT STDMETHODCALLTYPE RemoveMenus( /* [in] */ HMENU hmenuShared); virtual /* [input_sync] */ HRESULT STDMETHODCALLTYPE SetStatusText( /* [unique][in] */ LPCOLESTR pszStatusText); virtual HRESULT STDMETHODCALLTYPE EnableModeless( /* [in] */ BOOL fEnable); virtual HRESULT STDMETHODCALLTYPE TranslateAccelerator( /* [in] */ LPMSG lpmsg, /* [in] */ WORD wID); // // IOleInPlaceSite methods // virtual HRESULT STDMETHODCALLTYPE CanInPlaceActivate( void); virtual HRESULT STDMETHODCALLTYPE OnInPlaceActivate( void); virtual HRESULT STDMETHODCALLTYPE OnUIActivate( void); virtual HRESULT STDMETHODCALLTYPE GetWindowContext( /* [out] */ IOleInPlaceFrame **ppFrame, /* [out] */ IOleInPlaceUIWindow **ppDoc, /* [out] */ LPRECT lprcPosRect, /* [out] */ LPRECT lprcClipRect, /* [out][in] */ LPOLEINPLACEFRAMEINFO lpFrameInfo); virtual HRESULT STDMETHODCALLTYPE Scroll( /* [in] */ SIZE scrollExtant); virtual HRESULT STDMETHODCALLTYPE OnUIDeactivate( /* [in] */ BOOL fUndoable); virtual HRESULT STDMETHODCALLTYPE OnInPlaceDeactivate( void); virtual HRESULT STDMETHODCALLTYPE DiscardUndoState( void); virtual HRESULT STDMETHODCALLTYPE DeactivateAndUndo( void); virtual HRESULT STDMETHODCALLTYPE OnPosRectChange( /* [in] */ LPCRECT lprcPosRect); /* ////////////////////////////////////////////////// // We don't want those functions to access global // variables, because then we couldn't use more // than one browser object. (ie, we couldn't have // multiple windows, each with its own embedded // browser object to display a different web page). // // So here is where I added my extra HWND that the // CWBIOleInPlaceFrame function Frame_GetWindow() needs // to access. ////////////////////////////////////////////////// */ HWND m_hWindow; }; #endif //__WBCLIENTSITE_H__ /******************************************************************** @created: 2006/01/16 @version: 0.1 @filename: WBClientSite.cpp @file base: WBClientSite @file ext: cpp @author: Pomelo Wu @purpose: Implementation for class CWBClientSite @remark: NULL @revision history: -- Date Ver. Modifier Description-- 2006/01/16 0.1 Pomelo Create *********************************************************************/ #include "WBClientSite.h" // Implementation of CWBClientSite, a derived class of IOleClientSite, IOleInPlaceFrame, and IOleInPlaceSite HRESULT STDMETHODCALLTYPE CWBClientSite::QueryInterface( /* [in] */ REFIID riid, /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject) { // query the interface of IUnknown if (!memcmp(&riid, &IID_IUnknown, sizeof(GUID))) { *ppvObject = reinterpret_cast<IUnknown *> (this); } // query the interface of IOleClientSite else if (!memcmp(&riid, &IID_IOleClientSite, sizeof(GUID))) { *ppvObject = (IOleClientSite *)this; } // query the interface of IOleInPlaceSite else if (!memcmp(&riid, &IID_IOleInPlaceSite, sizeof(GUID))) { *ppvObject = (IOleInPlaceSite *)this; } // For other types of objects the browser wants, just report that we don't have any such objects. // NOTE: If you want to add additional functionality to your browser hosting, you may need to // provide some more objects here. You'll have to investigate what the browser is asking for // (ie, what REFIID it is passing). else { *ppvObject = 0; return(E_NOINTERFACE); } return(S_OK); } ULONG STDMETHODCALLTYPE CWBClientSite::AddRef( void) { return 1; } ULONG STDMETHODCALLTYPE CWBClientSite::Release( void) { return 0; } // IOleClientSite methods HRESULT STDMETHODCALLTYPE CWBClientSite::SaveObject( void) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBClientSite::GetMoniker( /* [in] */ DWORD dwAssign, /* [in] */ DWORD dwWhichMoniker, /* [out] */ IMoniker **ppmk) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBClientSite::GetContainer( /* [out] */ IOleContainer **ppContainer) { // Tell the browser that we are a simple object and don't support a container *ppContainer = 0; return(E_NOINTERFACE); } HRESULT STDMETHODCALLTYPE CWBClientSite::ShowObject( void) { return(NOERROR); } HRESULT STDMETHODCALLTYPE CWBClientSite::OnShowWindow( /* [in] */ BOOL fShow) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBClientSite::RequestNewObjectLayout( void) { NOTIMPLEMENTED; } // IOleWindow methods HRESULT STDMETHODCALLTYPE CWBClientSite::GetWindow( /* [out] */ HWND *phwnd) { *phwnd = m_hWindow; return S_OK; } HRESULT STDMETHODCALLTYPE CWBClientSite::ContextSensitiveHelp( /* [in] */ BOOL fEnterMode) { NOTIMPLEMENTED; } // IOleInPlaceUIWindow methods HRESULT STDMETHODCALLTYPE CWBClientSite::GetBorder( /* [out] */ LPRECT lprectBorder) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBClientSite::RequestBorderSpace( /* [unique][in] */ LPCBORDERWIDTHS pborderwidths) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBClientSite::SetBorderSpace( /* [unique][in] */ LPCBORDERWIDTHS pborderwidths) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBClientSite::SetActiveObject( /* [unique][in] */ IOleInPlaceActiveObject *pActiveObject, /* [unique][string][in] */ LPCOLESTR pszObjName) { return S_OK; } // IOleInPlaceFrame methods HRESULT STDMETHODCALLTYPE CWBClientSite::InsertMenus( /* [in] */ HMENU hmenuShared, /* [out][in] */ LPOLEMENUGROUPWIDTHS lpMenuWidths) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBClientSite::SetMenu( /* [in] */ HMENU hmenuShared, /* [in] */ HOLEMENU holemenu, /* [in] */ HWND hwndActiveObject) { return S_OK; } HRESULT STDMETHODCALLTYPE CWBClientSite::RemoveMenus( /* [in] */ HMENU hmenuShared) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBClientSite::SetStatusText( /* [unique][in] */ LPCOLESTR pszStatusText) { return S_OK; } HRESULT STDMETHODCALLTYPE CWBClientSite::EnableModeless( /* [in] */ BOOL fEnable) { return S_OK; } HRESULT STDMETHODCALLTYPE CWBClientSite::TranslateAccelerator( /* [in] */ LPMSG lpmsg, /* [in] */ WORD wID) { NOTIMPLEMENTED; } // IOleInPlaceSite methods HRESULT STDMETHODCALLTYPE CWBClientSite::CanInPlaceActivate( void) { return S_OK; } HRESULT STDMETHODCALLTYPE CWBClientSite::OnInPlaceActivate( void) { return S_OK; } HRESULT STDMETHODCALLTYPE CWBClientSite::OnUIActivate( void) { return S_OK; } HRESULT STDMETHODCALLTYPE CWBClientSite::GetWindowContext( /* [out] */ IOleInPlaceFrame **ppFrame, /* [out] */ IOleInPlaceUIWindow **ppDoc, /* [out] */ LPRECT lprcPosRect, /* [out] */ LPRECT lprcClipRect, /* [out][in] */ LPOLEINPLACEFRAMEINFO lpFrameInfo) { *ppFrame = (IOleInPlaceFrame *)this; // maybe incorrect // We have no OLEINPLACEUIWINDOW *ppDoc = 0; // Fill in some other info for the browser lpFrameInfo->fMDIApp = FALSE; lpFrameInfo->hwndFrame = m_hWindow; // maybe incorrect lpFrameInfo->haccel = 0; lpFrameInfo->cAccelEntries = 0; // Give the browser the dimensions of where it can draw. We give it our entire window to fill GetClientRect(lpFrameInfo->hwndFrame, lprcPosRect); GetClientRect(lpFrameInfo->hwndFrame, lprcClipRect); return S_OK; } HRESULT STDMETHODCALLTYPE CWBClientSite::Scroll( /* [in] */ SIZE scrollExtant) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBClientSite::OnUIDeactivate( /* [in] */ BOOL fUndoable) { return S_OK; } HRESULT STDMETHODCALLTYPE CWBClientSite::OnInPlaceDeactivate( void) { return S_OK; } HRESULT STDMETHODCALLTYPE CWBClientSite::DiscardUndoState( void) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBClientSite::DeactivateAndUndo( void) { NOTIMPLEMENTED; } HRESULT STDMETHODCALLTYPE CWBClientSite::OnPosRectChange( /* [in] */ LPCRECT lprcPosRect) { return S_OK; } 调用示例: // Sample.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "Resource.h" #include <mshtml.h> // Defines of stuff like IHTMLDocument2. This is an include file with Visual C 6 and above #include <exdisp.h> // Defines of stuff like IWebBrowser2. This is an include file with Visual C 6 and above #include "WebBrowser.h" #pragma comment (lib, "webbrowser.lib") CWebBrowser *g_pWebBrowser = NULL; #define MAX_LOADSTRING 100 // Global Variables: HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. MSG msg; HACCEL hAccelTable; // COM initialization OleInitialize(NULL); // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_IEMINI, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_IEMINI)); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } OleUninitialize(); return (int) msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // // This function and its usage are only necessary if you want this code // to be compatible with Win32 systems prior to the 'RegisterClassEx' // function that was added to Windows 95. It is important to call this function // so that the application will get 'well formed' small icons associated // with it. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_IEMINI)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_IEMINI); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } // // FUNCTION: InitInstance(HINSTANCE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: //DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); if (g_pWebBrowser) { g_pWebBrowser->DisplayHTMLPage(hWnd, L"http://www.microsoft.com"); } break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); if (g_pWebBrowser) { g_pWebBrowser->UnEmbedBrowserObject(hWnd); delete g_pWebBrowser; } break; case WM_CREATE: g_pWebBrowser = new CWebBrowser; if (g_pWebBrowser) g_pWebBrowser->EmbedBrowserObject(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Message handler for about box. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; } BTW,没有做异常处理。 |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=535