【问题】
在折腾:
的过程中,需要知道任务栏的高度和位置,才能准确将子窗体显示在右下角。
【解决过程】
1.参考:
How do I get the taskbar’s position and size?
去写了代码:
Rectangle taskbarRect = Screen.PrimaryScreen.WorkingArea;
然后调试的结果是:
而对应的屏幕分辨率是1680×1050:
对应的当前任务栏是在屏幕下面的:
2.又去测试了其他几种情况:
任务栏是在右边的:
然后调试结果是:
任务栏在上面的:
任务栏在左边的:
3.由此,继续去调试,期间遇到想要设置窗口位置的问题,后来解决了,详参:
【已解决】C#中通过代码改变/设置窗口的位置(Location)
4.最终整理出算法,始终希望窗口位置显示在屏幕边角,贴近任务栏中是显示时间的那个位置:
// 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.然后,又花时间,整理出来函数,可以获得当前屏幕的任务栏的大小:
// 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#中,获得当前任务栏的大小,以及获得当前屏幕的边角(靠近任务栏中时间的那个)位置的相关代码如下:
// 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】
后来,又添加了,获得当前任务栏的位置的函数。
全部的代码如下:
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);
}函数使用举例:
private void completeHint_Load(object sender, EventArgs e)
{
Size curTaskbarSize = crl.getCurTaskbarSize();
Point curTaskbarLocation = crl.getCurTaskbarLocation();
this.Location = crl.getCornerLocation(this.Size);
}
另,上述所有函数,都已整理至:
转载请注明:在路上 » 【已解决】C#中获得当前任务栏的大小(宽度和高度)和位置(X,Y) 和 当前屏幕的右下角位置(X,Y) – 均支持任务栏的下右上左四种模式








