【已解决】C#中获恰当前任务栏的巨细(宽度和高度)和位置(X,Y) 和 当前屏幕的右下角位置(X,Y) – 均撑持任务栏的下右上左四种模式
在折腾:
C#中如安在右下角添加提示窗口,用于显示打开文件和文件夹
的过程中,需要知道任务栏的高度和位置,才华准确将子窗体显示在右下角。
【解决过程】
1.参考:
How do I get the taskbar’s position and size?
去写了代码:
?
1
Rectangle taskbarRect = Screen.PrimaryScreen.WorkingArea;
然后调试的功效是:
而对应的屏幕辨别率是1680×1050:
对应确当前任务栏是在屏幕下面的:
2.又去测试了其他几种情况:
任务栏是在右边的:
然后调试功效是:
任务栏在上面的:
任务栏在左边的:
3.由此,继续去调试,期间遇到想要设置窗口位置的问题,后来解决了,详参:
【已解决】C#中通过代码转变/设置窗口的位置(Location)
4.最终整理出算法,始终但愿窗口位置显示在屏幕边角,贴近任务栏中是显示时间的阿谁位置:
?
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
// get current right bottom corner position(X, Y), support four mode: taskbar bottom/right/up/left
public Point getCornerLocation()
{
int xPos = 0, yPos = 0;
if( (Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y == 0))
{
//taskbar bottom
xPos = Screen.PrimaryScreen.WorkingArea.Width - this.Size.Width;
yPos = Screen.PrimaryScreen.WorkingArea.Height - this.Size.Height;
}
else if((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X == 0) )
{
//taskbar right
xPos = Screen.PrimaryScreen.WorkingArea.Width - this.Size.Width;
yPos = Screen.PrimaryScreen.WorkingArea.Height - this.Size.Height;
}
else if((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y > 0) )
{
//taskbar up
xPos = Screen.PrimaryScreen.WorkingArea.Width - this.Size.Width;
yPos = Screen.PrimaryScreen.WorkingArea.Y;
}
else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X > 0))
{
//taskbar left
xPos = Screen.PrimaryScreen.WorkingArea.X;
yPos = Screen.PrimaryScreen.WorkingArea.Height - this.Size.Height;
}
return new Point(xPos, yPos);
}
private void completeHint_Load(object sender, EventArgs e)
{
this.Location = getCornerLocation();
}
5.然后,又花时间,整理出来函数,,可以获恰当前屏幕的任务栏的巨细:
?
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
// get current taskbar size(width, height), support four mode: taskbar bottom/right/up/left
public Size getCurTaskbarSize()
{
int width = 0, height = 0;
if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y == 0))
{
//taskbar bottom
width = Screen.PrimaryScreen.WorkingArea.Width;
height = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
}
else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X == 0))
{
//taskbar right
width = Screen.PrimaryScreen.Bounds.Width - Screen.PrimaryScreen.WorkingArea.Width;
height = Screen.PrimaryScreen.WorkingArea.Height;
}
else if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y > 0))
{
//taskbar up
width = Screen.PrimaryScreen.WorkingArea.Width;
//height = Screen.PrimaryScreen.WorkingArea.Y;
height = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
}
else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X > 0))
{
//taskbar left
width = Screen.PrimaryScreen.Bounds.Width - Screen.PrimaryScreen.WorkingArea.Width;
height = Screen.PrimaryScreen.WorkingArea.Height;
}
return new Size(width, height);
}
private void completeHint_Load(object sender, EventArgs e)
{
Size curTaskbarSize = getCurTaskbarSize();
}
【总结】
凡事,还是要靠本身,靠别人还是不靠谱。
C#中,获恰当前任务栏的巨细,以及获恰当前屏幕的边角(靠近任务栏中时间的阿谁)位置的相关代码如下:
?
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
// get current taskbar size(width, height), support four mode: taskbar bottom/right/up/left
public Size getCurTaskbarSize()
{
int width = 0, height = 0;
if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y == 0))
{
//taskbar bottom
width = Screen.PrimaryScreen.WorkingArea.Width;
height = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
}
else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X == 0))
{
//taskbar right
width = Screen.PrimaryScreen.Bounds.Width - Screen.PrimaryScreen.WorkingArea.Width;
height = Screen.PrimaryScreen.WorkingArea.Height;
}
else if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y > 0))
{
//taskbar up
width = Screen.PrimaryScreen.WorkingArea.Width;
//height = Screen.PrimaryScreen.WorkingArea.Y;
height = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
}
else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X > 0))
{
//taskbar left
width = Screen.PrimaryScreen.Bounds.Width - Screen.PrimaryScreen.WorkingArea.Width;
height = Screen.PrimaryScreen.WorkingArea.Height;
}
return new Size(width, height);
}
// get current right bottom corner position(X, Y), support four mode: taskbar bottom/right/up/left
public Point getCornerLocation()
{
int xPos = 0, yPos = 0;
if( (Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y == 0))
{
//taskbar bottom
xPos = Screen.PrimaryScreen.WorkingArea.Width - this.Size.Width;
yPos = Screen.PrimaryScreen.WorkingArea.Height - this.Size.Height;
}
else if((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X == 0) )
{
//taskbar right
xPos = Screen.PrimaryScreen.WorkingArea.Width - this.Size.Width;
yPos = Screen.PrimaryScreen.WorkingArea.Height - this.Size.Height;
}
else if((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y > 0) )
{
//taskbar up
xPos = Screen.PrimaryScreen.WorkingArea.Width - this.Size.Width;
yPos = Screen.PrimaryScreen.WorkingArea.Y;
}
else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X > 0))
{
//taskbar left
xPos = Screen.PrimaryScreen.WorkingArea.X;
yPos = Screen.PrimaryScreen.WorkingArea.Height - this.Size.Height;
}
return new Point(xPos, yPos);
}
private void completeHint_Load(object sender, EventArgs e)
{
Size curTaskbarSize = getCurTaskbarSize();
this.Location = getCornerLocation();
}
【跋文 2012-09-25】
后来,又添加了,获恰当前任务栏的位置的函数。
全部的代码如下:
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System.Drawing;
using System.Windows.Forms;
/*********************************************************************/
/* Screen */
/*********************************************************************/
// get current taskbar size(width, height), support 4 mode: taskbar bottom/right/up/left
public Size getCurTaskbarSize()
{
int width = 0, height = 0;
if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y == 0))
{
//taskbar bottom
width = Screen.PrimaryScreen.WorkingArea.Width;
height = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
}
else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X == 0))
{
//taskbar right
width = Screen.PrimaryScreen.Bounds.Width - Screen.PrimaryScreen.WorkingArea.Width;
height = Screen.PrimaryScreen.WorkingArea.Height;
}
else if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y > 0))
{
//taskbar up
width = Screen.PrimaryScreen.WorkingArea.Width;
//height = Screen.PrimaryScreen.WorkingArea.Y;
height = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
}
else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X > 0))
{
//taskbar left
width = Screen.PrimaryScreen.Bounds.Width - Screen.PrimaryScreen.WorkingArea.Width;
height = Screen.PrimaryScreen.WorkingArea.Height;
}
return new Size(width, height);
}
// get current taskbar position(X, Y), support 4 mode: taskbar bottom/right/up/left
public Point getCurTaskbarLocation()
{
int xPos = 0, yPos = 0;
if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y == 0))
{
//taskbar bottom
xPos = 0;
yPos = Screen.PrimaryScreen.WorkingArea.Height;
}
else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X == 0))
{
//taskbar right
xPos = Screen.PrimaryScreen.WorkingArea.Width;
yPos = 0;
}
else if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y > 0))
{
//taskbar up
xPos = 0;
yPos = 0;
}
else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X > 0))
{
//taskbar left
xPos = 0;
yPos = 0;
}
return new Point(xPos, yPos);
}
// get current right bottom corner position(X, Y), support 4 mode: taskbar bottom/right/up/left
public Point getCornerLocation(Size windowSize)
{
int xPos = 0, yPos = 0;
if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y == 0))
{
//taskbar bottom
xPos = Screen.PrimaryScreen.WorkingArea.Width - windowSize.Width;
yPos = Screen.PrimaryScreen.WorkingArea.Height - windowSize.Height;
}
else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X == 0))
{
//taskbar right
xPos = Screen.PrimaryScreen.WorkingArea.Width - windowSize.Width;
yPos = Screen.PrimaryScreen.WorkingArea.Height - windowSize.Height;
}
else if ((Screen.PrimaryScreen.Bounds.Width == Screen.PrimaryScreen.WorkingArea.Width) &&
(Screen.PrimaryScreen.WorkingArea.Y > 0))
{
//taskbar up
xPos = Screen.PrimaryScreen.WorkingArea.Width - windowSize.Width;
yPos = Screen.PrimaryScreen.WorkingArea.Y;
}
else if ((Screen.PrimaryScreen.Bounds.Height == Screen.PrimaryScreen.WorkingArea.Height) &&
(Screen.PrimaryScreen.WorkingArea.X > 0))
{
//taskbar left
xPos = Screen.PrimaryScreen.WorkingArea.X;
yPos = Screen.PrimaryScreen.WorkingArea.Height - windowSize.Height;
}
return new Point(xPos, yPos);
}
函数使用举例:
?
1
2
3
4
5
6
private void completeHint_Load(object sender, EventArgs e)
{
Size curTaskbarSize = crl.getCurTaskbarSize();
Point curTaskbarLocation = crl.getCurTaskbarLocation();
this.Location = crl.getCornerLocation(this.Size);
}
另,上述所有函数,都已整理至:
crifan的C#函数库:crifanLib.cs