c# winform 悬浮窗的效果

用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;
}

       下来就是悬浮窗口部分代码,首先要设置其窗体的FormBorderStyleNone,然后设置其的TopMosttrue,接下来,就是主要是三个鼠标事件的处理,以下,具体说明如何实现的。

       局部成员声明。

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();
}
以上介绍的就是C# Winform实现一个悬浮窗口程序,希望对你有所帮助。

转载于:https://www.cnblogs.com/oldcell/archive/2013/04/21/3033737.html

发布了149 篇原创文章 · 获赞 93 · 访问量 18万+
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览