码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2018-01-22 11:02:23      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:过程   etc   技术分享   结果   c#   private   cin   大小   tps   

【已解决】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);

}

 

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

crifanC#函数库:crifanLib.cs

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

来自 <https://www.crifan.com/csharp_get_current_taskbar_location_and_height/>

 

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

标签:过程   etc   技术分享   结果   c#   private   cin   大小   tps   

原文地址:https://www.cnblogs.com/pugongying123/p/8327551.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!