這篇文章主要介紹了C#實(shí)現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復(fù)原窗體的方法,涉及C#窗體及鼠標(biāo)事件響應(yīng)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#實(shí)現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復(fù)原窗體的方法。分享給大家供大家參考。具體如下:
這里演示C#實(shí)現(xiàn)主窗體最小化后出現(xiàn)懸浮框,雙擊懸浮框恢復(fù)原窗體的效果。類似于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>
- /// 窗體初始狀態(tài)
- /// </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>
- /// 還原窗口方法,即供懸浮窗口進(jìn)行調(diào)用的。
- /// </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>
- /// 首先要設(shè)置其窗體的FormBorderStyle為None,然后設(shè)置其的TopMost為true,接下來,就是主要是三個(gè)鼠標(biāo)事件的處理
- /// </summary>
- public partial class frmTopMost : Form
- {
- public frmTopMost()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 懸浮窗口的構(gòu)造函數(shù)
- /// </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>
- /// 雙擊懸浮窗體,進(jìn)行恢復(fù)主窗體。
- /// </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();
- }
- }
- }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注