A short message prompt
A short message prompts Toast:
Used to present a short message, which will automatically disappear after the prompt is completed;
Features:
① Pop-up window;
② Short text can be displayed in folded lines;
③ Automatically adjust the size according to the text length;
④ After about 1.5, it will disappear automatically;
2 Pop-up window
In Winform, all windows are implemented using Form, for example:
① General window Window;
② Dialog window Dialog;
③ FloatWindow;
④ Tooltip Tooltip;
⑤ Popup window (such as menu windows, drop-down list windows);
Customize a Form class to define a window
public class myToast:Form { }
This window does not require borders, and its position and size are controlled by yourself.
Create and display window
myToast toast=new myToast(); ("This is a totas!");
Subform code
using System; using ; using ; using ; using ; using .Drawing2D; using ; using ; using ; using ; using ; using ; namespace Pop-up prompts { public partial class Toast : Form { private string message; public Toast() { //Borderless = ; //The background is white = ; } public void ShowMessage(string message) { = message; //Manually specify the location = ; = new Point(0, 0); = new Size(300, 100); //Display window (); } protected override void OnPaint(PaintEventArgs e) { (e); Graphics g = ; int w = , h = ; Rectangle rect = new Rectangle(0, 0, w, h); (-4, -4); //Smooth drawing, anti-aliasing = ; = ; if(message!=null) { StringFormat sf = new StringFormat(); = ; = ; Brush brush = new SolidBrush(); (message, , brush, rect, sf); (); } } } }
Parent form code
using System; using ; using ; using ; using ; using ; using ; using ; using ; namespace Pop-up prompts { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Toast toast = new Toast(); ("This is a toast!"); } } }
3. Window location
When the toast window is displayed, it is equivalent to the center of the main window.
① Find the main window and get the location of the main window;
② Calculate the position of the toast window;
When the toast window is displayed, the focus of the main window is not deprived, and ShowWithoutActivation needs to be rewrite
protected override bool ShowWithoutActivation { get { return true; } }
1 Points and details
① When a control control is known, you can get the window where it is located;
Form form=();
Subform code:
using System; using ; using ; using ; using ; using .Drawing2D; using ; using ; using ; using ; using ; namespace Window location { public partial class Toast : Form { private string message; public Toast() { = ; = ; = false; } //Owner can be a child window or a control public void ShowMessage(Control owner,string message) { = message; = ; = new Size(300, 100); //Find the top window where the owner is located Form form = (); = form; //Center the toast window to center relative to the main window Rectangle fr = new Rectangle(, ); int x = + ( - ) / 2; int y = + ( - ) / 2; = new Point(x, y); (); } //Focus control: This window does not deprive the focus of the main window // Otherwise, when the toast window is activated, the focus of the main window is deprived protected override bool ShowWithoutActivation { get { return true; } } protected override void OnPaint(PaintEventArgs e) { (e); Graphics g = ; int w = , h = ; Rectangle rect = new Rectangle(0, 0, w, h); (-4, -4); //Smooth drawing, anti-aliasing = ; = ; if(message!=null) { StringFormat sf = new StringFormat(); = ; = ; Brush brush = new SolidBrush(); (message, , brush, rect, sf); (); } } } }
Parent form code
using System; using ; using ; using ; using ; using ; using ; using ; using ; namespace Window location { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Toast toast = new Toast(); (button1, "Chinese People's Liberation Army!"); } } }
Four Window size
The size of the Toast window should be adjusted according to the message length, such as:
(button1,"success"); (button1,"Simida"); (button1,"Programming is an art");
Use() to calculate the size of the text
Graphics g=(); SizeF size=(str,,300); ();
Note that the temporarily created Graphics need to be destroyed manually after they are used up.
Summarize
This is the article about the location and size of the C# short message prompt window. For more information about the location of the C# short message prompt window, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!