转自:
http://blog.csdn.net/Knight94/article/details/663089
用C#做一个像FlashGet的悬浮窗口,其实很简单,不像以前需要调用很多系统API。大致的步骤如下。
首先是主窗体部分,即要判断窗体的状态来决定是否显示悬浮窗口。
局部成员声明:
private FormWindowState fwsPrevious;
private frmTopMost myTopMost;
主窗体的Load事件:
private void frmMain_Load(object sender, System.EventArgs e)
{
// Save window state
fwsPrevious = this.WindowState;
// Create top most window
myTopMost = new frmTopMost(this );
}
主窗体的SizeChanged事件:
private void frmMain_SizeChanged(object sender, System.EventArgs e)
{
if( this.WindowState == FormWindowState.Minimized )
{
// Window minimized
// Show top most window
myTopMost.Show();
this.ShowInTaskbar = false;
}
else if( this.WindowState != fwsPrevious )
{
// Save current window state
fwsPrevious = this.WindowState;
}
}
然后,就是还原窗口方法,即供悬浮窗口进行调用的。
///<summary>
/// Restore window state
///</summary>
public void RestoreWindow()
{
this.WindowState = fwsPrevious;
this.ShowInTaskbar = true;
}
下来就是悬浮窗口部分代码,首先要设置其窗体的FormBorderStyle为None,然后设置其的TopMost为true,接下来,就是主要是三个鼠标事件的处理,以下,具体说明如何实现的。
局部成员声明。
private Point ptMouseCurrrnetPos, ptMouseNewPos,
ptFormPos, ptFormNewPos;
private bool blnMouseDown=false;
private frmMain pParent;
悬浮窗口的构造函数。
public frmTopMost( frmMain pMain )
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// Init main window object
pParent = pMain;
}
悬浮窗口的Load事件。
private void frmTopMost_Load(object sender, System.EventArgs e)
{
this.Show();
// Init window size and position
this.Top = 100;
this.Left = Screen.PrimaryScreen.Bounds.Width - 100;
this.Width = 80;
this.Height = 80;
}
主要的三个鼠标事件,即MouseDown、 MouseMove、 MouseUp三个事件。
private void frmTopMost_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if( blnMouseDown )
{
//Get the current position of the mouse in the screen
ptMouseNewPos = Control.MousePosition;
//Set window position
ptFormNewPos.X = ptMouseNewPos.X - ptMouseCurrrnetPos.X + ptFormPos.X;
ptFormNewPos.Y = ptMouseNewPos.Y - ptMouseCurrrnetPos.Y + ptFormPos.Y;
//Save window position
Location = ptFormNewPos;
ptFormPos = ptFormNewPos;
//Save mouse position
ptMouseCurrrnetPos = ptMouseNewPos;
}
}
private void frmTopMost_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
blnMouseDown = true;
// Save window position and mouse position
ptMouseCurrrnetPos = Control.MousePosition;
ptFormPos = Location;
}
}
private void frmTopMost_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Left)
//Return back signal
blnMouseDown = false;
}
然后就是双击悬浮窗体,进行恢复主窗体。
private void frmTopMost_DoubleClick(object sender, System.EventArgs e)
{
SwitchToMain();
}
private void SwitchToMain()
{
//Change main window status here
pParent.RestoreWindow();
//Hide top most window
this.Hide();
}
此外,还可以给悬浮窗口添加ContextMenu,来辅助操作,这里就不细说了。
private System.Windows.Forms.ContextMenu mnuPopup;
private System.Windows.Forms.MenuItem mnuMainWindow;
private System.Windows.Forms.MenuItem mnuExit;
private void mnuMainWindow_Click(object sender, System.EventArgs e)
{
SwitchToMain();
}
private void mnuExit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}