码迷,mamicode.com
首页 > Windows程序 > 详细

winform 屏幕右上角悬浮窗demo

时间:2017-09-11 19:38:36      阅读:430      评论:0      收藏:0      [点我收藏+]

标签:handle   div   nbsp   设置   data   store   背景   stat   窗口   

当窗体最小化时,显示右悬浮窗!!!

 

1新建右上角小窗口

(1)去除边框 (2)最前显示(设置背景图案)

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 
  9 namespace TopMost
 10 {
 11     public partial class frmTopMost : Form
 12     {
 13         private Point ptMouseCurrentPos, ptMouseNewPos, ptFormPos, ptFormNewPos;
 14         private bool blnMouseDown = false;
 15         private frmMain pParent;
 16         //Add ContextMenuStrip .
 17         private ContextMenuStrip docMenu;
 18 
 19         public frmTopMost(frmMain pMain)
 20         {
 21             InitializeComponent();
 22             pParent=pMain;
 23         }
 24 
 25         private void frmTopMost_Load(object sender, EventArgs e)
 26         {
 27             this.FormBorderStyle = FormBorderStyle.None;
 28             this.TopMost = true;
 29             this.Show();
 30             this.Top = 100;
 31             this.Left = Screen.PrimaryScreen.Bounds.Width - 200;
 32             this.Width = 35;
 33             this.Height = 35;
 34 
 35             docMenu = new ContextMenuStrip();
 36             
 37             ToolStripMenuItem OpenLable = new ToolStripMenuItem();
 38             OpenLable.Text = "Open";
 39             OpenLable.Click+=new EventHandler(OpenLable_Click);
 40             ToolStripMenuItem ExitLable = new ToolStripMenuItem();
 41             ExitLable.Text = "Exit";
 42             ExitLable.Click+=new EventHandler(ExitLable_Click);
 43 
 44             docMenu.Items.AddRange(new ToolStripMenuItem[] { OpenLable,ExitLable});
 45             this.ContextMenuStrip = docMenu;
 46         }
 47         private void frmTopMost_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
 48         {
 49             if (blnMouseDown)
 50             {
 51                 //Get the current postion of the mouse in the screen.
 52                 ptMouseNewPos = Control.MousePosition;
 53 
 54                 //Set window postion.
 55                 ptFormNewPos.X = ptMouseNewPos.X - ptMouseCurrentPos.X + ptFormPos.X;
 56                 ptFormNewPos.Y = ptMouseNewPos.Y - ptMouseCurrentPos.Y + ptFormPos.Y;
 57 
 58                 //Save window postion.
 59                 Location = ptFormNewPos;
 60                 ptFormPos = ptFormNewPos;
 61 
 62                 //Save mouse pontion.
 63                 ptMouseCurrentPos = ptMouseNewPos;
 64             }
 65         }
 66 
 67         private void frmTopMost_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e)
 68         {
 69             if (e.Button == MouseButtons.Left)
 70             {
 71                 blnMouseDown = true;
 72 
 73                 //Save window postion and mouse postion.
 74                 ptMouseCurrentPos = Control.MousePosition;
 75                 ptFormPos = Location;
 76             }
 77         }
 78 
 79         private void frmTopMost_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
 80         {
 81             if (e.Button == MouseButtons.Left)
 82             {
 83                 blnMouseDown = false;
 84             }
 85         }
 86         //Restore parent from.
 87         private void frmTopMost_DoubleClick(object sender, System.EventArgs e)
 88         {
 89             SwitchToMain();
 90         }
 91         private void SwitchToMain()
 92         {
 93             pParent.RestoreWindow();
 94             this.Hide();
 95         }
 96 
 97         #region  //Context events.
 98         private void OpenLable_Click(object sender, System.EventArgs e)
 99         {
100             SwitchToMain();
101         }
102         private void ExitLable_Click(object sender, System.EventArgs e)
103         {
104             Application.Exit();
105         }
106         #endregion
107 
108         private void frmTopMost_MouseEnter(object sender, System.EventArgs e)
109         {
110             this.toolTip1.Active = true;
111             this.toolTip1.AutoPopDelay = 500000;
112             this.toolTip1.InitialDelay = 500;
113             this.toolTip1.ReshowDelay = 100;
114             this.toolTip1.ShowAlways = true;
115         }
116         private void frmTopMost_MouseLeave(object sender, System.EventArgs e)
117         {
118             this.toolTip1.Active = false;
119         }
120 
121     }
122 }
View Code

 

2、主窗体

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 
 9 namespace TopMost
10 {
11     public partial class frmMain : Form
12     {
13         public frmMain()
14         {
15             InitializeComponent();
16         }
17 
18         private FormWindowState fwsPrevious;
19         private frmTopMost myTopMost;
20         private void frmMain_Load(object sender, EventArgs e)
21         {
22             //save the windowState;
23             fwsPrevious = this.WindowState;
24 
25             //Create top most window;
26             myTopMost = new frmTopMost(this);
27         }
28 
29         private void frmMain_SizeChanged(object sender,EventArgs e)
30         {
31             if (this.WindowState == FormWindowState.Minimized)
32             {
33                 //window minimized.
34                 //show the most window.
35                 myTopMost.Show();
36                 this.ShowInTaskbar = false;
37             }
38             else if(this.WindowState!=fwsPrevious)
39             {
40                 fwsPrevious = WindowState;
41             }
42         }
43         //Restore window.
44         public void RestoreWindow()
45         {
46             this.WindowState = fwsPrevious;
47             this.ShowInTaskbar = true;
48         }
49     }
50 }
View Code

 

完!!

 

winform 屏幕右上角悬浮窗demo

标签:handle   div   nbsp   设置   data   store   背景   stat   窗口   

原文地址:http://www.cnblogs.com/wwz-wwz/p/7506300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!