欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现主窗体最小化后出现悬浮框及双击悬浮框恢复原窗体的方法

程序员文章站 2023-11-22 14:12:58
本文实例讲述了c#实现主窗体最小化后出现悬浮框及双击悬浮框恢复原窗体的方法。分享给大家供大家参考。具体如下: 这里演示c#实现主窗体最小化后出现悬浮框,双击悬浮框恢复原窗...

本文实例讲述了c#实现主窗体最小化后出现悬浮框及双击悬浮框恢复原窗体的方法。分享给大家供大家参考。具体如下:

这里演示c#实现主窗体最小化后出现悬浮框,双击悬浮框恢复原窗体的效果。类似于360桌面。

主窗体:frmmain

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
using system.threading;
namespace appdemo
{
  public partial class frmmain : form
  {
    public frmmain()
    {
      initializecomponent();
    }
    /// <summary>
    /// 窗体初始状态
    /// </summary>
    private formwindowstate fwsprevious;
    /// <summary>
    /// 悬浮窗体
    /// </summary>
    private frmtopmost mytopmost;
    /// <summary>
    /// 主窗体的load事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void frmmain_load(object sender, eventargs e)
    {
      fwsprevious = this.windowstate;
      mytopmost = new frmtopmost(this);
    }
    /// <summary>
    /// 主窗体的sizechanged事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void frmmain_sizechanged(object sender, eventargs e)
    {
      if (this.windowstate == formwindowstate.minimized)
      {
        mytopmost.show();
        this.showintaskbar = false;
      }
      else if (this.windowstate != fwsprevious)
      {
        fwsprevious = this.windowstate;
      }
    }
    /// <summary>
    /// 还原窗口方法,即供悬浮窗口进行调用的。
    /// </summary>
    public void restorewindow()
    {
      this.windowstate = fwsprevious;
      this.showintaskbar = true;
    }
  }
}

悬浮子窗体:frmtopmost

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
namespace appdemo
{
  /// <summary>
  /// 首先要设置其窗体的formborderstyle为none,然后设置其的topmost为true,接下来,就是主要是三个鼠标事件的处理
  /// </summary>
  public partial class frmtopmost : form
  {
    public frmtopmost()
    {
      initializecomponent();
    }
    /// <summary>
    /// 悬浮窗口的构造函数
    /// </summary>
    /// <param name="main"></param>
    public frmtopmost(frmmain main)
    {
      initializecomponent();
      pparent = main;
    }
    private point ptmousecurrrnetpos, ptmousenewpos, ptformpos, ptformnewpos;
    private bool blnmousedown = false;
    private frmmain pparent;
    /// <summary>
    /// 悬浮窗口的load事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void frmtopmost_load(object sender, eventargs e)
    {
      this.show();
      this.top = 100;
      this.left = screen.primaryscreen.bounds.width - 100;
      this.width = 80;
      this.height = 80;
    }
    private void frmtopmost_mousemove(object sender, mouseeventargs e)
    {
      if (blnmousedown)
      {
        ptmousenewpos = control.mouseposition;
        ptformnewpos.x = ptmousenewpos.x - ptmousecurrrnetpos.x + ptformpos.x;
        ptformnewpos.y = ptmousenewpos.y - ptmousecurrrnetpos.y + ptformpos.y;
        location = ptformnewpos;
        ptformpos = ptformnewpos;
        ptmousecurrrnetpos = ptmousenewpos;
      }
    }
    private void frmtopmost_mousedown(object sender, mouseeventargs e)
    {
      if (e.button == mousebuttons.left)
      {
        blnmousedown = true;
        ptmousecurrrnetpos = control.mouseposition;
        ptformpos = location;
      }
    }
    private void frmtopmost_mouseup(object sender, mouseeventargs e)
    {
      if (e.button == mousebuttons.left)
        blnmousedown = false;
    }
    /// <summary>
    /// 双击悬浮窗体,进行恢复主窗体。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void frmtopmost_mousedoubleclick(object sender, mouseeventargs e)
    {
      switchtomain();
    }
    private void switchtomain()
    {
      pparent.restorewindow();
      this.hide();
    }
  }
}

希望本文所述对大家的c#程序设计有所帮助。