Embed the program window into the taskbar

Source: Internet
Author: User

Today, let's take a look at how VB. NET embeds the program window into the taskbar, which is our implementation.


Before starting, Meng briefly introduced the composition of the taskbar. Here, Meng only introduced the components related to our program. The entire taskbar has a large window container (the container class name is
Shell_traywnd) is composed of some windows. In the container whose class name is shell_traywnd, there is a class named rebarwindow32 Level 2 capacity.
. Windows contained in the second-level container are different in win7 and WINXP (Windows 7 is used by Meng, but Windows 7 and WINXP have the same effect in this program ). In
WINXP contains a window in which the class name is mstaskswwclass. This window is used to display the taskbar button (that is, the area where you opened the window. Here, Xiao Meng
Minimize the window), including the Quick Start bar (displayed with a small icon on the left, the area where the corresponding program will be started after you click); In win7, only one class name is mstaskswwclass
Windows, because there is no quick start bar in win7, although there is a similar Quick Start area on the left side, but that is not quick start, but part of the minimized window. The above is what we have with our program
The taskbar of the Customs is formed. Xiao Meng has limited expression ability, and you may not understand it. So
Meng posted a picture below.

========================================================== ========================================================== =====

Win7:

Red Area: The class name is the yellow area of the shell_traywnd container. The class name is the second-level container of rebarwindow32.

Green area: the class name is the mstaskswwclass window.

WINXP:

Red Area: Red Area -- yellow area of the container whose class name is shell_traywnd: second-level container whose class name is rebarwindow32

Green Zone: Quick Start Zone
Purple window: The Window whose class name is mstaskswwclass

========================================================== ========================================================== =====

We have finished introducing the composition of the taskbar. Let's talk about the general idea below. First, we need to minimize the width of the window to make room for our program; then, we need to set our program to a subwindow of the taskbar to facilitate embedding in the taskbar; finally, adjust the program size and position in the taskbar.

After so many preparations, the project will be officially started.

Create a Windows form application ]. Change the borderstyle attribute of the form to none to remove the border of the form. Add a text box to the form, as shown in:


Write the code below.

Let's declare the APIs and structures required by the Program (add importssystem. runtime. interopservices to the class first ):

'Look for the first top-level window in the window list that meets the specified conditions. Private declare function findwindow lib "USER32" _ alias "find0000wa" (byval lpclassname as string, _ byval lpwindowname as string) as INTEGER: In the window list, find the first sub-window that matches the specified condition. Private declare function find1_wex lib "USER32" _ alias "find1_wexa" (byval hwnd1 as integer, _ byval hwnd2 as integer, _ byval lpsz1 as string, _ byval lpsz2 as string) as integer specifies the new parent private Decl of a window. Are function setparent lib "USER32" _ alias "setparent" (byval hwndchild as integer, _ byval hwndnewparent as integer) as integer 'change the location and size of the specified window. Top-level windows may be limited by the maximum or minimum size. The size prevails over the parameter private declare function movewindow lib "USER32" _ alias "movewindow" (byval hwnd as intptr, _ byval X as integer, _ byval y as integer, _ byval nwidth as integer, _ byval nheight as integer, _ byval brepaint as integer) as integer 'get the range rectangle of the entire window, the border, title bar, scroll bar, and menu of the window are all in this rectangle. Private declare function getwindowrect lib "USER32" _ alias "getwindowrect" (byval hwnd as intptr, _ byref lprect as rect) as integer <structlayout (layoutkind. sequential, charset: = charset. auto)> _ private structure rect public left as integer public top as integer public right as integer public bottom as integer end structure

To facilitate debugging, we first add the code for program Exit:

    Private Sub cmdTextBox_KeyPress(ByVal sender As Object, ByVal e As _                                      System.Windows.Forms.KeyPressEventArgs) _                                      Handles cmdTextBox.KeyPress          If e.KeyChar = Chr(13) Then              If UCase(cmdTextBox.Text) = "END" Then                  Me.Close()              End If          End If      End Su  

In this way, you can enter "end" in the text box and press enter to exit the program.

Since we need to change the width of the minimized window, we should first look for the handle of the minimized window and declare a global variable to store the handle of the minimized window we found:

    Dim hMin As Long  

Next, we will look for the handle to minimize the window (the following code is in the load event of the form ):

Dim htaskbar, hbar as long htaskbar = findwindow ("handler", vbnullstring) 'find the Class Name: Handler window handle hbar = find1_wex (htaskbar, 0, "rebarwindow32", vbnullstring) 'Find the handle hmin of the second-level Container = findjavaswex (hbar, 0, "mstaskswwclass", vbnullstring) 'to find the handle that minimizes the window

After finding the handle, we can get the coordinates of the second-level container and the coordinates of the minimized window:

Dim rcbar as rect getwindowrect (hmin, rcmin) 'Get the handle getwindowrect (hbar, rcbar)' of the minimal window to get the handle of the second-level container

Next we use the movewindow function to change the width of the window:

    MoveWindow(hMin, 0, 0, rcMin.Right - rcMin.Left - Me.Width, _                 rcMin.Bottom - rcMin.Top, True)  

We have made room for the taskbar. It is time to embed the program into the taskbar:

Setparent (Me. Handle, hbar) 'sets the program window to a subwindow of the taskbar.

Adjust the size and position of the program window:

    Me.Height = cmdTextBox.Height      Me.Width = cmdTextBox.Width      MoveWindow(Me.Handle, rcMin.Right - rcMin.Left - Me.Width + 2, _                (rcBar.Bottom - rcBar.Top - Me.Height) / 2, _                 Me.Width, Me.Height, True)  

The task of embedding the program window into the taskbar has been completed, but we still need to handle the closing event of the form so that when the program is closed, restore the width of the minimized window (the following code is stored in the closing event of the form ):

MoveWindow(hMin, 0, 0, rcMin.Right - rcMin.Left, rcMin.Bottom - rcMin.Top, True) 

Now we have finished. Now we can run it to check the effect, which is the same as the effect at the beginning of the 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.