C#自动切换Windows窗口程序,如何才能调出主窗口?

 
  1 namespace AutoChangeWindow
  2 {
  3     partial class Form1
  4     {
  5         /// <summary>
  6         /// 必需的设计器变量。
  7         /// </summary>
  8         private System.ComponentModel.IContainer components = null;
  9 
 10         /// <summary>
 11         /// 清理所有正在使用的资源。
 12         /// </summary>
 13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
 14         protected override void Dispose(bool disposing)
 15         {
 16             if (disposing && (components != null))
 17             {
 18                 components.Dispose();
 19             }
 20             base.Dispose(disposing);
 21         }
 22 
 23         #region Windows 窗体设计器生成的代码
 24 
 25         /// <summary>
 26         /// 设计器支持所需的方法 - 不要
 27         /// 使用代码编辑器修改此方法的内容。
 28         /// </summary>
 29         private void InitializeComponent()
 30         {
 31             this.btnGetProcessList = new System.Windows.Forms.Button();
 32             this.btnStart = new System.Windows.Forms.Button();
 33             this.txtWin1 = new System.Windows.Forms.TextBox();
 34             this.label1 = new System.Windows.Forms.Label();
 35             this.label2 = new System.Windows.Forms.Label();
 36             this.txtWin2 = new System.Windows.Forms.TextBox();
 37             this.txtWin3 = new System.Windows.Forms.TextBox();
 38             this.label3 = new System.Windows.Forms.Label();
 39             this.label4 = new System.Windows.Forms.Label();
 40             this.btnStop = new System.Windows.Forms.Button();
 41             this.label5 = new System.Windows.Forms.Label();
 42             this.label6 = new System.Windows.Forms.Label();
 43             this.label7 = new System.Windows.Forms.Label();
 44             this.txtSpan1 = new System.Windows.Forms.TextBox();
 45             this.txtSpan2 = new System.Windows.Forms.TextBox();
 46             this.txtSpan3 = new System.Windows.Forms.TextBox();
 47             this.label8 = new System.Windows.Forms.Label();
 48             this.label9 = new System.Windows.Forms.Label();
 49             this.label10 = new System.Windows.Forms.Label();
 50             this.SuspendLayout();
 51             // 
 52             // btnGetProcessList
 53             // 
 54             this.btnGetProcessList.Location = new System.Drawing.Point(-78, -16);
 55             this.btnGetProcessList.Name = "btnGetProcessList";
 56             this.btnGetProcessList.Size = new System.Drawing.Size(75, 23);
 57             this.btnGetProcessList.TabIndex = 10;
 58             this.btnGetProcessList.Text = "获取进程列表";
 59             this.btnGetProcessList.UseVisualStyleBackColor = true;
 60             // 
 61             // btnStart
 62             // 
 63             this.btnStart.Location = new System.Drawing.Point(60, 103);
 64             this.btnStart.Name = "btnStart";
 65             this.btnStart.Size = new System.Drawing.Size(75, 23);
 66             this.btnStart.TabIndex = 11;
 67             this.btnStart.Text = "启动";
 68             this.btnStart.UseVisualStyleBackColor = true;
 69             this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
 70             // 
 71             // txtWin1
 72             // 
 73             this.txtWin1.Location = new System.Drawing.Point(60, 23);
 74             this.txtWin1.Name = "txtWin1";
 75             this.txtWin1.Size = new System.Drawing.Size(195, 21);
 76             this.txtWin1.TabIndex = 12;
 77             // 
 78             // label1
 79             // 
 80             this.label1.AutoSize = true;
 81             this.label1.Location = new System.Drawing.Point(13, 29);
 82             this.label1.Name = "label1";
 83             this.label1.Size = new System.Drawing.Size(47, 12);
 84             this.label1.TabIndex = 13;
 85             this.label1.Text = "窗口1:";
 86             // 
 87             // label2
 88             // 
 89             this.label2.AutoSize = true;
 90             this.label2.Location = new System.Drawing.Point(13, 53);
 91             this.label2.Name = "label2";
 92             this.label2.Size = new System.Drawing.Size(47, 12);
 93             this.label2.TabIndex = 14;
 94             this.label2.Text = "窗口2:";
 95             // 
 96             // txtWin2
 97             // 
 98             this.txtWin2.Location = new System.Drawing.Point(60, 47);
 99             this.txtWin2.Name = "txtWin2";
100             this.txtWin2.Size = new System.Drawing.Size(195, 21);
101             this.txtWin2.TabIndex = 15;
102             // 
103             // txtWin3
104             // 
105             this.txtWin3.Location = new System.Drawing.Point(60, 71);
106             this.txtWin3.Name = "txtWin3";
107             this.txtWin3.Size = new System.Drawing.Size(195, 21);
108             this.txtWin3.TabIndex = 17;
109             // 
110             // label3
111             // 
112             this.label3.AutoSize = true;
113             this.label3.Location = new System.Drawing.Point(13, 77);
114             this.label3.Name = "label3";
115             this.label3.Size = new System.Drawing.Size(47, 12);
116             this.label3.TabIndex = 16;
117             this.label3.Text = "窗口3:";
118             // 
119             // label4
120             // 
121             this.label4.AutoSize = true;
122             this.label4.Location = new System.Drawing.Point(58, 8);
123             this.label4.Name = "label4";
124             this.label4.Size = new System.Drawing.Size(77, 12);
125             this.label4.TabIndex = 18;
126             this.label4.Text = "填写窗口名称";
127             // 
128             // btnStop
129             // 
130             this.btnStop.Location = new System.Drawing.Point(156, 103);
131             this.btnStop.Name = "btnStop";
132             this.btnStop.Size = new System.Drawing.Size(75, 23);
133             this.btnStop.TabIndex = 19;
134             this.btnStop.Text = "停止";
135             this.btnStop.UseVisualStyleBackColor = true;
136             this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
137             // 
138             // label5
139             // 
140             this.label5.AutoSize = true;
141             this.label5.Location = new System.Drawing.Point(262, 28);
142             this.label5.Name = "label5";
143             this.label5.Size = new System.Drawing.Size(29, 12);
144             this.label5.TabIndex = 20;
145             this.label5.Text = "间隔";
146             // 
147             // label6
148             // 
149             this.label6.AutoSize = true;
150             this.label6.Location = new System.Drawing.Point(262, 53);
151             this.label6.Name = "label6";
152             this.label6.Size = new System.Drawing.Size(29, 12);
153             this.label6.TabIndex = 21;
154             this.label6.Text = "间隔";
155             // 
156             // label7
157             // 
158             this.label7.AutoSize = true;
159             this.label7.Location = new System.Drawing.Point(262, 75);
160             this.label7.Name = "label7";
161             this.label7.Size = new System.Drawing.Size(29, 12);
162             this.label7.TabIndex = 22;
163             this.label7.Text = "间隔";
164             // 
165             // txtSpan1
166             // 
167             this.txtSpan1.Location = new System.Drawing.Point(298, 23);
168             this.txtSpan1.Name = "txtSpan1";
169             this.txtSpan1.Size = new System.Drawing.Size(37, 21);
170             this.txtSpan1.TabIndex = 23;
171             // 
172             // txtSpan2
173             // 
174             this.txtSpan2.Location = new System.Drawing.Point(298, 47);
175             this.txtSpan2.Name = "txtSpan2";
176             this.txtSpan2.Size = new System.Drawing.Size(37, 21);
177             this.txtSpan2.TabIndex = 24;
178             // 
179             // txtSpan3
180             // 
181             this.txtSpan3.Location = new System.Drawing.Point(298, 70);
182             this.txtSpan3.Name = "txtSpan3";
183             this.txtSpan3.Size = new System.Drawing.Size(37, 21);
184             this.txtSpan3.TabIndex = 25;
185             // 
186             // label8
187             // 
188             this.label8.AutoSize = true;
189             this.label8.Location = new System.Drawing.Point(341, 29);
190             this.label8.Name = "label8";
191             this.label8.Size = new System.Drawing.Size(17, 12);
192             this.label8.TabIndex = 26;
193             this.label8.Text = "";
194             // 
195             // label9
196             // 
197             this.label9.AutoSize = true;
198             this.label9.Location = new System.Drawing.Point(341, 52);
199             this.label9.Name = "label9";
200             this.label9.Size = new System.Drawing.Size(17, 12);
201             this.label9.TabIndex = 27;
202             this.label9.Text = "";
203             // 
204             // label10
205             // 
206             this.label10.AutoSize = true;
207             this.label10.Location = new System.Drawing.Point(341, 75);
208             this.label10.Name = "label10";
209             this.label10.Size = new System.Drawing.Size(17, 12);
210             this.label10.TabIndex = 28;
211             this.label10.Text = "";
212             // 
213             // Form1
214             // 
215             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
216             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
217             this.ClientSize = new System.Drawing.Size(490, 175);
218             this.Controls.Add(this.label10);
219             this.Controls.Add(this.label9);
220             this.Controls.Add(this.label8);
221             this.Controls.Add(this.txtSpan3);
222             this.Controls.Add(this.txtSpan2);
223             this.Controls.Add(this.txtSpan1);
224             this.Controls.Add(this.label7);
225             this.Controls.Add(this.label6);
226             this.Controls.Add(this.label5);
227             this.Controls.Add(this.btnStop);
228             this.Controls.Add(this.label4);
229             this.Controls.Add(this.txtWin3);
230             this.Controls.Add(this.label3);
231             this.Controls.Add(this.txtWin2);
232             this.Controls.Add(this.label2);
233             this.Controls.Add(this.label1);
234             this.Controls.Add(this.txtWin1);
235             this.Controls.Add(this.btnStart);
236             this.Controls.Add(this.btnGetProcessList);
237             this.Name = "Form1";
238             this.Text = "窗口自动切换";
239             this.Load += new System.EventHandler(this.Form1_Load);
240             this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
241             this.ResumeLayout(false);
242             this.PerformLayout();
243 
244         }
245 
246         #endregion
247 
248         private System.Windows.Forms.Button btnGetProcessList;
249         private System.Windows.Forms.Button btnStart;
250         private System.Windows.Forms.TextBox txtWin1;
251         private System.Windows.Forms.Label label1;
252         private System.Windows.Forms.Label label2;
253         private System.Windows.Forms.TextBox txtWin2;
254         private System.Windows.Forms.TextBox txtWin3;
255         private System.Windows.Forms.Label label3;
256         private System.Windows.Forms.Label label4;
257         private System.Windows.Forms.Button btnStop;
258         private System.Windows.Forms.Label label5;
259         private System.Windows.Forms.Label label6;
260         private System.Windows.Forms.Label label7;
261         private System.Windows.Forms.TextBox txtSpan1;
262         private System.Windows.Forms.TextBox txtSpan2;
263         private System.Windows.Forms.TextBox txtSpan3;
264         private System.Windows.Forms.Label label8;
265         private System.Windows.Forms.Label label9;
266         private System.Windows.Forms.Label label10;
267     }
268 }
View Code
  1 using System;
  2 using System.Windows.Forms;
  3 using System.Diagnostics;
  4 using System.Runtime.InteropServices;
  5 using System.Threading;
  6 
  7 namespace AutoChangeWindow
  8 {
  9 
 10     public partial class Form1 : Form
 11     {
 12         public Form1()
 13         {
 14             InitializeComponent();
 15 
 16            
 17             this.txtWin1.Text = "计算器";
 18             this.txtWin2.Text = "无标题 - 记事本";
 19             this.txtWin3.Text = "窗口自动切换";
 20 
 21             this.txtSpan1.Text = this.txtSpan2.Text = this.txtSpan3.Text = "10";
 22 
 23 
 24         }
 25 
 26         #region API
 27 
 28 
 29         [DllImport("user32.dll")]
 30         private static extern bool SetForegroundWindow(IntPtr hWnd);
 31         [DllImport("user32.dll")]
 32         private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 33 
 34         [DllImport("user32.dll")]
 35         private static extern IntPtr GetForegroundWindow();
 36         [DllImport("kernel32.dll")]
 37         private static extern double GetCurrentThreadId();
 38 
 39         [DllImport("user32.dll")]
 40         private static extern double GetWindowThreadProcessId(IntPtr hWnd, string lpdwProcessId);
 41         [DllImport("user32.dll")]
 42         private static extern bool AttachThreadInput(double idAttach, double idAttachTo, bool fAttach);
 43 
 44         [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
 45         static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
 46 
 47         [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
 48         static extern bool SetWindowPos(
 49         IntPtr hWnd,
 50         IntPtr hWndInsertAfter,
 51         int X,
 52         int Y,
 53         int cx,
 54         int cy,
 55         uint uFlags
 56         );
 57         static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
 58         static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
 59         static readonly IntPtr HWND_TOP = new IntPtr(0);
 60         const UInt32 SWP_NOSIZE = 0x0001;
 61         const UInt32 SWP_NOMOVE = 0x0002;
 62         const UInt32 SWP_NOZORDER = 0x0004;
 63         const UInt32 SWP_NOREDRAW = 0x0008;
 64         const UInt32 SWP_NOACTIVATE = 0x0010;
 65         const UInt32 SWP_FRAMECHANGED = 0x0020;
 66         const UInt32 SWP_SHOWWINDOW = 0x0040;
 67         const UInt32 SWP_HIDEWINDOW = 0x0080;
 68         const UInt32 SWP_NOCOPYBITS = 0x0100;
 69         const UInt32 SWP_NOOWNERZORDER = 0x0200;
 70         const UInt32 SWP_NOSENDCHANGING = 0x0400;
 71         const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
 72 
 73         const UInt32 SW_HIDE = 0;             // ' {隐藏}Private Const SW_ShowNormal = 1         ' {用最近的大小和位置显示, 激活}/
 74         const UInt32 SW_NORMAL = 1;          // ' {同 SW_ShowNORMAL}Private Const SW_ShowMinimized = 2      ' {最小化, 激活}
 75         const UInt32 SW_ShowMaximized = 3;    // ' {最大化, 激活}Private Const SW_Maximize = 3           ' {同 SW_ShowMaximized}
 76         const UInt32 SW_ShowNOACTIVATE = 4;   // ' {用最近的大小和位置显示, 不激活}Private Const SW_Show = 5               ' {同 SW_ShowNORMAL}
 77         const UInt32 SW_Minimize = 6;       // ' {最小化, 不激活}Private Const SW_ShowMinNOACTIVE = 7    ' {同 SW_Minimize}
 78         const UInt32 SW_ShowNA = 8;          // ' {同 SW_ShowNOACTIVATE}Private Const SW_RESTORE = 9            ' {同 SW_ShowNORMAL}
 79         const UInt32 SW_ShowDEFAULT = 10;    // ' {同 SW_ShowNORMAL}Private Const SW_Max = 10               ' {同 SW_ShowNORMAL}
 80         
 81         #endregion
 82 
 83         private void Form1_Load(object sender, EventArgs e)
 84         {
 85 
 86         }
 87 
 88 
 89         int n = 1;
 90 
 91         protected void ExecChangeWindow()
 92         {
 93             do
 94             {
 95                 IntPtr hWnd = FindWindow(null, "计算器");
 96                 double dwForeID;
 97                 double dwCurID;
 98 
 99                 IntPtr hForeWnd = GetForegroundWindow();
100                 dwCurID = GetCurrentThreadId();
101                 dwForeID = GetWindowThreadProcessId(hForeWnd, null);
102                 AttachThreadInput(dwCurID, dwForeID, true);
103                 ShowWindow(hWnd, SW_ShowMaximized);
104                 SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
105                 SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
106                 SetForegroundWindow(hWnd);
107                 AttachThreadInput(dwCurID, dwForeID, false);
108                 int span1 = int.Parse("10");
109                 Thread.Sleep(span1 * 1000);
110 
111                 hWnd = FindWindow(null, "无标题 - 记事本");
112 
113                 hForeWnd = GetForegroundWindow();
114                 dwCurID = GetCurrentThreadId();
115                 dwForeID = GetWindowThreadProcessId(hForeWnd, null);
116                 AttachThreadInput(dwCurID, dwForeID, true);
117                 ShowWindow(hWnd, SW_ShowMaximized);
118                 SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
119                 SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
120                 SetForegroundWindow(hWnd);
121                 AttachThreadInput(dwCurID, dwForeID, false);
122 
123                 span1 = int.Parse("10");
124 
125                Thread.Sleep(span1 * 1000);
126 
127 
128 
129                //hWnd = FindWindow(null, "窗口自动切换");
130 
131                //hForeWnd = GetForegroundWindow();
132                //dwCurID = GetCurrentThreadId();
133                //dwForeID = GetWindowThreadProcessId(hForeWnd, null);
134                //AttachThreadInput(dwCurID, dwForeID, true);
135                //ShowWindow(hWnd, SW_ShowMaximized);
136                //SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
137                //SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
138                //SetForegroundWindow(hWnd);
139                //AttachThreadInput(dwCurID, dwForeID, false);
140 
141                //span1 = int.Parse("10");
142 
143                //Thread.Sleep(span1 * 1000);
144 
145             }
146             while (n > 0);
147         }
148 
149 
150         private void btnStart_Click(object sender, EventArgs e)
151         {
152 
153 
154             Thread thread = new Thread(new ThreadStart(ExecChangeWindow));
155             
156           //  ThreadPool.QueueUserWorkItem(new WaitCallback(ExecChangeWindow));
157 
158         }
159 
160         private void btnStop_Click(object sender, EventArgs e)
161         {
162      
163             n = 0;
164         }
165 
166         private void Form1_KeyDown(object sender, KeyEventArgs e)
167         {
168             if (e.KeyCode == Keys.F12)
169             {
170                 
171             }
172         }
173 
174 
175 
176 
177 
178 
179     }
180 }
View Code


求教:为何我的主窗口切不到最上面,一直显示“无法响应”,我猜的原因是Thread.Sleep()导致的。

请问我该如何做,可以切到主窗口并能执行“停止”动作呢?

原文地址:https://www.cnblogs.com/mishy/p/3682606.html