TWebBrowser控件打印网页能否实现程序控制横向打印?

gorlden 2006-06-12 10:39:57
我在程序中打印网页,需要自动实现多页打印,有横向有纵向,调用TWebBrowser.ExecWB()方法打印网页内容,但好像只能通过调出页面设置界面由用户点一下横向还是纵向才能实现打印方向的切换,查了N个地方也没有找到程序控制方向的方法。望各位老大指点一二。
...全文
631 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengfeiwuwq 2006-06-14
  • 打赏
  • 举报
回复
LZ试试TPrintDialog控件能不能帮你实现这个功能,理论上讲在应用程序中是可以通过API来控制的,而TPrintDialog就是封装了部分打印API的功能。
gorlden 2006-06-14
  • 打赏
  • 举报
回复
顶一下,今天再没有解决办法就结贴。。。。
gorlden 2006-06-14
  • 打赏
  • 举报
回复
非常感谢各位鼎立相助!
特别哈哈镜兄,感谢感谢~~~
研究中...
飞天揽月 2006-06-14
  • 打赏
  • 举报
回复
In the above code sample:

SetWebBrowser allows your application to pass in a pointer to the WebBrowser instance. Call SetWebBrowser, specifying NULL to release the reference.
ReadDlgSettings reads the settings in the Page Setup and Print dialog boxes into the data members.
Print writes the settings from the data members back into the Page Setup and Print dialog boxes and starts the printing process.
GetPrinterCount returns the number of printers that are defined on the system. This is valid after calling ReadDlgSettings.
GetDefaultPrinterName returns the name of the default printer, or an empty string if there is no default printer. You should verify that a default printer is available before calling ReadDlgSettings or Print as they will not function properly unless there is at least one printer defined.
GetPrinterName returns the name of a specified printer. Specify an integer from 0 to 1 minus the number returned by GetPrinterCount.
The data members are self-explanatory. They are valid after calling ReadDlgSettings. You can modify any of these values to set custom print settings and then call Print to print with your specified settings.
The CWebBrowserPrint class installs a system hook with thread scope. The hook is only active from the ReadDlgSettings and Print functions. ReadDlgSettings installs the hook, calls ExecWB to invoke the Page Setup and Print dialog boxes, reads the dialog box settings into the class data members, clicks Cancel to dismiss the dialog boxes, and then removes the hook. Print installs the hook, calls ExecWB to invoke the Page Setup and Print dialog boxes, and transfers the changes from the class data members back to the dialog box. Print then prints the Web page, clicks OK to dismiss the dialog boxes, and then removes the hook.
Here is some sample code to illustrate how to use the class in an application. Note that we are checking to make sure there is a default printer before calling ReadDlgSettings or Print.

CWebBrowserPrint wbp;
if (_tcslen(wbp.GetDefaultPrinterName()))
{
wbp.SetWebBrowser(pWebBrowser);
wbp.ReadDlgSettings();
wbp.m_sHeader = _T("My Header");
wbp.m_Orientation = CWebBrowserPrint::Landscape;
wbp.Print();
}

The phookctl sample exposes the printer settings from an ActiveX control. This gives you the ability to set custom print settings from script in a Web page as shown below.

<HTML>
<HEAD>
<OBJECT ID="wbp"
CLASSID="CLSID:778C58A9-81B6-11D3-BB8F-00C04FA3471C">
</OBJECT>
<SCRIPT LANGUAGE="VBScript">
Sub Print()
If Len(wbp.DefaultPrinterName) = 0 Then
MsgBox "No default printer!"
Exit Sub
End If
wbp.Header = "My Header"
wbp.Orientation = 2 ' Landscape
wbp.Print
End Sub
</SCRIPT>
</HEAD>
<BODY>
<INPUT TYPE="BUTTON" VALUE="Print" ONCLICK="Print()">
</BODY>
</HTML>

Note that there is no need to call SetWebBrowser or ReadDlgSettings when using this control on a Web page, because the control can obtain this information on its own. However, SetWebBrowser is exposed so you can set the reference when hosting the WebBrowser control in your own application.

Lastly, the phookvb sample demonstrates using phookctl in a VB application that hosts the WebBrowser control.

飞天揽月 2006-06-14
  • 打赏
  • 举报
回复
Hooking the Page Setup and Print Dialog Boxes
Install a system hook and take full control over the Page Setup and Print dialog boxes.

Advantages Disadvantages
Complete control over all Page Setup and Print dialog box functions. Uses undocumented aspects of the WebBrowser's implementation and is therefore unsupported.

This method uses an old trick called the "CBT" hook, which stands for computer-based training. Windows provides a way to install a system hook that allows an application to receive notifications of certain windows events before the windows themselves are notified. This facility was originally intended to only support online help applications so that they could do things like pop up a help window whenever a certain dialog box is opened by the user. Using this facility to manipulate other applications against their will is usually frowned upon and is not considered to be good practice.

Even though this technique employs very questionable methods, it is probably the smoothest solution of all. You can control any of the settings that are exposed in the user interface of the product.

The following Win32 functions are used to create a system hook. SetWindowsHookEx installs the hook. CallNextHookEx chains a hook procedure to the next hook procedure. UnhookWindowsHookEx removes the hook. Refer to the Win32 documentation for detailed information on these functions. You will also need to implement a hook procedure so that Windows knows where to send the notifications. A skeletal version of a hook procedure is shown below.

