zoukankan      html  css  js  c++  java
  • 如何用C#做一个悬浮窗口程序

    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();

        }

  • 相关阅读:
    Steps to Writing Well----Reading Notes
    How to Improve Reading Skills
    Requirement-Driven Linux Shell Programming
    Linux tar command usage
    MVC和MVVM模型
    js中特殊的宏任务
    js 超浓缩 双向绑定
    JavaScript 中的遍历详解
    多段动画整合为一个动画的思路
    Js事件循环(Event Loop)机制
  • 原文地址:https://www.cnblogs.com/lgx5/p/7045311.html
Copyright © 2011-2022 走看看