Build your own address bar in Visual C # .NET

xiaoxiao2021-03-06  41

This article:

Overview

Establish your own address bar

Establish an example program

At last

-------------------------------------------------- -------------------------------------------

Overview:

This article describes how to create a simple, commonly used user control-address bar.

I believe that as long as I am online, I know that IE has an input box that provides you enter the website you want to go. In this input box, you only need to enter some characters, which is displayed in its drop-down list box, showing the content (memory function) that you enter.

If you only ask if you want to enter a string. So we can use TEXTBOX and other controls directly to complete the input box. But if you want to make your input box memory function. So, what we need is to ask you to read the previously entered content.

Ok, nonsense is half a day. So, we have begun to explain how to make our address bar have memory.

-------------------------------------------------- -------------------------------------------

Establish your own address bar:

First, we have to walk two steps.

The first step, we must first understand that the content of our IE address bar is from where. Because I only know where it is, we can understand our data.

So let's take a look at it, IE what is in regedit (registry). Because regeidt is a very nice database (^ _ ^) in Windows, it can store some things related to the entire machine in it.

In regedit, related to IE is:

Of course, this is only part, and some are:

We want the data of "Software / Microsoft / Internet Explorer / typeDURLS" in the first picture. Otherwise, we can't do anything about the memory function we write. Or, some other data appear. To know, the data saved in the regedit can be some critical data. If you are not careful to be dropped, then l.

OK, now I have found the data we have to come. So, we have to start building our own address bar with memory features.

Of course, is it enough? Of course, it is enough. But don't you want your address bar to make a powerful? So, we write such a class to see:

1. New projects, choose New Category Library, the name is smooth. For example: ControlSet.urlControl.

2. Add reference system.windows.forms.dll in resource management.

3, then, change the class1.cs to unmanagethods.cs in the resource manager, then replace with the following code:

Using system;

Using system.Runtime.InteropServices;

Namespace Controlset.urlControl

{

[Structlayout (layoutkind.sequential)]

INTERNAL STRUCT RECT

{

Public int LEFT;

Public int top;

Public int right;

Public Int Bottom;

}

[Structlayout (layoutkind.sequential)]

Internal Struct ComboBoxInfo

{

Public int Cbsize;

Public Rect Rcitem; Public Rect RcButton;

Public INTPTR StateButton;

Public INTPTR HWNDCOMBO;

Public intptr hwndite;

Public INTPTR HWNDLIST;

}

///

/// all unmanaged dllimport method Methods Used in this assembly

///

INTERNAL CLASS UNMANAGEDMETHODS

{

[DLLIMPORT ("User32.dll")]]]]]

INTERNAL Static Extern Bool GetcomboBoxInfo (INTPTR HWNDCOMBO);

[DLLIMPORT ("shlwapi.dll")]]]]]

INTERNAL Static Extern void Shautocomplete (INTPTR HWND, INTPTR FLAGS);

}

}

In the second step, our address bar appears. So what do you want to use as its base control?

Because we have to have a memory function, then, of course, there is something that can drop. what? ComboBox is the best choice. Well, we started to build our own control with ComboBox.

Namespace Controlset.urlControl

{

///

/// a Control That Extends The Regular Combo Box To Show Urls.

///

Public Class UrlcomboBOBOX: ComboBox

{

///

/// initilaizes a new instance of urlcombox

///

Public urlcomboBox (): base ()

{

}

}

}

First, we add the following quote:

USING Microsoft.win32;

In this control, you should use some things below, we add the following code (added to the namespace):

///

/// A Simple Enumeration That Wraps Various Auto Complete Flags of ShautoComplete.

/// See Document of Shautocomplete for Details

///

[Flags]

Public Enum AutoCompleteflags: int

{

///

/// this include the file system as well as the rest of the shell (desktop / my computer / control panel /)

///

FILESYSTEM = 0x00000001,

///

/// Urls in the user's history

///

URLHISTORY = 0x00000002,

///

/// Urls in the user's realthtly used list.

///

URLMRU = 0x00000004,

.

///

UseTab = 0x00000008,

///

/// this incruDes the file system

///

FileSystemonly = 0x00000010,

///

/// Same As FileSystemonly Except It Only Includes Directories, UNC Servers, and UNC Server Shares.

///

FileSystemDirs = 0x00000020,

///

/// ignore the registry default and force the auto suggest feature on.

///

AutoSuggestForceon = 0x10000000,

///

/// ignore the registry default and force the auto suggest feature OFF

///

AutoSuggestforceOff = 0x20000000,

///

/// ignore the registry default and force the auto append.

///

AutoAppendForceon = 0x40000000,

///

/// ignore the registry default and force auto append off.

///

AutoAppendForceoff = -2147483648

}

///

/// Enumeration for Possible Types of Registry Base Keys for Storing Most Recntly Typed URLS

///

Public Enum MRUKEYHIVE: INT

{

///

/// Value That INDICES HKEY_CURRENT_USER SHOULD BE Used for MRUKEY PROPERTY

///

Currentuser = 1,

///

/// Value That INDICES HKEY_LOCAL_MACHINE SHOULD BE Used for MRUKEY PROPERTY

///

LocalMachine = 2,

}

Then, then load the following code to complete the function thereof:

///

/// a Control That Extends The Regular Combo Box To Show Urls.

///

Public Class UrlcomboBOBOX: ComboBox

