2/05/2012

How To Determine When a Page Is Done Printing in WebBrowser Control


SUMMARY
The printing from a WebBrowser control can be customized by using custom headers and footers ( http://support.microsoft.com/kb/267240) or replacing the default print template. However, using such techniques conflicts with the PRINT_WAITFORCOMPLETION flag, which is used for synchronous printing.

Since Internet Explorer 6.0, the WebBrowser control raises the DWebBrowserEvents2::PrintTemplateTeardown event when a print template has been destroyed. If the printing involves a custom print template, this is a good indicator of print completion. You can create a event handler function in your application for this event to determin if a the WebBrowser control is finished printing a Web page.
MORE INFORMATION

The WebBrowser control launches another thread which hosts another MSHTML document which renders to the printer. When this thread is finished, if a custom print template is involved, the WebBrowser control destroys the print template and raises PrintTemplateTeardown event. This indicates that the WebBrowser control has completed printing the Web page.

If the default print template and the customization of headers and footers are desired, load the template from the source of IExplorer.exe, and use it as a custom template.

To translate the asynchronous printing to synchronous, a message loop need to be created (better if use with a timeout handler) .

BOOL PrintTemplateTeardownFired=FALSE;
//launch printing......
//loop until PrintTemplateTeardown is set to TRUE in the PrintTemplateTeardown event handler
MSG msg;
while (!PrintTemplateTeardownFired&&GetMessage(&msg, NULL, 0, 0) > 0)
{
  TranslateMessage(&msg);
  DispatchMessage(&msg);
}

Reference

About Me



Hi, my name is Sheng Jiang. Most of you know me because I am active in several C++ forums and newsgroups, and publishes technical articles namely MSDN forums, Joycode (http://blog.joycode.com), a Chinese blog site focused on Microsoft technology, and CSDN, the largest Chinese-speaking software programming community in the world.


According to CSDN, I have around 20,000 posts on its forums (http://community.csdn.net), about 1/300 of the total posts. I am the forum moderator of the Visual C++ and .Net forums in CSDN, and the forum moderator of Chinese forums on Microsoft's MSDN forums, where I have around 15,000 posts and a top 10 contributor. I sometimes also take part in discussions in several other technologies, such VB, C#, Java and assembly language, as well as other online forums, including StackOverflow, GotSpeech.Net and Codeproject message boards, but at a lot less frequency. I have around 200 posts per year on StackOverflow, QQ Ask and Microsoft public newsgroups.

I also wrote blogs and publish dozens of articles per year. Frequently updated blogs include the blog.joycode.com/jiangsheng and blog.csdn.net/jiangsheng. I also posted a few articles in CodeProject before. My interested topics include MFC, ATL, C++/CLI, Web developing, Windows Shell, Windows Forms, Windows common controls, Windows Media, Desktop Search, Windows Speech, IE programming, security, C++ tips and Visual Studio tricks.

You can contact me via http://twitter.com/jiangsheng or webbo.com/jiangshengvc

Education
B.S. in Math, Jilin University, Changchun, China
Austin Community College

Employment
InfraHealth
Beijing Flex Sunland Centry
Beijing Encyc Digital
Beijing Sunland Centry

Awards
Microsoft Most Valuable Professional 2004-2012
First class prize of Jilin Province in China Undergraduate Mathematical Contest in Modeling, 1997

9/05/2010

MSDN Forums random thoughts

User Qurrel wants to know how to differentiate mouse clicks in CHTMLView so a context menu can be truly contextual. The documentation of CHTMLView could be more useful if it said when to override the function, not just how to use the function. This requires a collaboration between the Visual C++ team and the Windows SDK team.

User JosexD wants to know how to get the serial number of the motherboard and processor. The thread is marked as answered by a Microsoft Admin a year and a half ago, I guess to express him/herself JosexD twice voted the answer as helpful instead.

User LorenD wants to know why a MPEG-2 video is playing in his DirectShow-based software on Windows XP but not on Windows 7. The answer isn’t satisfactory, but that is how the standard behavior of video playing is. Maybe that’s why so many video players embed ffmpeg instead of relying on DirectShow?

It looks like a lot of people can’t find MSDN forums. Maybe other forums should have better navigation to point user to different brands, like consumer questions to Microsoft Answers, developer questions to MSDN forums, design questions to Expression, and network administrator questions to Technet.

6/24/2010

分析Hilo项目

image

Hilo项目是微软的一个用于演示Windows 7 API和Visual C++ 2010的功能的一个开源项目。这个项目不仅演示了Windows API的使用,而且也提供了设计和开发高性能Windows程序的建议。这个项目的目标在它的twitter主页表现无遗:将开发本地代码重新作为重点。

这个项目是基于免费的Visual C++ Express和Windows 7 SDK,这意味着更多的程序员可以尝试这个项目而不必购买Visual Studio 2010。这有助于扩展这个项目的影响力。不过这也意味着不能使用成熟MFC和ATL类库。作为替代,Hilo项目包含了以下类

  • ComPtr COM指针管理
  • WindowApplication 消息循环处理
  • Window 窗口API封装
  • WindowFactory 窗口类和窗口过程
  • WindowLayout 窗口布局
  • WindowMessageHandler 窗口消息分发

可以很明显地看到,这些类都被放在了一个比较容易重用的DLL模块里面,文档中也允许程序员在自己的程序中直接拿来使用。

由于WIndows Vista/7中WDDM 1.0的驱动是没有GDI硬件加速的,直到WDDM1.1才有有限度的支持,所以在不支持DirectX 10.1的显卡下GDI会很慢。在对于用户界面技术的选择上,Hilo项目组根据这个现象选择了在现代硬件上更快、更节省内存和CPU(当然,这也意味着更耗显存和GPU)的DirectX,而不是比较慢的GDI。这也减少了窗口的数目,整个程序现在只有一个窗口。这样做的好处是发明MFC的消息映射的时候所针对的项目需求,例如大量的窗口、很小内存、派生类需要继承消息处理函数现在都不成为问题了,所以在这唯一的一个窗口的过程里面可以放一个大大的switch语句。

Hilo在模块化这方面做得比较深好,基本上看到类名字就知道这个类是做什么的,不像看文档里面成员函数看到眼花的CWnd,可以看到Hilo对窗口的各个方面处理是分开的。而且虽然Hilo并不是一个COM服务器,但是在耦合的时候是使用COM的接口而不是直接使用类的名字,这样在编译的时候就可以执行类型检同时也保留可扩展性。MFC就做得比较差,不但CObject::IsKindOf需要在运行的时候判断,而且有引用视图类的头文件之前必须先包含文档类的头文件的问题。

比较遗憾的是,在推广本地代码开发方面,Hilo项目似乎做得还不够。Visual C++ Express的使用者主要是学生和业余人员,而这个项目演示的代码复用、COM和模板对于他们来说需要花一些时间来理解。另外,项目的许可协议看起来也让人一头雾水,里面把Hilo描述为软件而不是源代码。不过Visual C++项目组宣布这个项目的时候声称Hilo只是本地开发示例系列的第一个例子,作为原型来说,Hilo的作用在把“酷”这个词和Windows本地开发联系起来这个方面还是做得不错的。上一次VC开发被人称为酷的时间应该是远在98年了吧……

6/11/2010

二十万分之一

因为有些人问起中国互联网状况白皮书的数据,所以做了一些个人统计来做比较。


我在网上发表言论的位置主要是在技术论坛。2000之前的数据没有了,因为网易虚拟社区被解散,以前的帖子也不能访问。不过我贡献的一些精华贴在http://www.moon-soft.com/program/bbs/docelite9532.htm还是可以看到。这里统计的是2000年之后的数据。


从2000年到2010年6月,我在CSDN社区的参与讨论的问题大概有两万,速度大概是每年2000个的样子.根据CSDN的统计(截至2009年5月600万),这相当于CSDN每年总问题数的三百分之一。说起来很奇怪,我都不去灌水乐园的,难道灌水乐园的问题不参与统计?还是被和谐的太多?


我在微软的MSDN论坛参与讨论的问题也有一万多,MSDN论坛是2005年创建的,所以我的发帖速度大概是每年3000个问题,每个问题可能有多个回复,所以发帖量要比这个还要多。另外,在微软的新闻组的帖子有一千多,新闻组是2003年才去,所以速度是每年大概两百多贴,和在QQ问问中回答问题的速度差不多。


非技术论坛方面,在寄托天下和未名这两个海外人比较集中的社区也灌了几千贴,大多是和海外生活有关,例如签证、留学、移民、德克萨斯等,有个标准水车的头衔。在一些游戏论坛的帖子大概有几千,发帖比较多的地方是在机战世界论坛,算下来每年大概冠几百贴没有技术含量的水,数量大概是技术问题的十分之一。


博客方面,每年大概几十贴。比较经常更新的有CSDN和博客堂这两个,新浪和网易的在我这里访问速度太慢,估计是用户太多的原因,所以不怎么用。blogspot和msn space是兴致所至去更新一下。微博客方面,2008年起大概有一千贴的样子,平均每年500贴。新闻评论比较少写,大概一年只有几十贴的样子。总计的话,每年也就600吧。


综合起来,我每年大概要发表6000条帖子、评论和博客。按照国务院发表的中国互联网状况白皮书的数据,这相当于全国人民每年发表的信息的二十万分之一。


题外话


刚才又上机战世界论坛,看见一帖被雷到了:

家长:啊,老师,不好意思,我儿子和女朋友私奔了,我给他们请两天假

这年代还有人学琼瑶小说私奔啊。