您的位置:首页 > 编程语言 > C#

不传任何参数得到窗体对象实例(C#)

2008-02-17 10:55 453 查看
1using System;

2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7using System.Runtime.InteropServices;
8
9namespace Demo
10{
11 publicclass NativeWin32
12 {
13 public NativeWin32()
14 {
15
16 }
17
18 publicdelegatebool CallBackFunction(IntPtr hwnd, IntPtr lParam);
19
20 [DllImport("user32.dll", CharSet=CharSet.Auto)]
21 publicstaticextern IntPtr FindWindow(string lpClassName,
22 string lpWindowName);
23
24 [DllImport("user32.dll", CharSet=CharSet.Auto)]
25 publicstaticexternbool EnumThreadWindows(int threadId, CallBackFunction funPtr, IntPtr lParam);
26 }
27
28 publicclass Form1 : System.Windows.Forms.Form
29 {
30 private System.Windows.Forms.Button btnMethod1;
31 private System.Windows.Forms.Button btnMethod2;
32 private System.Windows.Forms.Button btnMethod3;
33 private System.Windows.Forms.Button btnMethod4;
34 private System.Windows.Forms.Button btnMethod5;
35
36 private System.ComponentModel.Container components = null;
37
38 public Form1()
39 {
40 InitializeComponent();
41 }
42
43 protectedoverridevoid Dispose( bool disposing )
44 {
45 if( disposing )
46 {
47 if (components != null)
48 {
49 components.Dispose();
50 }
51 }
52 base.Dispose( disposing );
53 }
54
55 privatevoid InitializeComponent()
56 {
57 this.btnMethod1 = new System.Windows.Forms.Button();
58 this.btnMethod2 = new System.Windows.Forms.Button();
59 this.btnMethod3 = new System.Windows.Forms.Button();
60 this.btnMethod4 = new System.Windows.Forms.Button();
61 this.btnMethod5 = new System.Windows.Forms.Button();
62 this.SuspendLayout();
63 //
64 // btnMethod1
65 //
66 this.btnMethod1.Location = new System.Drawing.Point(56, 24);
67 this.btnMethod1.Name = "btnMethod1";
68 this.btnMethod1.Size = new System.Drawing.Size(72, 32);
69 this.btnMethod1.TabIndex = 0;
70 this.btnMethod1.Text = "Method1";
71 this.btnMethod1.Click += new System.EventHandler(this.btnMethod1_Click);
72 //
73 // btnMethod2
74 //
75 this.btnMethod2.Location = new System.Drawing.Point(176, 24);
76 this.btnMethod2.Name = "btnMethod2";
77 this.btnMethod2.Size = new System.Drawing.Size(72, 32);
78 this.btnMethod2.TabIndex = 1;
79 this.btnMethod2.Text = "Method2";
80 this.btnMethod2.Click += new System.EventHandler(this.btnMethod2_Click);
81 //
82 // btnMethod3
83 //
84 this.btnMethod3.Location = new System.Drawing.Point(56, 80);
85 this.btnMethod3.Name = "btnMethod3";
86 this.btnMethod3.Size = new System.Drawing.Size(72, 32);
87 this.btnMethod3.TabIndex = 2;
88 this.btnMethod3.Text = "Method3";
89 this.btnMethod3.Click += new System.EventHandler(this.btnMethod3_Click);
90 //
91 // btnMethod4
92 //
93 this.btnMethod4.Location = new System.Drawing.Point(176, 80);
94 this.btnMethod4.Name = "btnMethod4";
95 this.btnMethod4.Size = new System.Drawing.Size(72, 32);
96 this.btnMethod4.TabIndex = 3;
97 this.btnMethod4.Text = "Method4";
98 this.btnMethod4.Click += new System.EventHandler(this.btnMethod4_Click);
99 //
100 // btnMethod5
101 //
102 this.btnMethod5.Location = new System.Drawing.Point(120, 136);
103 this.btnMethod5.Name = "btnMethod5";
104 this.btnMethod5.Size = new System.Drawing.Size(72, 32);
105 this.btnMethod5.TabIndex = 4;
106 this.btnMethod5.Text = "Method5";
107 this.btnMethod5.Click += new System.EventHandler(this.btnMethod5_Click);
108 //
109 // Form1
110 //
111 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
112 this.ClientSize = new System.Drawing.Size(320, 189);
113 this.Controls.Add(this.btnMethod5);
114 this.Controls.Add(this.btnMethod4);
115 this.Controls.Add(this.btnMethod3);
116 this.Controls.Add(this.btnMethod2);
117 this.Controls.Add(this.btnMethod1);
118 this.MaximizeBox = false;
119 this.Name = "Form1";
120 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
121 this.Text = "Demo";
122 this.ResumeLayout(false);
123
124 }
125 #endregion
126
127 [STAThread]
128 staticvoid Main()
129 {
130 Application.Run(new Form1());
131 }
132
133 privatevoid btnMethod1_Click(object sender, System.EventArgs e)
134 {
135 System.IntPtr hwnd = NativeWin32.FindWindow(null,"Demo");
136 Form1 form = System.Windows.Forms.Form.FromHandle(hwnd) as Form1;
137 MessageBox.Show((null != form ? "Success!" : "Failure"),"Method1");
138 }
139
140 privatevoid btnMethod2_Click(object sender, System.EventArgs e)
141 {
142 System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess();
143 System.IntPtr hwnd = process.MainWindowHandle;
144 Form1 form = System.Windows.Forms.Form.FromHandle(hwnd) as Form1;
145 MessageBox.Show((null != form ? "Success!" : "Failure"),"Method2");
146
147 }
148
149 privatebool _isFind = false;
150 privatevoid btnMethod3_Click(object sender, System.EventArgs e)
151 {
152
153 System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess();
154
155 foreach (System.Diagnostics.ProcessThread thread in process.Threads)
156 {
157 _isFind = false;
158 NativeWin32.CallBackFunction callBackFun = new NativeWin32.CallBackFunction(CallBackMethod);
159 NativeWin32.EnumThreadWindows(thread.Id,callBackFun,IntPtr.Zero);
160
161 if (_isFind)
162 {
163 MessageBox.Show("Success!","Method3");
164 break;
165 }
166 }
167
168 if (!_isFind) MessageBox.Show("Failure!","Method3");
169 }
170
171
172 privatevoid btnMethod4_Click(object sender, System.EventArgs e)
173 {
174
175 _isFind = false;
176 NativeWin32.CallBackFunction callBackFun = new NativeWin32.CallBackFunction(CallBackMethod);
177
178 NativeWin32.EnumThreadWindows(AppDomain.GetCurrentThreadId(),callBackFun,IntPtr.Zero);
179
180 MessageBox.Show((_isFind ? "Success!" : "Failure"),"Method4");
181 }
182
183 privatevoid btnMethod5_Click(object sender, System.EventArgs e)
184 {
185 MessageBox.Show(".Net 2.0 Usage/r/nSystem.Windows.Forms.Application.OpenForms[/"Form1/"]","Method5");
186 }
187
188 privatebool CallBackMethod(IntPtr hwnd, IntPtr lParam)
189 {
190 Form1 form = System.Windows.Forms.Form.FromHandle(hwnd) as Form1;
191 if (null != form) _isFind = true;
192 returntrue;
193 }
194
195 }
196}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