Source: C # programming book 16.03
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. runtime. interopservices;
Namespace QQ
{
Public partial class form1: Form
{
# Region declares the API functions used in this program
// Obtain the function of the visualization control under the current mouse
[Dllimport ("user32.dll")]
Public static extern int windowfrompoint (INT xpoint, int ypoint );
// Obtain the parent-level function of the specified handle
[Dllimport ("user32.dll", exactspelling = true, charset = charset. Auto)]
Public static extern intptr getparent (intptr hwnd );
// Obtain the screen size
[Dllimport ("user32.dll", entrypoint = "getsystemmetrics")]
Private Static extern int getsystemmetrics (INT mval );
# Endregion
Public form1 ()
{
Initializecomponent ();
}
# Region variables to be declared when running the program
Private intptr currenthandle; // record the handle of the control in the current state.
Private int windowflag; // indicates whether to stretch the form.
Private int intoriheight;
# Endregion
Private void form1_load (Object sender, eventargs E)
{
Intoriheight = This. height;
This. Locate toplocation = new point (794, 0); // locate the current form
Judgewinmouposition. Enabled = true; // The timer judgewinmouposition starts to work.
Listview1.clear ();
Listview1.largeimagelist = imagelist1;
Listview1.items. Add ("pig", "pig", 0 );
Listview1.items. Add ("puppy", "Puppy", 1 );
Listview1.items. Add ("Jiao", "Jiao", 2 );
}
Public int oriheight
{
Get {return intoriheight ;}
}
Private void button#click (Object sender, eventargs E)
{
Listview1.dock = dockstyle. None;
Button1.dock = dockstyle. Top;
Button2.dock = dockstyle. bottom;
Button3.dock = dockstyle. bottom;
Button3.sendtoback ();
Listview1.bringtofront ();
Listview1.dock = dockstyle. bottom;
Listview1.clear ();
Listview1.items. Add ("pig", "pig", 0 );
Listview1.items. Add ("puppy", "Puppy", 1 );
Listview1.items. Add ("Jiao", "Jiao", 2 );
}
Private void button2_click (Object sender, eventargs E)
{
Listview1.dock = dockstyle. None;
Button2.dock = dockstyle. Top;
Button1.dock = dockstyle. Top;
Button1.sendtoback ();
Button3.dock = dockstyle. bottom;
Listview1.dock = dockstyle. bottom;
Listview1.clear ();
Listview1.items. Add ("North Wind", "North Wind", 3 );
}
Private void button3_click (Object sender, eventargs E)
{
Listview1.dock = dockstyle. None;
Button3.dock = dockstyle. Top; // set the button3 button to bind to the upper edge of the form
Button2.dock = dockstyle. Top; // set the button2 button to bind to the upper edge of the form
Button2.sendtoback (); // ensure that button2 is behind button3
Button1.dock = dockstyle. Top;
Button1.sendtoback (); // ensure that button1 is behind button2
Listview1.dock = dockstyle. bottom;
Listview1.clear ();
Listview1.items. Add ("Ice Rain", "Ice Rain", 5 );
}
Private void judgewinmouposition_tick (Object sender, eventargs E)
{
If (this. Top <3) // when the margin of the form to the screen is smaller than 3px
{
If (this. Handle = mousenowposition (cursor. position. X, cursor. position. y) // when the mouse is on the form
{
Windowflag = 1; // set the current form status
Hidewindow. Enabled = false; // sets the timer hidewindow to unavailable.
This. Top = 0; // set the distance between the edge on the form and the edge on the container work zone
}
Else // when the mouse is not on the form
{
Windowflag = 1; // set the current form status
Hidewindow. Enabled = true; // hidewindow
}
} // When the margin of the form from the screen is greater than 3px
Else
{
// When the form is at the leftmost, rightmost, and rightmost of the screen
If (this. left <3 | (this. left + this. width)> (getsystemmetrics (0)-3) | (this. top + this. height)> (screen. allscreens [0]. bounds. height-3 ))
{
If (this. Left <3) // when the form is on the left of the screen
{
If (this. Handle = mousenowposition (cursor. position. X, cursor. position. y) // when the mouse is on the form
{
This. Height = screen. allscreens [0]. bounds. Height-40;
This. Top = 3;
Windowflag = 2; // set the current form status
Hidewindow. Enabled = false; // sets the timer hidewindow to unavailable.
This. Left = 0; // set the distance between the left edge of the form and the left edge of the container work area.
}
Else // when the mouse is not on the form
{
Windowflag = 2; // set the current form status
Hidewindow. Enabled = true; // sets the timer hidewindow to available.
}
}
If (this. Left + this. width)> (getsystemmetrics (0)-3) // when the form is at the far right of the screen
{
If (this. Handle = mousenowposition (cursor. position. X, cursor. position. y) // when the cursor is on the form
{
This. Height = screen. allscreens [0]. bounds. Height-40;
This. Top = 3;
Windowflag = 3; // set the current form status
Hidewindow. Enabled = false; // sets the timer hidewindow to unavailable.
This. Left = getsystemmetrics (0)-This. width; // you can specify the distance between the form and the left edge of the container workspace.
}
Else // when the mouse leaves the form
{
Windowflag = 3; // set the current form status
Hidewindow. Enabled = true; // sets the timer hidewindow to available.
}
}
// When the distance between the form and the bottom of the screen is less than 3px
If (this. Top + this. Height)> (screen. allscreens [0]. bounds. Height-3 ))
{
If (this. Handle = mousenowposition (cursor. position. X, cursor. position. y) // when the mouse is on the form
{
Windowflag = 4; // set the current form status
Hidewindow. Enabled = false; // sets the timer hidewindow to unavailable.
This. Top = screen. allscreens [0]. bounds. Height-This. height; // you can specify the distance between the form and the edge of the container workspace.
}
Else
{
If (this. Left> This. Width + 3) & (getsystemmetrics (0)-This. Right)> 3)
{
Windowflag = 4; // set the current form status
Hidewindow. Enabled = true; // sets the timer hidewindow to available.
}
}
}
}
}
}
Private void hidewindow_tick (Object sender, eventargs E)
{
Switch (convert. toint32 (windowflag. tostring () // determine the status of the current form
{
Case 1: // when the form is at the top
If (this. Top <3) // when the distance between the form and the upper edge of the container workspace is less than 5px
This. Top =-(this. Height-2); // you can specify the value of the current form at the edge of the container workspace.
Break;
Case 2: // when the form is at the leftmost end
If (this. Left <3) // when the distance between the form and the left edge of the container workspace is less than 5px
This. Left =-(this. Width-2); // set the value of the left edge of the current form according to the container work zone
Break;
Case 3: // when the form is at the rightmost end
If (this. Left + this. width)> (getsystemmetrics (0)-3) // when the distance between the form and the right edge of the container workspace is less than 5px
This. Left = getsystemmetrics (0)-2; // you can specify the value of the current form at the left edge of the container workspace.
Break;
Case 4: // when the form is at the lowest end
If (this. Bottom> screen. allscreens [0]. bounds. Height-3) // when the distance between the form and the bottom edge of the container workspace is less than 5px
This. Top = screen. allscreens [0]. bounds. Height-5; // you can specify the distance between the current form and the edge of the container workspace.
Break;
}
}
# Region: Get the control handle in the current state of the mouse
/// <Summary>
/// Obtain the control handle in the current state of the mouse
/// </Summary>
/// <Param name = "X"> X coordinate of the current mouse </param>
/// <Param name = "Y"> Y coordinate of the current mouse </param>
/// <Returns> </returns>
Public intptr mousenowposition (int x, int y)
{
Intptr originalhandle; // declare the variable that saves the original handle
Originalhandle = (intptr) windowfrompoint (x, y); // obtain the handle of the window containing the original location of the mouse
Currenthandle = originalhandle; // sets the current handle.
While (originalhandle! = (Intptr) 0) // cyclically determines whether the mouse is moving
{
Currenthandle = originalhandle; // record the current handle
Originalhandle = getparent (currenthandle); // update the original handle
}
Return currenthandle; // returns the current handle.
}
# Endregion
// When the form leaves the Left and Right hidden area, the form returns to the original height.
Private void form1_locationchanged (Object sender, eventargs E)
{
If (this. Left> 3 & this. Right <(getsystemmetrics (0)-3 ))
{
If (this. Height = screen. allscreens [0]. bounds. Height-40)
{
This. Height = oriheight;
}
}
}
Private void form1_resize (Object sender, eventargs E)
{
Listview1.height = This. Height-button3.height * 3-30;
}
}
}