在不少软件中,都会对窗体的大小、位置和移动进行限定。在不一样分辨率的显示器中如何正确显示窗体,如何设置窗体始终在最上面...html
038 设置窗体在屏幕中的位置web
设置窗体在屏幕中的位置,能够经过设置窗体的属性来实现。窗体的 Left 属性表示窗体距屏幕左侧的距离,Top 属性表示窗体距屏幕上方的距离。数组
建立一个项目,默认窗体为 Form1,为 Form1 添加 Label 控件,添加 TextBox 控件用来输入距屏幕的距离,添加 Button 控件用来设置窗体在屏幕上的位置。ide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
namespace
_038_SetLocation
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender, EventArgs e)
{
this
.Left = Convert.ToInt32(textBox1.Text);
//设置窗体左边缘与屏幕左边缘之间的距离
this
.Top = Convert.ToInt32(textBox2.Text);
//设置窗体上边缘与屏幕上边缘之间的距离
}
}
}
|
039 始终在最上面的窗体函数
实现窗体始终在最上面只须要将窗体的 TopMost 属性设为 True 便可。post
建立一个项目,默认窗体为 Form1,为 Form1 窗体添加背景图片,并设置窗体 TopMost 属性为 True。动画
040 从桌面右下角显示的窗体this
主要用到动画显示窗体的 API 函数 AnimateWindow,在该函数的重载方法中,依据参数值的不一样窗体会以不一样的形式显示和隐藏。url
1.建立一个项目,默认窗体为 Form1,添加一个 Windows 窗体,将其命名为 MainForm。spa
2.在 Form1 窗体上添加一个 GroupBox 控件和两个 Button 控件。在 MainForm 窗体中添加一个 PictureBox 控件和一个 ImageList 控件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//Form1 窗体代码
namespace
_040_SoutheastDisplayWindow
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void
display_Click(
object
sender, EventArgs e)
{
MainForm.Instance().ShowForm();
//显示窗体
}
private
void
close_Click(
object
sender, EventArgs e)
{
MainForm.Instance().CloseForm();
//隐藏窗体
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
//MainForm 窗体代码
namespace
_040_SoutheastDisplayWindow
{
public
partial
class
MainForm : System.Windows.Forms.Form
{
#region 声明的变量
private
System.Drawing.Rectangle Rect;
//定义一个存储矩形框的数组
private
FormState InfoStyle = FormState.Hide;
//定义变量为隐藏
static
private
MainForm dropDownForm =
new
MainForm();
//实例化当前窗体
private
static
int
AW_HIDE = 0x00010000;
//该变量表示动画隐藏窗体
private
static
int
AW_SLIDE = 0x00040000;
//该变量表示出现滑行效果的窗体
private
static
int
AW_VER_NEGATIVE = 0x00000008;
//该变量表示从下向上开屏
private
static
int
AW_VER_POSITIVE = 0x00000004;
//该变量表示从上向下开屏
#endregion
#region 该窗体的构造方法
public
MainForm()
{
InitializeComponent();
//初始化工做区大小
System.Drawing.Rectangle rect = System.Windows.Forms.Screen.GetWorkingArea(
this
);
//实例化一个当前窗口的对象
this
.Rect =
new
System.Drawing.Rectangle(rect.Right -
this
.Width - 1, rect.Bottom -
this
.Height - 1,
this
.Width,
this
.Height);
//为实例化的对象建立工做区域
}
#endregion
#region 调用API函数显示窗体
[DllImportAttribute(
"user32.dll"
)]
private
static
extern
bool
AnimateWindow(IntPtr hwnd,
int
dwTime,
int
dwFlags);
#endregion
#region 鼠标控制图片的变化
private
void
pictureBox1_MouseEnter(
object
sender, EventArgs e)
{
pictureBox1.Image = imageList1.Images[1];
//设定当鼠标进入PictureBox控件时PictureBox控件的图片
}
private
void
pictureBox1_MouseLeave(
object
sender, EventArgs e)
{
pictureBox1.Image = imageList1.Images[0];
//设定当鼠标离开PictureBox控件时PictureBox控件的图片
}
#endregion
#region 定义标识窗体移动状态的枚举值
protected
enum
FormState
{
//隐藏窗体
Hide = 0,
//显示窗体
Display = 1,
//显示窗体中
Displaying = 2,
//隐藏窗体中
Hiding = 3
}
#endregion
#region 用属性标识当前状态
protected
FormState FormNowState
{
get
{
return
this
.InfoStyle; }
//返回窗体的当前状态
set
{
this
.InfoStyle = value; }
//设定窗体当前状态的值
}
#endregion
#region 显示窗体
public
void
ShowForm()
{
switch
(
this
.FormNowState)
//判断窗体当前处于何种状态
{
case
FormState.Hide:
//当窗体处于隐藏状态时
if
(
this
.Height <=
this
.Rect.Height - 192)
//当窗体没有彻底显示时
this
.SetBounds(Rect.X,
this
.Top - 192, Rect.Width,
this
.Height + 192);
//使窗体不断上移
else
{
this
.SetBounds(Rect.X, Rect.Y, Rect.Width, Rect.Height);
//设置当前窗体的边界
}
AnimateWindow(
this
.Handle, 800, AW_SLIDE + AW_VER_NEGATIVE);
//动态显示本窗体
break
;
}
}
#endregion
#region 关闭窗体
public
void
CloseForm()
{
AnimateWindow(
this
.Handle, 800, AW_SLIDE + AW_VER_POSITIVE + AW_HIDE);
//动画隐藏窗体
this
.FormNowState = FormState.Hide;
//设定当前窗体的状态为隐藏
}
#endregion
#region 返回当前窗体的实例化对象
static
public
MainForm Instance()
{
return
dropDownForm;
//返回当前窗体的实例化对象
}
#endregion
}
}
|