最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】C#中获得当前任务栏的大小(宽度和高度)和位置(X,Y) 和 当前屏幕的右下角位置(X,Y) – 均支持任务栏的下右上左四种模式

C# crifan 2630浏览 0评论

【问题】

在折腾:

C#中如何在右下角添加提示窗口,用于显示打开文件和文件夹

的过程中,需要知道任务栏的高度和位置,才能准确将子窗体显示在右下角。

【解决过程】

1.参考:

How do I get the taskbar’s position and size?

去写了代码:

Rectangle taskbarRect = Screen.PrimaryScreen.WorkingArea;

然后调试的结果是:

screen primaryscreen

而对应的屏幕分辨率是1680×1050:

screen resolution 1680x1050

 

对应的当前任务栏是在屏幕下面的:

taskbar bottom

2.又去测试了其他几种情况:

任务栏是在右边的:

taskbar right

然后调试结果是:

debug result while taskbar right

 

任务栏在上面的:

taskbar up

debug result while taskbar up

 

任务栏在左边的:

taskbar left

debug result while taskbar left

 

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);
}

 

另,上述所有函数,都已整理至:

crifan的C#函数库:crifanLib.cs

转载请注明:在路上 » 【已解决】C#中获得当前任务栏的大小(宽度和高度)和位置(X,Y) 和 当前屏幕的右下角位置(X,Y) – 均支持任务栏的下右上左四种模式

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 厉害,学习了,谢谢分享
    0x0000007F6年前 (2014-03-03)回复
59 queries in 0.094 seconds, using 19.02MB memory