欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

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

来源:本站原创|时间:2020-01-10|栏目: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#程序设计有所帮助。

上一篇:C#通过Semaphore类控制线程队列的方法

栏    目:C#教程

下一篇:C#编程实现自定义热键的方法

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

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/7025.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有