using System.Runtime.InteropServices;
/*
Winform的 FormBorderStyle=None;
*/
namespace Windows_API_实现屏幕右下角_消息框_
{
public partial class QQHide : Form
{
#region WinAPI http://msdn.microsoft.com/en-us/library/ms632669%28VS.85%29.aspx
/// <summary>
/// Enables you to produce special effects when showing or hiding windows. There are four types of animation: roll, slide, collapse or expand, and alpha-blended fade.
/// </summary>
/// <param name="handle">A handle to the window to animate. The calling thread must own this window.</param>
/// <param name="time">The time it takes to play the animation, in milliseconds. Typically, an animation takes 200 milliseconds to play. </param>
/// <param name="type">The type of animation. This parameter can be one or more of the following values. Note that, by default, these flags take effect when showing a window. To take effect when hiding a window, use AW_HIDE and a logical OR operator with the appropriate flags. </param>
/// <returns>If the function succeeds, the return value is nonzero.</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "AnimateWindow")]
private static extern bool AnimateWindow(IntPtr handle, int time, AnimationType type);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
#endregion
#region 获取全局鼠标位置 http://msdn.microsoft.com/en-us/library/ms648390(VS.85).aspx
/// <summary>
/// Retrieves the position of the mouse cursor, in screen coordinates.
/// </summary>
/// <param name="p">A pointer to a POINT structure that receives the screen coordinates of the cursor.</param>
/// <returns>Returns nonzero if successful or zero otherwise. To get extended error information, call GetLastError.</returns>
[DllImport("user32.dll")]
private static extern bool GetCursorPos(ref Point p);
#endregion
public QQHide()
{
InitializeComponent();
}
private void QQHide_Load(object sender, EventArgs e)
{
MessageBox.Show("模拟窗体附顶停靠隐藏");
}
int Y;
int SimulateY;
private void QQHide_LocationChanged(object sender, EventArgs e)
{
Y = this.Location.Y;
this.lblX.Text = this.Location.X.ToString();
this.lblY.Text = this.Location.Y.ToString();
label1.Text = "X:" + MousePosition.X + "Y:" + MousePosition.Y;
label2.Text = "X:" + (MousePosition.X - this.Location.X) + "Y:" + SimulateY;
}
/// <summary>
/// 窗体原来位置
/// </summary>
Point SourcePoint;
/// <summary>
/// 窗体非激活状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void QQHide_Deactivate(object sender, EventArgs e)
{
if (HideConditionOfOne)
{///满足条件隐藏窗体
AnimateWindow(this.Handle, 150, AnimationType.AW_HIDE | AnimationType.AW_VER_NEGATIVE | AnimationType.AW_SLIDE);
SourcePoint = this.Location;
HideConditionOfOne = false;
}
}
/// <summary>
/// 隐藏条件之一:窗体上边缘贴近屏幕上边缘
/// </summary>
bool HideConditionOfOne;
new bool IsEnter;
private void QQHide_MouseEnter(object sender, EventArgs e)
{
IsEnter = true;
}
/// <summary>
/// 鼠标离开窗体时更改条件变量值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void QQHide_MouseLeave(object sender, EventArgs e)
{
if (IsEnter && Y < 3)
{
Point p = new Point(this.Location.X, 0);
this.PointToScreen(p);
this.Location = p;
///保存窗体位置并满足隐藏条件
HideConditionOfOne = true;
}
IsEnter = false;
}
/// <summary>
/// 鼠标按下时模拟窗体顶部边框拖拉窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void QQHide_MouseDown(object sender, MouseEventArgs e)
{
SimulateY = (MousePosition.Y - this.Location.Y);
///模拟窗体拖拉框
if (SimulateY > 30)
return;
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
/// <summary>
/// 实时更新鼠标位置
/// </summary>
Point MousePoint = new Point();
private void tMonitor_Tick(object sender, EventArgs e)
{
GetCursorPos(ref MousePoint);
///Y方向符合显示窗体
if (MousePoint.Y > 3)
return;
///X方向符合显示窗体
if (MousePoint.X > SourcePoint.X && MousePoint.X < (this.Width + SourcePoint.X))
AnimateWindow(this.Handle, 100, AnimationType.AW_ACTIVATE | AnimationType.AW_SLIDE | AnimationType.AW_VER_POSITIVE);
/*
if (MousePosition.Y > 3)
return;
if (MousePosition.X > SourcePoint.X && MousePosition.X < (this.Width + SourcePoint.X))
AnimateWindow(this.Handle, 100, AnimationType.AW_ACTIVATE | AnimationType.AW_SLIDE | AnimationType.AW_VER_POSITIVE);
*/
}
}
}