the source code found on the internet is printed by Using VC, but there is a bug. I have added comments. If someone can solve these bugs, please let me know. Code :
/* Function: print the local files that can be opened by any browser and implement the print function @ Surl: file path to be printed (only local files can be opened) BUG: tested, if the webpage has a script error or the browser intercepts the plug-in, the printed content may be blank. Do you have any good solutions? In addition, there is no effect on the page on the webpage (files will not be printed) */bool print_html (const char * Surl) {hresult hr; CLSID; iunknown * punknown = NULL; iwebbrowser2 * pwebbrowser2 = NULL; iolecommandtarget * pcommandtarget = NULL; idispatch * pdispatch = NULL; variant Va; olechar solechar [max_path]; coinitialize (null); terminate (L "internetexplorer. application ", & CLSID); HR = cocreateinstance (CLSID, null, clsctx_all, iid_iunknown, (void **) & Punknown); If (succeeded (HR) {hR = punknown-> QueryInterface (iid_iwebbrowser2, (void **) & pwebbrowser2); If (succeeded (HR )) {variantinit (& VA); VA. vt = vt_i4; VA. lval = 0; multibytetowidechar (cp_acp, 0, Surl,-1, solechar, sizeof solechar); BSTR bstrurl = sysallocstring (solechar); HR = pwebbrowser2-> navigate (bstrurl, & va, & VA); sysfreestring (bstrurl); HR = pwebbrowser2-> get_document (& PDI Spatch); If (succeeded (HR) {hR = pdispatch-> QueryInterface (iid_iolecommandtarget, (void **) & pcommandtarget); If (succeeded (HR )) {hR = pcommandtarget-> exec (null, olecmdid_print, olecmdexecopt_dontpromptuser, null, null); sleep (2000); pcommandtarget-> release ();} else {pdispatch-> release (); pwebbrowser2-> release (); punknown-> release (); couninitialize (); Return false;} pdispatch-> release ();} else {pwebb Rowser2-> release (); punknown-> release (); couninitialize (); Return false;} pwebbrowser2-> release ();} else {punknown-> release (); couninitialize (); Return false;} punknown-> release ();} else {couninitialize (); Return false;} couninitialize (); Return true ;} // whether to print the header and footer // I _sign: 0-print // 1-do not print bool print_header_footer (INT I _sign) {const int key_len = 100; char lp_header [key_len]; char lp_footer [key_len]; char * St R_keyname = "Software \ Microsoft \ Internet Explorer \ pagesetup \"; hkey; int err; If (regopenkeyex (HKEY_CURRENT_USER, str_keyname, 0, key_write, & hkey )! = Error_success) {err = getlasterror (); Return false;} If (I _sign = 0) {memset (lp_header, 0, sizeof (lp_header); memset (lp_footer, 0, sizeof (lp_footer); strcpy (lp_header, "& W & B page number, & P/& P"); // "& W & B page number, & P/& P "; strcpy (lp_footer," & U & B & D "); //" & U & B & D "; if (regsetvalueex (hkey, "Header", 0, REG_SZ, (const unsigned char *) lp_header, strlen (lp_header ))! = Error_success) {regclosekey (hkey); Return false;} If (regsetvalueex (hkey, "footer", 0, REG_SZ, (const unsigned char *) lp_footer, strlen (lp_footer ))! = Error_success) {regclosekey (hkey); Return false;} regclosekey (hkey); Return true;} If (I _sign = 1) {memset (lp_header, 0, sizeof (lp_header); memset (lp_footer, 0, sizeof (lp_footer); If (regsetvalueex (hkey, "Header", 0, REG_SZ, (const unsigned char *) lp_header, strlen (lp_header ))! = Error_success) {regclosekey (hkey); Return false;} If (regsetvalueex (hkey, "footer", 0, REG_SZ, (const unsigned char *) lp_footer, strlen (lp_footer ))! = Error_success) {regclosekey (hkey); Return false;} regclosekey (hkey); Return true;} return true ;}
Tested, print_html is only valid for local files and does not generate any printed files for the entered URLs. In addition, the local file cannot have a script error. Otherwise, the printed file content may be blank.
The principle of this Code is to use a stream tool to open a file and use the print function in the browser menu to print the file. Therefore, if you do not use Microsoft's default Internet assumer.exe, you may not be able to print files. Therefore, you must set ie as the default browser!