Make A Window That POPS UP Taskbar

xiaoxiao2021-03-06  58

Make A Window That POPS UP Taskbar

Friends who have used MSN Messager know that when there is a friend connected to the Internet, at the top right of the Taskbar (the taskbar), the whole screen will slowly rise a prompt window.

In fact, it is not difficult to use C # to achieve this effect. The whole thoughts are constantly changing the location of the window, in C #, is to set the Location property of the Form. Let's implement it.

Step 1): Making windows

To complete your design, a form is naturally indispensable. Create a Windows Application, set the form's FORMPORDERSTYLE property to None, showintaskbar property set to false. And make the appropriate interface arrangement (such as setting backgroundImage).

At this point, the form is completed.

Step 2): Add Lostfocus event processing for the form

Due to the settings in STEP 1, this window cannot be turned off (of course, you can lay out some of the controls on the form, and then implement the form of the form, this method is not used), you have to lose focus in the form. Turn it off. Lostfocus events can help you achieve this. The specific implementation is as follows:

//Registration issue

This.lostfocus = New EventHandler (myDialog_lostfocus);

//deal with

Private vid mydialog_lostfocus (Object Sender, Eventargs E)

{

this.close (); // Close the form

}

Step 3) Implement animation

To achieve our goals, it seems to be easy on the surface. In fact, a difficult problem is how to get the position of Taskbar and its height. We know that Taskbar may be located at a location around the screen, while Taskbar's height can be dynamically adjusted.

Here, the solution I use is to use the WIN32 API, in which the FindWindow function is used to get the Taskbar handle (Handle), and get the rectangle of its window via the getWindowRect function. The animation effect is achieved by the cooperation of these two functions.

First add a class nativeAPI, which is used to declare the API to use, as follows:

Public Class NativeApi

{

[DLLIMPORT ("User32.dll", entrypoint = "findwindow")]

Public Static Extern INTPTR FINDWINDOW

String lpclassname,

String lpwindowname

);

[DLLIMPORT ("User32.dll", entrypoint = "getWindowRect")]

Public Static Extern Bool getWindowRect

INTPTR HWND,

Out Rectangle LPRECT

);

}

Of course, in order to be compiled, you have to add:

Using system;

Using system.Runtime.InteropServices;

Using system.drawing;

We know that Taskbar's class name "shell_trayWnd", which calls the FindWindow function to get the window handle is not difficult. With this handle, call getWindowRect to get its window rectangle, it is not a problem, once this window rectangle is obtained, the position of the Taskbar can be determined according to the position of the window rectangle. It seems that everything is very clear, but there is a small problem. You have to pay attention. C # defined Rectangle instance is the use of vertex coordinates, width, and highly expressed, the Rectangle instance returned from the API (this is not the structure instance, but RECT Structure, the definition of this structure here is that J) is defined by two points, ie, two points (LEFT, TOP) and (Right, Bottom). Therefore, be careful when using the returned Rectangle structure instance. The following is the code implemented:

Private rectangle taskbarbound; // surrounded by Taskbar // ...

Private void showit () {

/ / Get the window handle of Taskbar

INTPTR HANDLE = NativeApi.FindWindow ("shell_trayWnd", null;

IF (Handle == INTPTR.ZERO) Return; // Get failed, return

// Get screen resolution

Size screensize = systeminformation.primarymonitorsize;

/ / Get the window rectangle of Taskbar

IF (NativeApi.GetwindowRect (Handle, Out Taskbarbound) {

// Get successful

IF (taskbarbound.x == 0 && Taskbarbound.y == 0) {

IF (taskbarbound.width == Screensize.width)

Showitwhentaskbartop (); // Taskbar is above

Else

Showitwhentaskbarleft (); // Taskbar on the left

}

Else {

IF (taskbarbound.x == 0)

Showitwhentaskbarbottom (); // Taskbar in the following

Else

Showitwhentaskbarright (); // taskbar on the right

}

}

}

// If Taskbar is above

Private void showitwhentaskbartop () {

/ / Set the initial position of the form to the top right and hide within Taskbar

THISLOCATION = New Point (TaskbarBound.width - this.width, taskbarbound.height - this.height);

// Down

While (this.location.y

this.location = new point (this.location.x, this.location.y 1);

}

// If Taskbar is on the left

Private void showitwhentaskbarleft () {

/ / Set the initial position of the form to the lower left, and all outside the screen

THIS.LOCATION = New Point (TaskbarBound.Width);

/ / Shift upward

While (this.location.y> taskbarbound.height - this.height)

This.Location = New Point (this.location.x, this.location.y - 1);

}

// If taskbar is on the right

Private void showitwhentaskbarright () {

/ / Set the initial position of the form to the lower right and all outside the screen

THISLOCATION = New Point (TaskbarBound.x - this.width, taskbarbound.Height);

// Move up

While (this.location.y> taskbarbound.height - this.height)

This.Location = New Point (this.location.x, this.location.y - 1);

}

// If Taskbar is below

Private void showitwhentaskbarbottom () {

/ / Set the initial position of the form to the lower right and hide within Taskbar

this.location = new point (taskbarbound.width - this.width, taskbarbound.Height); // Get the height of Taskbar

INT taskbarheight = taskbarbound.height - taskbarbound.y;

//Move up

While (this.location.y> taskbarbound.height - this.height - taskbarheight)

This.Location = New Point (this.location.x, this.location.y - 1);

}

STEP 4) How to use the showit function

If we add the LOAD event handling, and call the showit function, we can see the effect. However, the effect is not too obvious, the main problem is too fast, and people have no feelings. To this end, I use thread to complete the task:

Using system.threading;

PRIVATE THREAD THSHOW;

// Add LOAD Event Processing

Private Void AboutDialog_Load (Object Sender, System.EventArgs E) {

This.thshow = New Thread (New ThreadStart (Showit);

THIS.THSHOW.START ();

This.lostfocus = New EventHandler (myDialog_lostfocus);

// lostfocus event is added, IDE does not provide you with a simple method

}

Of course, we have to terminate it when the form disappears, so as not to end from memory. I don't plan to add a Close event, but add a paragraph in the DISPOSE function:

Protected Override Void Dispose (BOOL Disposing) {

// This is the end of the thread to terminate

IF (this.thshow! = NULL)

THIS.THSHOW = NULL;

/ / The following is the generated by Ide

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

At this point, the program is fully written, compiled in VS2003. You also feel it ~~

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

New Post(0)