LRESULT CALLBACK CbtProc(int nCode, WPARAM wParam, LPARAM lParam)
{
switch (nCode)
{
case HCBT_CREATEWND:
// code to handle WM_CREATE notification
HWND hWnd = (HWND)wParam;
LPCBT_CREATEWND pcbt = (LPCBT_CREATEWND)lParam;
LPCREATESTRUCT pcs = pcbt->lpcs;
break;
case HCBT_ACTIVATE:
// code to handle WM_ACTIVATE notification
break;
}
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}

The documentation for SetWindowsHookEx covers the various notifications that can be received in your hook procedure. For our purposes, we are interested in two notifications-HCBT_CREATEWIN and HCBT_ACTIVATE.

HCBT_CREATEWIN notifies us when a window is about to be created, before it receives its WM_CREATE message. The wParam parameter identifies the HWND of the window and lParam gives us access to the CREATESTRUCT for the window. From the CREATESTRUCT, we can use lpszClass to determine what type of window is being created. We can also alter the position of the window to move it off screen so that the user will not see the dialog.

HCBT_ACTIVATE notifies us when a window is about to receive the WM_ACTIVATE message. It is at this point that we can take control of the dialog. The idea is to send messages to the window to make it think that the user is interacting with the dialog. You can enter text into textboxes, set checkboxes and radio buttons, and click OK or Cancel. The easiest way to identify controls in the dialog is to find out their resource ID's with a tool like Spy++. Then you can call GetDlgItem to obtain the HWND of the control and SendMessage to interact with it.

The following samples are provided to demonstrate how to automatically control the WebBrowser's Page Setup and Print dialog boxes in a number of scenarios. Please note that these samples will not work with Windows 2000 due to changes in the architecture of the Print dialog box.

Sample code Function of this code
Phookmfc (wb_print02.exe) Demonstrates printing with custom print settings from an MFC application.
Phookctl (wb_print03.exe) Demonstrates printing with custom print settings from script on a Web page.
Phookvb (wb_print04.exe) Uses phookctl to print with custom print settings from a VB application.

The code that does all of the dirty work in the samples is implemented in a reusable class named CWebBrowserPrint. This is defined in the WebBrowserPrint.h and WebBrowserPrint.cpp files. This makes it easy to incorporate the ability to set custom print settings in any C++ application that is hosting the WebBrowser control. The public aspects of the class are shown below.

class CWebBrowserPrint
{
public:
enum Orientation {
OrientationUndefined,
OrientationPortrait,
OrientationLandscape };
enum PrintRange {
PrintRangeUndefined,
PrintRangeAll,
PrintRangePages,
PrintRangeSelection };
enum PrintFrames {
PrintFramesUndefined,
PrintFramesScreen,
PrintFramesSelected,
PrintFramesIndividually };

void SetWebBrowser(IWebBrowser2* pWebBrowser);
bool Print();
bool ReadDlgSettings();
CString GetPrinterName(ULONG lIndex);
ULONG GetPrinterCount();
CStrin GetDefaultPrinterName();

// Page Setup dialog settings
CString m_sPaperSize;
CString m_sPaperSource;
CString m_sHeader;
CString m_sFooter;
Orientation m_Orientation;
float m_fLeftMargin;
float m_fTopMargin;
float m_fRightMargin;
float m_fBottomMargin;

// Print dialog settings
CString m_sPrinterName;
bool m_bPrintToFile;
PrintRange m_PrintRange;
ULONG m_lPrintRangePagesFrom;
ULONG m_lPrintRangePagesTo;
ULONG m_lCopies;
bool m_bCollate;
PrintFrames m_PrintFrames;
bool m_bPrintLinks;
bool m_bPrintLinkTable;
};

飞天揽月 2006-06-14
  • 打赏
  • 举报
回复
How can I specify the target printer or set Landscape mode and other print attributes? The WebBrowser does not expose a way to programmatically specify a target printer or any other print attributes; therefore, there is no directly-supported way of programmatically defining these settings from script or from an application hosting the WebBrowser control.
There are two workarounds. The first and most straightforward approach is to hook the Print dialog box and send a message to appropriate window control (that is, send a WM_SETTEXT message to the Printer combo box to select the desired printer). The other approach is to temporarily change the application default printer. Both of these approaches are discussed further in the Printing Workarounds section.

自己用api 写吧
gorlden 2006-06-13
  • 打赏
  • 举报
回复
有没有办法了?其实是这样的,我在delphi程序中用TWebBrowser就是为了实现打印网页的内容,有没有其他办法能够打印出网页的内容呢,可以实现程序控制打印方向的?(不一定非要用TWebBrowser)
gorlden 2006-06-12
  • 打赏
  • 举报
回复
to 哈哈镜:
看了一下iHtmlDocument2接口,好像也没有办法控制打印方向啊
to Delphi被卖:
是的那里可以改变部分的页面设置,但是好像没有打印方向的设置 :(

不过还是非常感谢两位帮忙,再看看还有没有办法?
sxtdxvb 2006-06-12
  • 打赏
  • 举报
回复
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup
可以改变页面设置。但是如果改变的话,会影响IE所有的打印功能。
最好先备份,使用完后恢复。
飞天揽月 2006-06-12
  • 打赏
  • 举报
回复
使用 ihtmldocument2 控制一下

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