程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成主窗體最小化後湧現懸浮框及雙擊懸浮框恢回復復興窗體的辦法

C#完成主窗體最小化後湧現懸浮框及雙擊懸浮框恢回復復興窗體的辦法

編輯:C#入門知識

C#完成主窗體最小化後湧現懸浮框及雙擊懸浮框恢回復復興窗體的辦法。本站提示廣大學習愛好者:(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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved