C# Winform多螢幕多顯示器編程技巧執行個體

來源:互聯網
上載者:User

C# Winform多螢幕多顯示器編程技巧執行個體

   這篇文章主要介紹了C# Winform多螢幕多顯示器編程技巧執行個體,本文直接給出代碼執行個體,需要的朋友可以參考下

  在視窗的中間有一個System.Windows.Forms.PictureBox控制項(該控制項地區的面積為所在視窗的1/4),當該控制項的大部分地區落在其中一台顯示器時,在另一台顯示器將不顯示該控制項,(該PictureBox控制項將移動到主顯示器所在的視窗地區)。

  實現方法:

  ?

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

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace WindowsApplication12

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private int tmpx = 0;

private int tmpy = 0;

private System.Windows.Forms.PictureBox pictureBox1;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

System.Drawing.Rectangle[] ScreensRect;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.pictureBox1 = new System.Windows.Forms.PictureBox();

this.SuspendLayout();

//

// pictureBox1

//

this.pictureBox1.BackColor = System.Drawing.SystemColors.HotTrack;

this.pictureBox1.Location = new System.Drawing.Point(120, 88);

this.pictureBox1.Name = "pictureBox1";

this.pictureBox1.Size = new System.Drawing.Size(248, 176);

this.pictureBox1.TabIndex = 0;

this.pictureBox1.TabStop = false;

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(504, 357);

this.Controls.Add(this.pictureBox1);

this.Name = "Form1";

this.Text = "Form1";

this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);

this.Load += new System.EventHandler(this.Form1_Load);

this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

{

this.tmpx = e.X;

this.tmpy = e.Y;

this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.form1_MouseMove);

}

private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)

{

this.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.form1_MouseMove);

System.Drawing.Rectangle pictureBox1Rect=Screen.GetWorkingArea(pictureBox1);

for(int i=0;i<ScreensRect.Length;i++)

{

if(ScreensRect[i].X==pictureBox1Rect.X && ScreensRect[i].Y==pictureBox1Rect.Y)

this.Location=new Point(ScreensRect[i].X,pictureBox1Rect.Y);

}

//MessageBox.Show(" WorkingArea:" + re.ToString());

}

private void form1_MouseMove(object sender, MouseEventArgs e)

{

this.Location = new System.Drawing.Point(this.Location.X + e.X - this.tmpx, this.Location.Y + e.Y - this.tmpy);

}

private void Form1_Load(object sender, System.EventArgs e)

{

Screen[] s=Screen.AllScreens;

ScreensRect=new Rectangle[s.Length];

for(int i=0;i<s.Length;i++)

{

ScreensRect[i]= s[i].WorkingArea;

}

}

}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.