{

///

/// Member Variable Which Stores the AutoComplete Flags ///

Private autocompleteflags _flags = autocpeteflags.Filesystem | AutoCompleteflags.urlhistory | AutoCompleteflags.urlmru

///

///Member Variable Which Stores the MRU Key

///

Private string _Mrukey = @ "Software / Microsoft / Internet Explorer / TypeDURLS";

///

///Member Variable Which Stores The MRU Key Hive

///

Private mrukeyhive _mrukeyhive = mrukeyhive.currentuser;

///

/// initilaizes a new instance of urlcombox

///

Public urlcomboBox (): base ()

{

}

///

/// Gets The Registry Key WHERE MRU URLS Are Stored

///

/// INDICES WHETER TO GET THE Key So That It Values ​​Written to It

/// RegistryKey Object for the MRU REGISTRY Key or Null IF None Exists

Private registryKey getMrukey (Bool Writable)

{

IF (_Mrukey.length == 0)

Return NULL;

RegistryKey Ret = NULL;

Switch (_Mrukeyhive)

{

Case MrukeyHive.localMachine:

Ret = registry.localmachine.opensubKey (_MRUKEY, WRITABLE);

Break;

Case MrukeyHive.currentuser:

Ret = registry.currentuser.opensubKey (_MRUKEY, WRITABLE);

Break;

}

Return Ret;

}

///

/// Writes Information about Any Ignored Exception to the TRACE.

///

/// The Exception Which is being ignored

Private void TRACEIGNOREDERROR (Exception E)

{

// it's ok if there is any error

System.Diagnostics.trace.writeline (E.MESSAGE);

System.diagnostics.trace.writeline (E.STACKTRACE);

}

///

/// Utility Function to Fill The Combob Box Most Recessly Typed Urls Read from Registry.

///

Private void mrufill ()

{

IF (DesignMode)

Return;

RegistryKey Mrukey = NULL;

Try

{

INT i = 1;

String strformat = "URL {0}";

Object defaultValue = string.empty;

Object URL;

Mrukey = GetMrukey (false);

IF (Mrukey! = NULL)

{

While (url = Mrukey.getValue (String.Format, I), DefaultValue))! = defaultValue)

{

Items.Add (URL);

i ;

}

}

}

Catch (Exception E)

{

TraceignRedError (E);

}

Finally

{

IF (Mrukey! = NULL)

Mrukey.close ();

}

}

///

/// Gets or sets the auto complebote flags

///

[Description ("Gets Or Sets The Auto Complete Flags")]]

Public AutoCompleteflags Flags

{

get

{

Return_flags;

}

set

{

_flags = value;

}

}

///

/// Gets or sets the registry key name where the combo box maintains mru list.

///

[DescriptionAttribute ("The Registry Key Name Where The Combo Box MAINTAINS MRU LIST"]]

Public String Mrukey

{

get

{

Return_Mrukey;

}

set

{

_MRUKEY = VALUE;

}

}

///

/// Gets or sets the registry key hive for the Mrukey Property.

///

[DescriptionAttribute ("The Registry Hive Where The Combo Box Maintains Mru List")]]

Public Mrukeyhive Mrukeyhive

{

get

{

Return_Mrukeyhive;

}

set

{

_MrukeyHive = Value;

}

}

///

/// Writes The Recntly Typed Url To The Registry if IT Is Not Already There

///

///

Protected Override Void OnValidated (System.EventArgs E)

{

IF (DesignMode) return;

IF ((Text.length! = 0) && (items.indexof (text) == -1))

{

Items.add (text);

RegistryKey Mrukey = NULL;

// finally add it to the registry

Try

{

Mrukey = GetMrukey (TRUE);

IF (Mrukey! = NULL)

Mrukey.SetValue (String.Format ("URL {0}", items.count);

}

Catch (Exception EX)

{

TraceignRedError (ex);

}

Finally

{

IF (Mrukey! = NULL)

Mrukey.close ();

}

}

Base.onvalidated (e);

}

///

/// Finds The Handle to The Edit Control and Calls Shautocomplete on It.

/// Also Fills The ComboBOBOX from The VALUES Read from The Registry

///

/// ignored

Protected Override Void OnHandlecreated (System.EventArgs E)

{

Base.OnHandlecreated (E);

IF (DesignMode)

Return;

// this is the right place do auto completion

ComboBoxInfo Info = New ComboBoxInfo ();

Info.cbsize = system.runtime.interopservices.Marshal.sizeof (Info);

UNMANAGEDMETHODS.GETCOMBOBOXINFO (HANDLE, REF INFO))

{

UnmanagedMethods.shautocomplete (INFO.HWNDIT, (INTPTR) _FLAGS);

}

Mrufill ();

}

}

Ok, then, until now. Our address bar with memory has been built.

You can debug the generated solution in the menu [Generate (B)].

-------------------------------------------------- -------------------------------------------

Establish an example program:

1. New project, select Windows Applications, Name: Testrulcombobox.

2, let's put the controls we need to the toolbox. Right click on the toolbox. Add / remove. Open the COM component. As shown below:

3, then put the controls we wrote to it in the toolbox. As shown above, click the browsing button inside. Find the directory where you last solution is stored, then find out the dynamic link library files it generated. You can add it directly to the top of the toolbox.

4. In the existing project, add the drag and drop to the Microsoft Web browser control above the toolbox, and drag and drop the main window to the main window. And arrange it.

5, add a button.

6, double-click the button, generate an event, and enter the following code in the event:

Cursor CurrentCursor = Cursor.current; TRY

{

Cursor.current = cursors.waitcursor;

Object arg1 = 0; Object arg2 = ""; object arg3 = ""; object arg4 = "

AxWebBrowser1.naviGate (UrlcomboBox1.text, ref Arg1, Ref Arg2, Ref Arg3, REF ARG4);

}

Finally

{

Cursor.current = CurrentCursor;

}

7, generate a solution.

-------------------------------------------------- -------------------------------------------

At last:

Ok, you can try your own, personalized browser. If you still want to add other features. That is not the purpose of this article. J

转载请注明原文地址:https://www.9cbs.com/read-78258.html

New Post(0)