首页 > 代码库 > WinForm窗体屏幕边缘停靠
WinForm窗体屏幕边缘停靠
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
namespace
WZDH
{
public
partial
class
Form1 : Form
{
internal
AnchorStyles StopAanhor = AnchorStyles.None;
public
Form1()
{
InitializeComponent();
}
/// <summary>
/// 初始化窗体信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
Form1_Load(
object
sender, EventArgs e)
{
//设置窗体左边距离主显示器位置
this
.Width = 200;
this
.Left = Screen.PrimaryScreen.Bounds.Width -
this
.Width;
//设置窗体顶边距离主显示器位置
this
.Top = 0;
//设置窗体宽度为主显示器宽度(不包括任务栏)
this
.Height = Screen.PrimaryScreen.WorkingArea.Height;
}
/// <summary>
/// 淡入加载窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
timer1_Tick(
object
sender, EventArgs e)
{
this
.Opacity += 0.1;
if
(
this
.Opacity >= 1)
this
.timer1.Enabled =
false
;
}
/// <summary>
/// 退出程序菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
tsmiExit_Click(
object
sender, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// 当窗体的位置改变时发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
Form1_LocationChanged(
object
sender, EventArgs e)
{
mStopAnhor();
}
private
void
timer2_Tick(
object
sender, EventArgs e)
{
//如果鼠标在窗体上,则根据停靠位置显示整个窗体
if
(
this
.Bounds.Contains(Cursor.Position))
{
switch
(
this
.StopAanhor)
{
case
AnchorStyles.Top:
this
.Location =
new
Point(
this
.Location.X, 0);
break
;
case
AnchorStyles.Bottom:
this
.Location =
new
Point(
this
.Location.X, Screen.PrimaryScreen.Bounds.Height -
this
.Height);
break
;
case
AnchorStyles.Left:
this
.Location =
new
Point(0,
this
.Location.Y);
break
;
case
AnchorStyles.Right:
this
.Location =
new
Point(Screen.PrimaryScreen.Bounds.Width -
this
.Width,
this
.Location.Y);
break
;
}
}
else
//如果鼠标离开窗体,则根据停靠位置隐藏窗体,但须留出部分窗体边缘以便鼠标选中窗体
{
switch
(
this
.StopAanhor)
{
case
AnchorStyles.Top:
this
.Location =
new
Point(
this
.Location.X, (
this
.Height - 3) * (-1));
break
;
case
AnchorStyles.Bottom:
this
.Location =
new
Point(
this
.Location.X, Screen.PrimaryScreen.Bounds.Height - 5);
break
;
case
AnchorStyles.Left:
this
.Location =
new
Point((-1) * (
this
.Width - 3),
this
.Location.Y);
break
;
case
AnchorStyles.Right:
this
.Location =
new
Point(Screen.PrimaryScreen.Bounds.Width - 2,
this
.Location.Y);
break
;
}
}
}
private
void
mStopAnhor()
{
if
(
this
.Top <= 0)
{
StopAanhor = AnchorStyles.Top;
}
else
if
(
this
.Left <= 0)
{
StopAanhor = AnchorStyles.Left;
}
else
if
(
this
.Left >= Screen.PrimaryScreen.Bounds.Width -
this
.Width)
{
StopAanhor = AnchorStyles.Right;
}
else
{
StopAanhor = AnchorStyles.None;
}
}
}
}
---转载:http://www.cnblogs.com/qinyun118/archive/2010/08/30/1812863.html
WinForm窗体屏幕边缘停靠
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。