Use Visual C # To implement the taskbar Notification Window

Source: Internet
Author: User
Most netizens may have used QQ, MSN, and other chats. Program Their interfaces are quite gorgeous, especially when online users and messages are prompted, there will be a floating form slowly rising from the lower right of the screen, both beautiful and user-friendly, as a programmer, we can't help but ask: how is this actually achieved? This article uses Visual Studio. net c #2005 and. Net Frame drawing technology to implement this taskbar Notification window.

Introduction

The taskbar Notification Window of QQ and MSN is very user-friendly. It can display a notification form with skin Skin without losing the focus of the main form. It will automatically disappear after a period of time, therefore, users do not have to intervene in it. There is no big difference between such a notification form and a general form with a title bar, System icon, and button. The form surface is actually a bitmap, and the floating of the form is a little more complicated, we will use. net Framework's double buffer plot technology (see the author's compilationArticle"Windows form. net Frame plotting technology ") to ensure that the content displayed when a mobile form is smooth and does not flash, use the P/invoke platform call to call the WIN32API function to complete the form display and non-title bar form dragging without obtaining focus. The interfaces of the two bitmap skins are as follows:

Background

The notification window attaches a normal form to a skin. The so-called skin is a bitmap image, which is drawn to the form surface through the onpaintbackground event of the form, before attaching a bitmap, you need to adjust the visible properties of the Form. Because the Drawing operation is for the form customer region, the customer region refers to all the areas under the Form title bar and within the form border, therefore, you need to adjust the border and appearance attribute formborderstyle of the form to none, so that the drawn image will fill the entire form.

First, we will use the region object. The region object can accurately depict the contour range of any desired shape, create a region object using a bitmap image and pass it to the region attribute of the form to display the form according to the profile defined by region. Bitmap files used as skin can be created and edited through any image editing software such as photeshop. Note that the background color of the image must be adjusted to a specific color so that the program can clear it when drawing, the background color here is pink. To allow the region object to create a form based on the Content border of the image, we also need to use the graphicspath class to mark the image outline according to a certain path, and then create a region object based on the path.

Then, the bitmap content is displayed on the surface of the form through the form drawing event. Instead of directly using the onpaintbackground event, we reload this method, the advantage of this is that some low-layer rendering operations are still handed over.. NET Framework, we only need to consider the actual drawing operation. In the onpaintbackground method, we have enabled the dual-buffer plot technology. The so-called technology is to first display or process the image to be displayed on a canvas in the memory, after the operation is complete, place the image displayed on the canvas to the form surface. This mechanism can effectively reduce the appearance of flashes and make the image display smoother. The notification form will be raised from the bottom right of the screen for a period of time and then slowly fall back. Here, the. NET Framework Method screen. getworkingarea (workarearectangle) that returns the size range of the screen area needs to be used.AlgorithmCalculate the initial position before the notification form is displayed. Finally, the text to be displayed is drawn to the form surface according to a certain format and the region range specified by the rectangle object. The notification form is closed by setting an area. When you click with the mouse, the system checks whether the clicked coordinates are in the area. If you are in the area, you can hide the notification formCode.

We have noticed that when the QQ and MSN notification windows are displayed, the focus of the main form is not lost, that is, the program has not shifted its focus to the displayed notification form. After testing, we can call it in any way.. NET Framework provides a form display routine such as: form. show cannot ensure that the focus of the main form is not lost. In the VC environment, we can use the showwindows function of WIN32API to complete complex form display operations,. net Framework does not provide similar methods at all, so can we pass. net Framework calls this API function to display the form? Fortunately. the Net Framework provides P/invoke Platform calls. By using the platform to call such services, managed code can call the unmanaged functions implemented in the dynamic link library and mail their parameters, we can easily display a form without getting the focus. Windows APIs and constant definitions used in the program are stored in winuser. in the H header file, the corresponding dynamic link library file is user32.dll. the dllimportattribute class provided by the net framework defines the imported function, and then it can be conveniently called in the program.

Because we have hidden the title bar of the notification form, we need to manually drag the form. This article describes how to drag a form more efficiently. Some netizens prefer to use mouse events in combination when programming a form that is not dragged by the title bar, however, frequent event response and processing reduce program performance. We will continue to use the underlying Processing Method of WIN32API to solve this problem. This means to send the message clicked on the title bar to the form and simulate the actual drag operation.

