Recently, due to the need for projects to focus on UI programming, We must process standard Windows message Prompt Windows in order to better display the prompt information to users. We all use USB flash drives, flash drives, and other mobile storage devices in Windows XP. When these devices are plugged in or unplugged, a pale yellow background will be displayed in the taskbar area and a prompt window with a marked style will pop up, this kind of reminder is friendly and beautiful. How can this be achieved? In fact, the principle is not complex. The annotation Prompt window itself is an irregular form. When displayed, it points the arrow of the annotation window to different controls. For example:
In general, the p annotation Prompt window
Annotation Prompt window at the edge of the screen
I. Key Technical Points
As mentioned at the beginning of this article, the "labeled message Prompt window" is actually a form with irregular shapes, but it has more complex attributes and behaviors. The arrow of the annotation points to different positions based on different controls. When the control to be labeled is too close to the edge of the screen, the annotation window automatically adjusts the display position and the length and size of the arrow.
We name the newly created form InfoWindow. Define the intArc and intArrowHeight private variables in the Class header. You can adjust their values to fine-tune the positions and arrows of the prompt window.
The arrow position of the prompt window is no more than four possibilities: top left, top right, bottom left, and bottom right. For this reason, we define the ArrowLocation variable of the enumeration type. According to the prompts, the window is located at different positions on the screen, getArrowLocation can calculate the location of the prompt window and return the appropriate ArrowLocation, which is defined as follows:
......
Public enum ArrowLocation
{
TopLeft,
TopRight,
BottomLeft,
BottomRight
}
The SetInfoWindowRegion function is very important in Form. the Load event is called when loading and displaying the prompt form. After calculating the position and arrow display position of the new Prompt window, call SetBounds to apply the updated position and size to the prompt window, gPath is a private variable of the GraphicsPath type. It indicates the irregular graph path of the annotation window. The path of the graph row is also created based on the position of the prompt window and the position displayed by the arrow. gPath. the AddArc method is used to draw the radians of the four corners of the prompt window. Together with the AddLine method, it depicts the outline of the prompt window including the arrow. After everything is ready, we use this gPath object to pass it to the Region object, after the Region object is assigned to the Region attribute of the Form, the Form has an irregular shape of the annotation style window. Some 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 prompt window. This function must pass the control and text to be displayed attached to the prompt window. Then, AnchorPointFromControl returns the coordinates indicated by the arrow in the prompt Window Based on the control position. The Code is 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 coordinates of the work zone are mapped to the screen coordinates for calculation. The code above is followed by a line indicating that the arrow in the prompt window is displayed at the midpoint of the control to be attached.
Set the background color of the prompt window to Info. The appearance is as follows:
We found the appearance a little awkward. That's right! Because the prompt window lacks a black border! Therefore, you also need to add code in the OnPaint event of the form, as shown below:
Protected override void OnPaint (PaintEventArgs e)
{
Pen p = new Pen (Color. Black, 2 );
E. Graphics. DrawPath (p, gPath );
Base. OnPaint (e );
}
Ii. Program Implementation
Start Visual Studio 2005, create a Windows application project named ShowInfoWindow, and add four Button components, one Label component, one textBox component, and three Panel components, three buttons are used to display the annotation message Prompt window and are attached to the three components respectively. The Code is as follows:
......
Private InfoWindow iw;
......
Private void button#click (object sender, EventArgs e)
{
Iw = new InfoWindow ();
Iw. ShowInfoWindow (label1, "instructions on label components. ");
}
Private void button3_Click (object sender, EventArgs e)
{
Iw = new InfoWindow ();
Iw. ShowInfoWindow (button2, "prompts about Button components. ");
}
Private void button4_Click (object sender, EventArgs e)
{
Iw = new InfoWindow ();
Iw. ShowInfoWindow (textBox1, "prompts for text box components. ");
}
Then, we add a new Windows form in the project, named InfoWindow, set the BackColor of InfoWindow to Info, FormBorderStyle to None, and set ShowIcon and ShowInTaskbar to False, place one Label component and one Button component on the form to display the message content and close the prompt window. For specific implementation, see the source code included in the article.
Iii. Summary
This article demonstrates the creation and display of the annotation message window. It uses GraphicsPath object, Region object, screen coordinate ing, and other methods to effectively implement the appearance and style of the prompt window, the prompt window can be automatically attached to the corresponding control, and the position and size of the prompt window arrow are automatically adjusted based on the position of the attached control on the screen. The demo program runs in Windows XP SP2 and. Net Framework 2.0.