In many software applications, the size, position, and movement of the form are limited. How to correctly display the form on a display with different resolutions, and how to set the form to always be at the top...
038 set the position of the form on the screen
You can set the position of the form on the screen by setting the form attributes. The Left attribute of the form indicates the distance between the form and the Left of the screen, and the Top attribute indicates the distance between the form and the Top of the screen.
Create a project. The default form is Form1. Add a Label Control for Form1, add a TextBox Control to enter the distance from the screen, and add a Button control to set the position of the form on the screen.
Namespace _ 038_SetLocation {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void button#click (object sender, EventArgs e) {this. left = Convert. toInt32 (textBox1.Text); // set the distance between the left edge of the form and the left edge of the screen. this. top = Convert. toInt32 (textBox2.Text); // set the distance between the edge on the form and the edge on the screen }}}
039 always on the top of the form
To implement the form, you only need to set the TopMost attribute of the form to True.
Create a project. The default form is Form1, add a background image for the Form1 form, and set the TopMost attribute of the form to True.
040 form displayed from the bottom right corner of the desktop
It mainly uses the animation display form's API function AnimateWindow. In this function's overload method, different forms based on parameter values are displayed and hidden in different forms.
1. Create a project. The default form is Form1. Add a Windows form and name it MainForm.
2. Add a GroupBox control and two Button controls on the Form1 form. Add a PictureBox control and an ImageList control to the MainForm.
// Form1 Form code namespace _ 040_SoutheastDisplayWindow {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void display_Click (object sender, EventArgs e) {MainForm. instance (). showForm (); // display form} private void close_Click (object sender, EventArgs e) {MainForm. instance (). closeForm (); // hide the form }}}
// MainForm form code namespace _ 040_SoutheastDisplayWindow {public partial class MainForm: System. windows. forms. form {# The variable private System declared by region. drawing. rectangle Rect; // defines an array that stores rectangular boxes. private FormState InfoStyle = FormState. hide; // define the variable to Hide static private MainForm dropDownForm = new MainForm (); // instantiate the current form private static int AW_HIDE = 0x00010000; // This variable indicates the animation hidden form private static int AW_SLIDE = 0x00040000; // This variable indicates the form private static int AW_VER_NEGATIVE = 0x00000008; // This variable indicates that the screen is opened from the bottom up private static int AW_VER_POSITIVE = 0x00000004; // This variable indicates that the screen is opened from the top down # endregion # region the construction method of the form public MainForm () {InitializeComponent (); // initialize the workspace size System. drawing. rectangle rect = System. windows. forms. screen. getWorkingArea (this); // instantiate an object in the current window this. rect = new System. drawing. rectangle (rect. right-this. width-1, rect. bottom-this. height-1, this. width, this. height); // create a work area for the instantiated object} # endregion # region call the API function to display the form [DllImportAttribute ("user32.dll")] private static extern bool AnimateWindow (IntPtr hwnd, int dwTime, int dwFlags); # endregion # region mouse controls image changes private void picturebox#mouseenter (object sender, EventArgs e) {pictureBox1.Image = imageList1.Images [1]; // set the image of the PictureBox control when the mouse enters the PictureBox control} private void picturebox?mouseleave (object sender, EventArgs e) {pictureBox1.Image = imageList1.Images [0]; // set the image of the PictureBox control when the mouse leaves the PictureBox control} # endregion # region defines the enumerated value protected enum FormState that identifies the form movement status {// Hide the form Hide = 0, // Display form Display = 1, // Display form Displaying = 2, // hide the Hiding = 3} # endregion # region in the form and use the property to identify the current status protected FormState FormNowState {get {return this. infoStyle;} // returns the current state set {this. infoStyle = value;} // set the current status of the form} # endregion # region display form public void ShowForm () {switch (this. formNowState) // determines the current state of the form {case FormState. hide: // if (this. height <= this. rect. height-192) // this. setBounds (Rect. x, this. top-192, Rect. width, this. height + 192); // keeps the form moving up else {this. setBounds (Rect. x, Rect. y, Rect. width, Rect. height); // set the border of the current form} AnimateWindow (this. handle, 800, AW_SLIDE + AW_VER_NEGATIVE); // dynamic display of this form break; }}# endregion # region close form public void CloseForm () {AnimateWindow (this. handle, 800, AW_SLIDE + AW_VER_POSITIVE + AW_HIDE); // hide the animation form this. formNowState = FormState. hide; // set the status of the current form to hidden} # endregion # region returns the instantiated object static public MainForm Instance () {return dropDownForm; // return the instantiated object of the current form} # endregion }}