How can I host a WebBrowser control in a Windows Form?

We have two suggestions with sample projects how you host a WebBrowser control inside a form and display HTML contents and listen to events such as NavigateComplete or BeforeNavigate. Of course there are many other ways to do this. Download htmlviewer.zip for two sample projects for the suggestions discussed below. 1) The first suggestion is to generate an ActiveX wrapper for shdocvw using the aximp tool. The command line for this tool should be as follows: aximp c:\windows\system32\shdocvw.dll This will generate the following assemblies. Generated Assembly: D:\Syncfusion\faq\HtmlBrowser\HtmlViewer2\SHDocVw.dll Generated Assembly: D:\Syncfusion\faq\HtmlBrowser\HtmlViewer2\AxSHDocVw.dll Now you can reference these dlls in your project and use AxWebBrowser. In the attached HtmlViewer2 sample we have derived a HtmlControl class from AxWebBrowser and added some properties that let you specify a CSS Stylesheet and the Html content as a string. 2) Our second sample lets you bypass the generation of a ActiveX wrapper. You don’t have to include and ship shdocvw.dll and axshdocvw.dll. In the attached HtmlViewer sample, we derived from AxHost and attached our own IWebBrowserEvents interface by overriding the CreateSink, AttachInterfaces and DetachSink methods. You can use HtmlControl in your form and specify HTML content by assigning a HTML string to HtmlControl. A cascading style sheet can be specified by assigning a path name to the CascadingStyleSheet property. The sample demonstrates how to use a CSS style sheet that has been embedded as a resource in the assembly.

How do I draw rotated text

private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.TranslateTransform(100.0f, 100.0f); g.RotateTransform(-90.0f); g.DrawString(‘Vertical Text’, Font, Brushes.Blue, 0.0f, 0.0f); g.ResetTransform(); g.TranslateTransform(100.0f, 100.0f); g.RotateTransform(-45.0f); g.DrawString(‘Slanted Text’, new Font(Font, FontStyle.Bold), Brushes.Red, 0.0f, 0.0f); g.ResetTransform(); }

What is alpha blending

Alpha-blending refers to allowing a background color to show through a particular color. You use the static Color.FromArgb method to create a alpha-blended color. For example, SolidBrush redBrushSolid = new SolidBrush(Color.FromArgb(255, 255, 0, 0)); SolidBrush redBrushMedium = new SolidBrush(Color.FromArgb(120, 255, 0, 0)); SolidBrush redBrushLight = new SolidBrush(Color.FromArgb(60, 255, 0, 0)); creates three red brushes. The first argument is the alpha-blending value, from 0 to 255. The last three arguments are the RGB values, denoting in this case, red. In the picture below, all three circles use the color red, but each circle has a different alpha blending setting, allowing the white background to show through.

Is there a way to halt a screen from painting until all the controls on the form are initialized

Shawn Burke responded to this question in a posting on microsoft.public.dotnet.framework.windowsforms newsgroup. There is not currently a way to do this built into the framework, but WM_SETREDRAW will do what you’re looking for. It can’t be called recursively, so here’s code for a property you can add to your form to handle it. A VB sample is also available. int paintFrozen; private const int WM_SETREDRAW = 0xB; [DllImport(”User32”)] private static extern bool SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); private bool FreezePainting { get { return paintFrozen > 0; } set { if (value && IsHandleCreated && this.Visible) { if (0 == paintFrozen++) { SendMessage(Handle, WM_SETREDRAW, 0, 0); } } if (!value) { if (paintFrozen == 0) { return; } if (0 == –paintFrozen) { SendMessage(Handle, WM_SETREDRAW, 1, 0); Invalidate(true); } } } }

Where can I find a succinct discussion of Windows Forms

One place to look is Jeff Prosise’s article, Windows Forms: A Modern-Day Programming Model for Writing GUI Applications , from the Feb 2001 MSDN magazine. A second place is Shawn Burke’s article Using the Microsoft .NET Framework to Create Windows-based  Applications found in the MSDN Library.