We use two timers to display, stay, and hide the form. By setting the speed variable, we can change the display speed and hide speed of the window.

Program Implementation

Start Visual Studio. NET 2005, create a C # windows form application, name the solution as taskbarform, and name the project included as taskbarform. First, create the main form form1 of the program, and add two button controls on it, one is used to display the notification form, and the other ends the program. In solution manager, right-click the project and click "Add-Windows form". we name the newly created form taskbarform.

Under the definition of the taskbarform class, we create a variable for displaying strings and their colors, define several variables of the rectangle object to place the title, prompt content, and the area where you can drag the form and the close button. Then, we need to save the height of the form when it is floating in order to calculate the new height after moving. The intervalvalue variable is used to determine the display and hiding speed of the form. During platform calling, we need to define the constant values for passing to the function in advance. The wm_nclbuttondown and ht_caption constants are used to drag the form, and their values are saved in winuser. in the H header file, the corresponding dynamic link library name is user32.dll. The WIN32API we use is sendmessage, releasecapture, and showwindow. You can use dllimportattribute to import the corresponding functions and redefine them in the program, as shown below:

[Dllimportattribute ("user32.dll")]
Public static extern int sendmessage (intptr hwnd, int MSG, int wparam, int lparam );
// Send the message // The function prototype is defined in winuser. h.
[Dllimportattribute ("user32.dll")]
Public static extern bool releasecapture (); // release the mouse to capture winuser. h
[Dllimportattribute ("user32.dll")] // winuser. h
Private Static extern Boolean showwindow (intptr hwnd, int32 ncmdshow );

Sendmessage sends a message whose title bar is pressed repeatedly to simulate the drag of the Form. showwindow is used to display the form of a specific handle. Note that the second parameter ncmdshow indicates how the form should be displayed, however, we need to display the form without getting the focus. sw_shownoactivate can meet our requirements and continue to be in winuser. search in the H file and find the corresponding value of this constant is 4, so we can call this to display the form:

Showwindow (this. Handle, 4 );

We have created a user-defined function showform to encapsulate the above showwindow to display the form. At the same time, we have passed several rectangle rectangular area objects used, and finally called the showwindows function to display the form, the code snippet is as follows:

Public void showform (string ftitletext, string fcontenttext, rectangle fregionofformtitle, rectangle fregionofformtitlebar, rectangle fregionofformcontent, rectangle fregionofclosebtn)
{
Titletext = ftitletext;
Contenttext = fcontenttext;
Workarearectangle = screen. getworkingarea (workarearectangle );
This. Top = workarearectangle. height + this. height;
Formborderstyle = formborderstyle. None;
Windowstate = formwindowstate. normal;
This. setbounds (workarearectangle. Width-This. Width, workarearectangle. Height-currenttop, this. Width, this. Height );
Currentstate = 1;
Timer1.enabled = true;
Titlerectangle = fregionofformtitle;
Titlebarrectangle = fregionofformtitlebar;
Contentrectangle = fregionofformcontent;
Closebtnrectangle = fregionofclosebtn;
Showwindow (this. Handle, 4); // # define sw_shownoactivate 4
}

The currentstate variable indicates whether the status of the form is displayed, staying, or hidden. The two timers modify the position of the form based on the status of the form. We will use setbounds to perform this operation:

This. setbounds (workarearectangle. Width-This. Width, workarearectangle. Height-currenttop, this. Width, this. Height );

When the form needs to be raised, the top attribute value of the form is continuously reduced. When the form falls back, the form that increases the top attribute value and exceeds the height of the screen disappears. Although the principle is simple, it still needs to be precisely controlled.

 

The setbackgroundbitmap function first saves the background image of the form to the backgroundbitmap variable, and then creates a region based on the contour and transparent color of the bitmap image. bitmaptoregion is used to convert bitmap to region, the program then pays the region value to the region attribute of the form to create an irregular form.

Public void setbackgroundbitmap (image, color transparencycolor)
{
Backgroundbitmap = new Bitmap (image );
Width = backgroundbitmap. width;
Height = backgroundbitmap. height;
Region = bitmaptoregion (backgroundbitmap, transparencycolor );
}

Public region bitmaptoregion (Bitmap bitmap, color transparencycolor)

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.