Two days ago, C # was used in the work to implement a winform hanging window;
I searched several times on the Internet before and found no similar descriptions. I 'd like to record them here.
Note: the so-called hanging window is a function similar to the QQ terminal tool. When the window is placed on the upper boundary of the desktop, it is automatically shrunk into a small bar to hide it. Once the mouse is again exposed to the above boundary, the window is automatically expanded.
There are two implementation methods: one is to implement the suspension in the window. A small panel is created in the window, which is invisible normally and displayed in the suspension,
When hanging, set the window title bar not to display and the window height to the Panel height. Second, separate window suspension. Create a new window as the display bar when hanging, and hide the main window.
I. Implement hanging in the window
Determine whether to reach the desktop boundary in the locationchanged event, and determine the current mouse position through the timer
(System. Windows. Forms. Control. mouseposition) whether to leave the window area or enter the window area.
Implementation code.
2. Separate window Suspension
I didn't want to consider the second method, but later I found that the height of the window to be suspended is invalid. It is a window class inherited from devcomponents. dotnetbar. office2007ribbonform.
The implementation idea is basically the same, but it should be noted that the current mouse location needs to be determined by different window conversion coordinates. For other details, seeCode.
Define variables:
Bool m_bxuangua = false; // whether the form is suspended
Hideflagform m_hideflagform = new hideflagform (); // The Hanging flag window
Main Methods:
/// <Summary>
/// RESPONSE event when the window position is dragged
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void formmain_locationchanged (Object sender, eventargs E)
{
If (location. Y <0)
{
Top = 0;
M_bxuangua = true;
}
Else
{
M_bxuangua = false;
Plhideform. Visible = false;
}
Sethideflagformpos ();
}
/// <Summary>
/// Set the position and size of the Flag form
/// </Summary>
Private void sethideflagformpos ()
{
M_hideflagform.left = This. Left;
M_hideflagform.top = This. Top;
M_hideflagform.width = This. width;
M_hideflagform.height = 10;
}
///
// suspension or expand the form
///
private void showhideform ()
{< br> point Pt = new point (system. windows. forms. control. mouseposition. x,
system. windows. forms. control. mouseposition. y);
point pt2 = new point ();
If (this. Visible)
Pt2 = pointtoclient (PT );
Else
Pt2 = m_hideflagform.pointtoclient (PT );
Int ihight = 0;
If (this. Visible)
Ihight = height;
Else
Ihight = m_hideflagform.height;
If (pt2.x <0 | pt2.x> width | pt2.y> ihight) // when outside the form customer region
{
If (m_bxuangua & formborderstyle! = Formborderstyle. None)
{
Formborderstyle = formborderstyle. None;
Sethideflagformpos ();
This. Hide ();
M_hideflagform.show ();
Sethideflagformpos ();
}
}
Else // within the form customer Region
{
If (m_bxuangua & formborderstyle! = Formborderstyle. sizable)
{
Formborderstyle = formborderstyle. sizable;
M_hideflagform.hide ();
This. Show ();
}
}
}
///
// timer
///
///
//
private void tmcursorposcalc_tick (Object sender, eventargs e)
{< br> If (system. windows. forms. control. mousebuttons = mousebuttons. left)
return; // when the left mouse button is pressed, the window is considered to be in the drag state, and the judgment of the mouse position is paused
showhideform ();
}
/// <Summary>
/// Window size change event
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void formmain_resize (Object sender, eventargs E)
{
M_hideflagform.left = left;
M_hideflagform.width = width;
}