码迷,mamicode.com
首页 > Windows程序 > 详细

如何用C#做一个悬浮窗口程序

时间:2017-06-18 21:56:39      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:position   pup   添加   epo   public   current   else   sep   set   

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#做一个悬浮窗口程序

标签:position   pup   添加   epo   public   current   else   sep   set   

原文地址:http://www.cnblogs.com/lgx5/p/7045311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!