Intended audience: entry level, old bird drifting
As we all know, QQ has an automatic dock function, that is, "up, left, right". When you drag the form to the edge of the screen and move the mouse away, it automatically scales, then only a small dot is displayed. We will observe it carefully and find that it only shows the edge of about 3 pixels. When you move the mouse up, it will stretch out again, some common entry-level scholars asked Ai Wei to share with friends who needed it how I implemented it, with little code. The effect is as follows:
First, get several variables in the current class to facilitate logical judgment:
QQ_MODE (used to record the current dock status of the form, that is, 0 is not docked, 1 is the X axis, 2 is the Y axis, and 3 is the top ), QQ_T (the size of the edge displayed when the form is scaled) and QQ_XY (the visible area when the mouse coordinates and the number of pixels of the form edge are used)
Logic Thinking:If the left mouse button is pressed on the current form, no matter where the form is located, the form must be displayed and may be dragged, that is, not docked; if you move the cursor to the window or to the area in the QQ_XY edge, the form is visible. When you move the cursor away from the form, you can determine whether the scaling conditions are met, that is, "Top, left, right", where "TOP" is the priority;
Drag the form to a "timer" control. The key logic judgment code is as follows:
# Region is similar to QQ's shrinking function and logic implementation code
Int QQ_MODE = 0, QQ_T = 3, QQ_XY = 6; // 0 is not docked, 1 is the X axis, 2 is the Y axis, 3 is the top; QQ_T is the display pixel; QQ_XY is an error
Private void timereffectick (object sender, EventArgs e)
{
// If you press the left button, the current logic will not be processed [whether to contract]
If (MouseButtons = MouseButtons. Left)
Return;
// Cursor position
Int x = MousePosition. X, y = MousePosition. Y;
// Move the cursor to the window to display
If (x> (this. Location. X-QQ_XY)
&&
X <(this. Location. X + this. Width + QQ_XY)
&&
Y> (this. Location. Y-QQ_XY)
&&
Y <(this. Location. Y + this. Height + QQ_XY ))
{
If (this. QQ_MODE = 1)
This. Location = new Point (QQ_T, this. Location. Y );
Else if (this. QQ_MODE = 2)
This. Location = new Point (Screen. PrimaryScreen. WorkingArea. Width-this. Width-QQ_T, this. Location. Y );
Else if (this. QQ_MODE = 3)
This. Location = new Point (this. Location. X, QQ_T );
}
Else // move the cursor out of the window to hide
{
If (this. Location. Y <= QQ_T) //
{
This. Location = new Point (this. Location. X, QQ_T-this. Height );
This. QQ_MODE = 3;
}
Else if (this. Location. X <= QQ_T) // left
{
This. Location = new Point (QQ_T-this. Width, this. Location. Y );
This. QQ_MODE = 1;
}
Else if (this. Location. X> = Screen. PrimaryScreen. WorkingArea. Width-this. Width-QQ_T) // right
{
This. Location = new Point (Screen. PrimaryScreen. WorkingArea. Width-QQ_T, this. Location. Y );
This. QQ_MODE = 2;
}
Else
This. QQ_MODE = 0;
}
}
// Solve the QQ logic when moving the form
Private void ToolsMenu_Move (object sender, EventArgs e)
{
This. QQ_MODE = 0;
}
# Endregion
Ai Wei from the author's blog