C#中获得当前任务栏的大小

2021-02-18 23:18

阅读:341

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

}


评论


亲,登录后才可以留言!