Using C # to implement annotated message prompt window

Source: Internet
Author: User

In recent years, as the project needs to be focused on UI programming, we have to deal with standard Windows message prompts to make it more user-friendly to present the prompts to the user. We all use a U disk under Windows XP, flash memory, such as mobile storage devices, when plugged in or unplug these devices when the taskbar area will display a light yellow background, and the annotation style of the hint window to bounce out, such a hint is friendly and beautiful, then how is this actually achieved? In fact, this is not complicated, the annotation window itself is an irregular form, and when displayed it will point the arrows of the callout window to different controls. The following figure:


P-Callout Prompt window in general

Callout Prompt window for screen edges


   first, technical points

As the "Callout message window" at the beginning of this article is actually a form with irregular shapes, it has more complex properties and behaviors. The callout's arrows point to different positions depending on the control, and when the control you want to annotate is too close to the edge of the screen, the callout window automatically adjusts the display position and the length and size of the arrows.

We name the newly created form Infowindow. Define the INTARC and intarrowheight two private variables in the header of the class, and adjust their values to fine-tune the position of the hint window and the size and position of the arrows.

The arrowhead position of the hint window has nothing more than four possibilities on the upper left, right, bottom left, and right, and we define the variable arrowlocation for this enumeration type, depending on the prompt window in different positions on the screen, Getarrowlocation can calculate the position of the hint window and return the appropriate arrowlocation, as defined below:

......
public enum Arrowlocation
{
TopLeft,
TopRight,
Bottomleft,
BottomRight
}
The Setinfowindowregion function is very important, It is invoked when the Form.Load event loads and displays the prompt form. When the position of the new prompt window is computed and the position of the arrow is displayed, the call SetBounds applies the updated position and size to the prompt window, Gpath is a private variable of the GraphicsPath type, which represents the irregular graphics path of the callout window , the diagram row path is also created according to the location of the prompt window and the position where the arrows appear. The Gpath.addarc method is used to draw the radian portion of the four corners of the prompt window, together with the AddLine method to depict the hint window including the outline of the arrow, and when everything is ready, we pass the Gpath object to the region object, and when the region object is assigned to the form's Region property, the window The body has a callout window style of irregular shape, part of the code is as follows:

private void Setinfowindowregion ()
{
if (!this. ishandlecreated)
Return
System.Drawing.Size windowsize = this. Size;
point[] arrowpoints = new Point[3];
Point topleftpoint = Point.empty;
Point bottomrightpoint = (point) windowsize;
Switch (this. Getarrowlocation)
{
Case Arrowlocation.topleft:
......
Case Arrowlocation.topright:
......
Case Arrowlocation.bottomleft:
......
Case Arrowlocation.bottomright:
......
}
......
......
if (this. Getarrowlocation = = Arrowlocation.topleft) | |
(This. Getarrowlocation = = arrowlocation.topright))
{
Gpath.addarc (Topleftpoint.x, Recty2-arcradius, Arcdia, Arcdia, 90, 90);
Gpath.addline (Topleftpoint.x, rectY2, Topleftpoint.x, rectY1);
Gpath.addarc (Topleftpoint.x, Topleftpoint.y, Arcdia, Arcdia, 180, 90);
Gpath.addline (rectX1, Topleftpoint.y, arrowpoints[0). X, TOPLEFTPOINT.Y);
Gpath.addlines (arrowpoints);
Gpath.addline (Arrowpoints[2]. X, Topleftpoint.y, rectX2, TOPLEFTPOINT.Y);
Gpath.addarc (Rectx2-arcradius, Topleftpoint.y, Arcdia, Arcdia, 270, 90);
Gpath.addline (Bottomrightpoint.x, rectY1, Bottomrightpoint.x, rectY2);
Gpath.addarc (Rectx2-arcradius, Recty2-arcradius, Arcdia, Arcdia, 0, 90);
Gpath.addline (rectX2, Bottomrightpoint.y, rectX1, BOTTOMRIGHTPOINT.Y);
}
Else
{
Gpath.addline (rectX1, Topleftpoint.y, rectX2, TOPLEFTPOINT.Y);
Gpath.addarc (Rectx2-arcradius, Topleftpoint.y, Arcdia, Arcdia, 270, 90);
Gpath.addline (Bottomrightpoint.x, rectY1, Bottomrightpoint.x, rectY2);
Gpath.addarc (Rectx2-arcradius, Recty2-arcradius, Arcdia, Arcdia, 0, 90);
Gpath.addline (rectX2, Bottomrightpoint.y, arrowpoints[0). X, BOTTOMRIGHTPOINT.Y);
Gpath.addlines (arrowpoints);
Gpath.addline (Arrowpoints[2]. X, Bottomrightpoint.y, rectX1, BOTTOMRIGHTPOINT.Y);
Gpath.addarc (Topleftpoint.x, Recty2-arcradius, Arcdia, Arcdia, 90, 90);
Gpath.addline (Topleftpoint.x, rectY2, Topleftpoint.x, rectY1);
Gpath.addarc (Topleftpoint.x, Topleftpoint.y, Arcdia, Arcdia, 180, 90);
}
Gpath.closefigure ();
This. Region = new Region (This.gpath);
}
The Showinfowindow function is used to display the hint window, which needs to pass the control that the prompt window attaches to and the text that needs to be displayed. Then, Anchorpointfromcontrol returns the coordinates of the hint window's arrows according to the position of the control, the code reads as follows:

