c# 悬浮窗口

转自:

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;

    }

 

       下来就是悬浮窗口部分代码,首先要设置其窗体的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();

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值