In Visual C #. NET to create your own address bar

Source: Internet
Author: User
Tags bool file system
Visual article content:

Overview

Create your own Address bar

Establish the sample program

At last

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

Overview:
This article describes how to build a simple, commonly used user control-the address bar.

Believe that as long as the Internet friends, all know ie there is a provision for you to enter the site you want to go to the input box. In the input box, you only need to enter a part of the character, which in its Drop-down list box, will display the content related to your input (memory function).

If you only require an input string. Then, we can complete the input box directly using the textbox and other controls. But if you want your input frame to have memory function. All we need, then, is to be able to read what was previously entered.

Well, that's a long time to talk nonsense. So, let's start by explaining how to make our address bar memory-capable.

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

To create your own address bar:
First of all, we have to take two steps.

The first step is to understand where the historical memory of our IE Address bar comes from. Because only know where it comes from, we can understand our data.

So, let's take a look at what IE has in Regedit (the registry). Because Regeidt is a very good database in Windows (^_^), it can put the whole machine related to some things are stored inside.

In regedit, these are related to ie:




Of course, this is only part of it, and part of it is:



What we want is the "Software\Microsoft\Internet explorer\typedurls" data in the first picture. Otherwise, we write the memory function can not play any role. Or, some other data appears. You know, the data stored in the Regedit can be some key data. If the person accidentally xx dropped, then, L.

OK, now we've found out where the data we're looking for is coming from. So we're going to start building our own memory-enabled address bar.

Sure, is that enough? Of course, enough is enough. But don't you want your address bar to be a little more powerful? So, let's write a class like this to look at:

1, new project, choose New Class Library, the name of Shun Yi. For example: Controlset.urlcontrol.

2, in the resource management to add Reference System.Windows.Forms.dll.

3, then, in the Explorer, change the Class1.cs to UnManagedMethods.cs, and then replace it 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 Hwndedit;

Public IntPtr hwndlist;

}



<summary>

All unmanaged DllImport methods used at this Assembly

</summary>

Internal class Unmanagedmethods

{

[DllImport ("User32.dll")]

Internal static extern bool Getcomboboxinfo (IntPtr Hwndcombo, ref comboboxinfo info);



[DllImport ("Shlwapi.dll")]

Internal static extern void Shautocomplete (INTPTR hwnd, INTPTR flags);

}

}




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

Because we want to have the memory function, then, of course, have a thing that can pull down. What the? ComboBox is the best choice. Well, we started using ComboBox to build our own controls.

Namespace Controlset.urlcontrol

{

<summary>

A control This extends the regular combo box to show URLs.

</summary>

public class Urlcombobox:combobox

{

<summary>

Initilaizes a new instance of Urlcombobox

</summary>

Public Urlcombobox (): Base ()

{

}

}

}




First, we add the following references:

Using Microsoft.Win32;




To use some of the following in the control, we add the following code to it (added to the namespace):

<summary>

A Simple enumeration that wraps various auto complete flags of Shautocomplete.

Documenation of Shautocomplete for details

</summary>

[Flags]

public enum Autocompleteflags:int

{

<summary>

This includes the File System as the rest of the shell (Desktop\my computer\control panel\)

</summary>

FileSystem = 0x00000001,

<summary>

URLs in the User ' s History

</summary>

Urlhistory = 0x00000002,

<summary>

URLs in the User ' s recently Used list.

</summary>

Urlmru = 0x00000004,

<summary>

Use the tab to move thru the autocomplete possibilities instead of "to" next Dialog/window control.

</summary>

Usetab = 0x00000008,

<summary>

This includes the File System

</summary>

Filesystemonly = 0x00000010,

<summary>

Same as Filesystemonly except it only includes directories, UNC servers, and UNC server shares.

</summary>

Filesystemdirs = 0x00000020,

<summary>

Ignore the registry default and force the auto suggest feature on.

</summary>

Autosuggestforceon = 0x10000000,

<summary>

Ignore the registry default and force the auto suggest feature off

</summary>

Autosuggestforceoff = 0x20000000,

<summary>

Ignore the registry default and force the auto append on.

</summary>

Autoappendforceon = 0x40000000,

<summary>

Ignore the registry default and force auto append off.

</summary>

Autoappendforceoff =-2147483648

}



<summary>

