Original: mr.lu December 10, 2003
Everyone knows that the expression of the web page can be said to be unlimited, and through a wide variety of technologies, such as JScript, CSS, DHTML technology can make your thoughts. The programming of standard applications (such as information management system development) in Windows Forms is convenient, and only needs to be implemented by programming components, and you want to express some colorful things, unless you have learned a deep GDI technology. It may be difficult to implement programming or call three-dimensional scene with Direct3D. Today I will tell you how to call the webpage in the form and control the web element, by the way, telling a small skill to rewind the listbox option in the VB.NET control. The program is running as shown in the figure: The first part, give you how to use the picture to reload the selection of the ListBox control, the key is to use ListBox two events.
DrawItem Occurs when a visual aspect of an owner-drawn ListBox changes. MeasureItem Occurs when an owner-drawn ListBox is created and the sizes of the list items are determined. In both cases described, the set may need to know the properties Listbox that It becomes "Owner-Drawn ListBox", so you need to adjust the following properties.
DrawMode gets or sets the Drawing Mode for the control. Set it to the value in the following table
Member nameDescriptionNormalAll the elements in a control are drawn by the operating system and are of the same size.OwnerDrawFixedAll the elements in the control are drawn manually and are of the same size.OwnerDrawVariableAll the elements in the control are drawn manually and can differ in size Draw a list of ListBox options requires a brush, with a few brushs for filling geometries under System.drawing and System.drawing.drawing2D domain names. System.Drawing.Drawing2D.HatchBrush System.Drawing.Drawing2D.LinearGradientBrush System.Drawing.Drawing2D.PathGradientBrush System.Drawing.SolidBrush System.Drawing.TextureBrush they are inherited from System.Drawing.Brush. Today I can only tell you with pictures Fill the listbox option, only Texturebrush, other pen brush readers can give a non-three as exercises. Step 1: Join the three orientation in an imagelist. And when the form is instantiated, a pen brush is generated as the parameters instantiated as Texturebrush. code show as below:
Public Class Form1 Inherits System.Windows.Forms.Form Private LyBrushes (3) As System.Drawing.TextureBrush 'generates reference Brush Private LyHeights () As Integer = {34, 34, 34}' Listbox items to be redrawn for description height ... Public Sub New () 'form constructor MyBase.New ()' This call is required. InitializeComponent () 'Add any initialization after the InitializeComponent () call evaluateBrush (3)' generated by the Windows form Designer Brush function END SUB
Private Sub evaluateBrush (ByVal evaluatetop As Integer) 'evaluateBrush function implemented Dim i As Integer Dim Tempbg (evaluatetop) As Image Dim f As TextureBrush For i = 0 To evaluatetop - 1 Tempbg (i) = imgLBitems.Images.Item (i) LyBrushes (i) = new texturebrush (Tempbg (i)) Next Step 2, DrawIDE attributes of the ListBox have been set to OwnerDrawVariable, so the DrawItem and MeasureItem events of the ListBox control are received, and the listbox options are drawn with the pen brush in the process function. code show as below:
#Region "lbSelect" 'DrawItem ListBox control processing events and MeasureItem Private Sub lbSelect_DrawItem (ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) _ Handles lbSelect.DrawItem Dim brush1 As Brush e.DrawBackground () brush1 = LyBrushes (e.Index) e.Graphics.FillRectangle (brush1, e.Bounds) e.DrawFocusRectangle () End Sub Private Sub lbSelect_MeasureItem lbSelect.MeasureItem (ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) _ Handles E.ItemHeight = lyHEights (E.index) End Sub # End Region program Run results: Part 2, I will say how to embed the web page in the form and control web pages via control. First of all, a control of Microsoft Web Browser is required to load a normal web page into a form. Once you are determined, you will see this control in Toolbox. Since it is a COM component, .NET will help you encapsulate it into a .NET specification, you will notice that there will be two.dll files in your program folder, put you on Windows Application Under contents. Add web address in the program code is as follows: Private Sub Form1_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim String1 As String Dim String2 As String Dim Len1 As Integer Dim Len2 As Integer Dim Lenmid As integer String1 = system.reflection.Assembly.getexecutingassembly.location 'Get program absolute path LEN1 = len (string1) len2 = len ("bin / myapp.exe") lenmid = len1 - len2 string1 = MID (String1, 1, Lenmid) String2 = string1 "myhtmlfiles / myhtml.htm" gets the webpage absolute path Me.MywebBrowser.naviGate2 (String2) 'Make the web browser control lock web page me.show () me.activate () end Sub Next How to use controls Control web elements, this needs to be added to a new reference in the program. This reference has a lot of interfaces that can help you organize different types of web pages, styles, and events in the webpage, and form a collection in the program. If you want to know more, please find a picture. In my program, the code is as follows:
Public Class Form1Inherits System.Windows.Forms.Form Private LyBrushes (3) As System.Drawing.TextureBrush 'generates reference Brush Private LyHeights () As Integer = {34, 34, 34}' Listbox height for the items to be redrawn description Private htmlDoc as mshtml.IHTMLDocument2 'page can be obtained in all the elements, a collection Private htmlDivElements as mshtml.IHTMLElementCollection' Web Browser control to add a reference to the program of the event DocumentComplete element of the collection, proceed as follows in the handler. Private Sub MyWebBrowser_DocumentComplete (ByVal sender As Object, ByVal e As _ AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles MyWebBrowser.DocumentComplete htmlDoc = MyWebBrowser.Document htmlDivElements = htmlDoc.all.tags ( "DIV") 'obtained in all pages DIV tag element now End Sub You can use our previous ListBox to control web elements.
Private Sub lbSelect_SelectedIndexChanged (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _ lbSelect.SelectedIndexChanged Dim SelectID As Integer SelectID = lbSelect.SelectedIndex Select Case SelectID Case 0 ControlIE (1) 'according to the different page elements function selection control Case 1 Controlie (2) Case 2 Controlie (3) End SelectensEnd Sub