public static point Anchorpointfromcontrol (Control Anchorcontrol)
{
if (Anchorcontrol = null)
throw new ArgumentException ();
Point controllocation = anchorcontrol.location;
System.Drawing.Size controlsize = anchorcontrol.size;

if (anchorcontrol.parent!= null)
Controllocation = AnchorControl.Parent.PointToScreen (controllocation);
Return controllocation + new Size (CONTROLSIZE.WIDTH/2, CONTROLSIZE.HEIGHT/2);
}
Pointtoscreen indicates that the location of the workspace point is mapped to the screen coordinate uniform for calculation. The preceding code is displayed at the midpoint of the attached control with an arrow in the Line Description prompt window.

Set the background color of the prompt window to info and look like the following figure:


We found this look a little awkward, yes! Because the hint window is missing a black border! Therefore, you also need to add code to the OnPaint event of the form, as follows:

protected override void OnPaint (PaintEventArgs e)
{
Pen p = new Pen (color.black, 2);
E.graphics.drawpath (P, Gpath);
Base. OnPaint (e);
}

   Second, the realization of the program

Start Visual Studio 2005, create a new Windows Application project for Visual C #, and name Showinfowindow, add 4 button components, 1 label components, 1 textbox components, and 3 panel components. The 3 button is used to display the callout Message prompt window and attach to three components respectively, the code is as follows:

......
Private Infowindow IW;
......
private void Button1_Click (object sender, EventArgs e)
{
IW = new Infowindow ();
Iw. Showinfowindow (Label1, "Instructions for the label component.) ");
}
private void Button3_Click (object sender, EventArgs e)
{
IW = new Infowindow ();
Iw. Showinfowindow (button2, "description of the ToolTip for the button component.) ");
}

private void Button4_Click (object sender, EventArgs e)
{
IW = new Infowindow ();
Iw. Showinfowindow (TextBox1, "Instructions for the text box component.) ");
}
Then, we add a new Windows Form to the project, name Infowindow, and set the Infowindow BackColor to Info,formborderstyle set to None, Set both Showicon and ShowInTaskbar to False, placing 1 label components and a button component on the form to display the message content and the action to close the prompt window, respectively. For specific implementation see the article with the source code, no longer detailed here.

   Third, summary

This article demonstrates the creation and display of the annotation Message prompt window, utilizes the GraphicsPath object, the region object and the screen coordinate mapping to implement the prompt window's appearance and the style effectively, the hint window can automatically attach to the corresponding control, and automatically adjusts the position and size of the prompt window arrows based on the position of the attached control on the screen. The demo program runs through Windows XP SP2 and the. Net Framework 2.0 environment.

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.