Enumeration for possible types of registry base keys for storing most recntly typed URLs

</summary>

public enum Mrukeyhive:int

{

<summary>

Value that indicates HKEY_CURRENT_USER should is used for Mrukey property

</summary>

CurrentUser = 1,

<summary>

Value that indicates HKEY_LOCAL_MACHINE should is used for Mrukey property

</summary>

LocalMachine = 2,

}


Then, in the class, load the following code to complete the function it should have:

<summary>

A control This extends the regular combo box to show URLs.

</summary>

public class Urlcombobox:combobox

{

<summary>

Member variable which stores the AutoComplete flags

</summary>

Private Autocompleteflags _flags = Autocompleteflags.filesystem | Autocompleteflags.urlhistory | Autocompleteflags.urlmru;

<summary>

Member variable which stores the MRU key

</summary>

private string _mrukey = @ "Software\Microsoft\Internet Explorer\TypedURLs";

<summary>

Member variable which stores the MRU key hive

</summary>

Private mrukeyhive _mrukeyhive = Mrukeyhive.currentuser;



<summary>

Initilaizes a new instance of Urlcombobox

</summary>

Public Urlcombobox (): Base ()

{

}



<summary>

Gets the registry key where MRU URLs are stored

</summary>

<param name= "writable" >indicates whether to get the key and so this it values written to it</param>

<returns>registrykey object for the MRU registry key or null if none exists</returns>

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;

}



<summary>

Writes information about any ignored exception to the trace.

</summary>

<param name= "E" >the exception which is being ignored</param>

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);

}



<summary>

Utility function to fill the Combob box most recently typed URL read from registry.

</summary>

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 (Strformat, i), DefaultValue))!= DefaultValue)

{

Items.Add (URL);

i++;

}

}

}

catch (Exception e)

{

Traceignorederror (e);

}

Finally

{

if (Mrukey!= null)

Mrukey.close ();

}

}



<summary>

Gets or sets the auto complete flags

</summary>

[Description ("Gets or sets the auto complete flags")]

Public Autocompleteflags Flags

{

Get

{

return _flags;

}

Set

{

_flags = value;

}

}



<summary>

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

</summary>

[DescriptionAttribute ("The registry key name where the combo box maintains MRU list")]

public string Mrukey

{

Get

{

return _mrukey;

}

Set

{

_mrukey = value;

}

}



<summary>

Gets or sets the registry key hive for the Mrukey property.

</summary>

[DescriptionAttribute ("The registry hive where the combo box maintains MRU list")]

Public Mrukeyhive mrukeyhive

{

Get

{

return _mrukeyhive;

}

Set

{

_mrukeyhive = value;

}

}



<summary>

Writes the recntly typed URL to the registry if it's not already there

</summary>

<param name= "E" ></param>

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), Text);

}

catch (Exception ex)

{

Traceignorederror (ex);

}

Finally

{

if (Mrukey!= null)

Mrukey.close ();

}

}



Base. Onvalidated (e);

}



<summary>

Finds the handle to the "edit control" and calls Shautocomplete on it.

Also fills the ComboBox from the values read from the registry

</summary>

<param name= "E" >Ignored</param>

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);



if (Unmanagedmethods.getcomboboxinfo (Handle, ref info))

{

Unmanagedmethods.shautocomplete (Info.hwndedit, (INTPTR) _flags);

}



Mrufill ();

}

}


Well, then, up until now. Our memory-enabled address bar has been built.

You can debug the build solution in the menu "Generate (B)".

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

To establish the sample program:
1, new project, select the Windows application, name: Testrulcombobox.

2. We put the controls we need into the toolbox. Right-click above the Toolbox. Add/Remove Items. Open the COM component. The following figure:



3, and then put our own written control also put in the toolbox. As shown above, click the Browse button inside. Locate the directory where you stored your last solution, and then find the dynamic link library file that it generated. You can add it directly to the toolbox.

4. Within the existing project, drag and drop the Microsoft Web Browser Control that you just added to the toolbox, and the control you just wrote, and drag and drop it to the main window. and arrange them.

5, add a control button.

6. Double-click the button to generate the 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, the generation of solutions.

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

At last:
Well, you can also try to make your own, personalized browser. If you want to add other features. That does not belong to the purpose of this article. J





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.