namespace AutoChangeWindow
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnGetProcessList = new System.Windows.Forms.Button();
this.btnStart = new System.Windows.Forms.Button();
this.txtWin1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtWin2 = new System.Windows.Forms.TextBox();
this.txtWin3 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.btnStop = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.txtSpan1 = new System.Windows.Forms.TextBox();
this.txtSpan2 = new System.Windows.Forms.TextBox();
this.txtSpan3 = new System.Windows.Forms.TextBox();
this.label8 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnGetProcessList
//
this.btnGetProcessList.Location = new System.Drawing.Point(-, -);
this.btnGetProcessList.Name = "btnGetProcessList";
this.btnGetProcessList.Size = new System.Drawing.Size(, );
this.btnGetProcessList.TabIndex = ;
this.btnGetProcessList.Text = "获取进程列表";
this.btnGetProcessList.UseVisualStyleBackColor = true;
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(, );
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(, );
this.btnStart.TabIndex = ;
this.btnStart.Text = "启动";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// txtWin1
//
this.txtWin1.Location = new System.Drawing.Point(, );
this.txtWin1.Name = "txtWin1";
this.txtWin1.Size = new System.Drawing.Size(, );
this.txtWin1.TabIndex = ;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(, );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "窗口1:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(, );
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(, );
this.label2.TabIndex = ;
this.label2.Text = "窗口2:";
//
// txtWin2
//
this.txtWin2.Location = new System.Drawing.Point(, );
this.txtWin2.Name = "txtWin2";
this.txtWin2.Size = new System.Drawing.Size(, );
this.txtWin2.TabIndex = ;
//
// txtWin3
//
this.txtWin3.Location = new System.Drawing.Point(, );
this.txtWin3.Name = "txtWin3";
this.txtWin3.Size = new System.Drawing.Size(, );
this.txtWin3.TabIndex = ;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(, );
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(, );
this.label3.TabIndex = ;
this.label3.Text = "窗口3:";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(, );
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(, );
this.label4.TabIndex = ;
this.label4.Text = "填写窗口名称";
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(, );
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(, );
this.btnStop.TabIndex = ;
this.btnStop.Text = "停止";
this.btnStop.UseVisualStyleBackColor = true;
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(, );
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(, );
this.label5.TabIndex = ;
this.label5.Text = "间隔";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(, );
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(, );
this.label6.TabIndex = ;
this.label6.Text = "间隔";
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(, );
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(, );
this.label7.TabIndex = ;
this.label7.Text = "间隔";
//
// txtSpan1
//
this.txtSpan1.Location = new System.Drawing.Point(, );
this.txtSpan1.Name = "txtSpan1";
this.txtSpan1.Size = new System.Drawing.Size(, );
this.txtSpan1.TabIndex = ;
//
// txtSpan2
//
this.txtSpan2.Location = new System.Drawing.Point(, );
this.txtSpan2.Name = "txtSpan2";
this.txtSpan2.Size = new System.Drawing.Size(, );
this.txtSpan2.TabIndex = ;
//
// txtSpan3
//
this.txtSpan3.Location = new System.Drawing.Point(, );
this.txtSpan3.Name = "txtSpan3";
this.txtSpan3.Size = new System.Drawing.Size(, );
this.txtSpan3.TabIndex = ;
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(, );
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(, );
this.label8.TabIndex = ;
this.label8.Text = "秒";
//
// label9
//
this.label9.AutoSize = true;
this.label9.Location = new System.Drawing.Point(, );
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(, );
this.label9.TabIndex = ;
this.label9.Text = "秒";
//
// label10
//
this.label10.AutoSize = true;
this.label10.Location = new System.Drawing.Point(, );
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(, );
this.label10.TabIndex = ;
this.label10.Text = "秒";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.label10);
this.Controls.Add(this.label9);
this.Controls.Add(this.label8);
this.Controls.Add(this.txtSpan3);
this.Controls.Add(this.txtSpan2);
this.Controls.Add(this.txtSpan1);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.label4);
this.Controls.Add(this.txtWin3);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtWin2);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtWin1);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.btnGetProcessList);
this.Name = "Form1";
this.Text = "窗口自动切换";
this.Load += new System.EventHandler(this.Form1_Load);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnGetProcessList;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.TextBox txtWin1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtWin2;
private System.Windows.Forms.TextBox txtWin3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox txtSpan1;
private System.Windows.Forms.TextBox txtSpan2;
private System.Windows.Forms.TextBox txtSpan3;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label10;
}
}
 using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading; namespace AutoChangeWindow
{ public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); this.txtWin1.Text = "计算器";
this.txtWin2.Text = "无标题 - 记事本";
this.txtWin3.Text = "窗口自动切换"; this.txtSpan1.Text = this.txtSpan2.Text = this.txtSpan3.Text = ""; } #region API [DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("kernel32.dll")]
private static extern double GetCurrentThreadId(); [DllImport("user32.dll")]
private static extern double GetWindowThreadProcessId(IntPtr hWnd, string lpdwProcessId);
[DllImport("user32.dll")]
private static extern bool AttachThreadInput(double idAttach, double idAttachTo, bool fAttach); [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow); [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
static extern bool SetWindowPos(
IntPtr hWnd,
IntPtr hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
uint uFlags
);
static readonly IntPtr HWND_TOPMOST = new IntPtr(-);
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-);
static readonly IntPtr HWND_TOP = new IntPtr();
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_NOZORDER = 0x0004;
const UInt32 SWP_NOREDRAW = 0x0008;
const UInt32 SWP_NOACTIVATE = 0x0010;
const UInt32 SWP_FRAMECHANGED = 0x0020;
const UInt32 SWP_SHOWWINDOW = 0x0040;
const UInt32 SWP_HIDEWINDOW = 0x0080;
const UInt32 SWP_NOCOPYBITS = 0x0100;
const UInt32 SWP_NOOWNERZORDER = 0x0200;
const UInt32 SWP_NOSENDCHANGING = 0x0400;
const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE; const UInt32 SW_HIDE = ; // ' {隐藏}Private Const SW_ShowNormal = 1 ' {用最近的大小和位置显示, 激活}/
const UInt32 SW_NORMAL = ; // ' {同 SW_ShowNORMAL}Private Const SW_ShowMinimized = 2 ' {最小化, 激活}
const UInt32 SW_ShowMaximized = ; // ' {最大化, 激活}Private Const SW_Maximize = 3 ' {同 SW_ShowMaximized}
const UInt32 SW_ShowNOACTIVATE = ; // ' {用最近的大小和位置显示, 不激活}Private Const SW_Show = 5 ' {同 SW_ShowNORMAL}
const UInt32 SW_Minimize = ; // ' {最小化, 不激活}Private Const SW_ShowMinNOACTIVE = 7 ' {同 SW_Minimize}
const UInt32 SW_ShowNA = ; // ' {同 SW_ShowNOACTIVATE}Private Const SW_RESTORE = 9 ' {同 SW_ShowNORMAL}
const UInt32 SW_ShowDEFAULT = ; // ' {同 SW_ShowNORMAL}Private Const SW_Max = 10 ' {同 SW_ShowNORMAL} #endregion private void Form1_Load(object sender, EventArgs e)
{ } int n = ; protected void ExecChangeWindow()
{
do
{
IntPtr hWnd = FindWindow(null, "计算器");
double dwForeID;
double dwCurID; IntPtr hForeWnd = GetForegroundWindow();
dwCurID = GetCurrentThreadId();
dwForeID = GetWindowThreadProcessId(hForeWnd, null);
AttachThreadInput(dwCurID, dwForeID, true);
ShowWindow(hWnd, SW_ShowMaximized);
SetWindowPos(hWnd, HWND_TOPMOST, , , , , SWP_NOSIZE | SWP_NOMOVE);
SetWindowPos(hWnd, HWND_NOTOPMOST, , , , , SWP_NOSIZE | SWP_NOMOVE);
SetForegroundWindow(hWnd);
AttachThreadInput(dwCurID, dwForeID, false);
int span1 = int.Parse("");
Thread.Sleep(span1 * ); hWnd = FindWindow(null, "无标题 - 记事本"); hForeWnd = GetForegroundWindow();
dwCurID = GetCurrentThreadId();
dwForeID = GetWindowThreadProcessId(hForeWnd, null);
AttachThreadInput(dwCurID, dwForeID, true);
ShowWindow(hWnd, SW_ShowMaximized);
SetWindowPos(hWnd, HWND_TOPMOST, , , , , SWP_NOSIZE | SWP_NOMOVE);
SetWindowPos(hWnd, HWND_NOTOPMOST, , , , , SWP_NOSIZE | SWP_NOMOVE);
SetForegroundWindow(hWnd);
AttachThreadInput(dwCurID, dwForeID, false); span1 = int.Parse(""); Thread.Sleep(span1 * ); //hWnd = FindWindow(null, "窗口自动切换"); //hForeWnd = GetForegroundWindow();
//dwCurID = GetCurrentThreadId();
//dwForeID = GetWindowThreadProcessId(hForeWnd, null);
//AttachThreadInput(dwCurID, dwForeID, true);
//ShowWindow(hWnd, SW_ShowMaximized);
//SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
//SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
//SetForegroundWindow(hWnd);
//AttachThreadInput(dwCurID, dwForeID, false); //span1 = int.Parse("10"); //Thread.Sleep(span1 * 1000); }
while (n > );
} private void btnStart_Click(object sender, EventArgs e)
{ Thread thread = new Thread(new ThreadStart(ExecChangeWindow)); // ThreadPool.QueueUserWorkItem(new WaitCallback(ExecChangeWindow)); } private void btnStop_Click(object sender, EventArgs e)
{ n = ;
} private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F12)
{ }
} }
}

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

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

C#自动切换Windows窗口程序,如何才能调出主窗口?的更多相关文章

  1. MFC窗口程序显示命令行输出窗口的方法

    来源:http://blog.csdn.net/k_shmily/article/details/52596927 WINDOWS为你提供了一系列的API来完成这个功能,例如:ReadConsole, ...

  2. python + selenium webdriver 从主窗口A跳转至主窗口B后,无法定位窗口B的元素的问题

    在做登录脚本的时候,如果只是单纯从登录页面进行元素定位的话,并不存在这个问题 但实际情况是,从首页A进入到登录页面B(并非弹出框),这时候在页面B无法定位到该页面的元素 问题:从页面A进入页面B,无法 ...

  3. [Qt Creator 快速入门] 第5章 应用程序主窗口

    对于日常见到的应用程序而言,许多都是基于主窗口的,主窗口中包含了菜单栏.工具栏.状态栏和中心区域等.这一章会详细介绍主窗口的每一个部分,还会涉及资源管理.富文本处理.拖放操作和文档打印等相关内容.重点 ...

  4. C# 控制台程序如何能不显示窗口

    创建好控制台程序后,选中项目,右键,属性,输出类型选为“windows 应用程序” 这样就没有dos窗口了

  5. VS2010 win32项目windows窗体程序 向导生成代码解析

    目录: 1.Win32项目的windows窗体程序的向导生成了如下代码 2.手工生成代码如下 3.当消息队列中没有消息需要处理,我们可以利用这段时间处理我们自己的任务 1.Win32项目的window ...

  6. win32程序之窗口程序,以及消息机制

    win32程序值窗口程序,以及消息机制 一丶简介 通过上一讲.我们了解了窗口其实是绘制出来的.而且是不断绘制的过程. 所以窗口的本质是绘制. 但是我们现在看到的窗口程序.都可以点击关闭按钮. 使用鼠标 ...

  7. Windows程序设计1(工具、编码、窗口)

    一.几个常用小工具: 1. 编译器:CL.EXE   将源文件转变为目标文件(汇编语言). CL.EXE  /c  xxx.c  或   xx.cpp cl.exe  -? 显示cl帮助 cl.exe ...

  8. WinForm 之 程序启动不显示主窗体

    在 WinForm 程序启动时,不显示主窗体的实现方法主要有以下5种,第五种最简单,而且效果也不错,第四种方法也值得推荐. 实例代码如下: //隐藏窗体的方法1/5:不指定任何窗体为主窗体 //注意: ...

  9. Qt5 主窗口组成

    1. 菜单栏 菜单是一系列命令的列表.为了实现菜单.工具栏按钮.键盘快捷键等命令的一致性,Qt使用动作(Action)来表示这些命令.Qt的菜单就是由一系列的QAction动作对象构成的列表,而菜单栏 ...

随机推荐

  1. 华为上机测试题(及格分数线-java)

    PS:自己写的,自测试OK,供大家参考. /* 描述:10个学生考完期末考试评卷完成后,A老师需要划出及格线,要求如下:(1) 及格线是10的倍数:(2) 保证至少有60%的学生及格:(3) 如果所有 ...

  2. [转]Google 的开源技术protobuf 简介与例子

    本文来自CSDN博客:http://blog.csdn.NET/program_think/archive/2009/05/31/4229773.aspx 今天来介绍一下“Protocol Buffe ...

  3. unicode和utf8之间的关系

    ,字符编码是计算机技术的基石,想要熟练使用计算机,懂得一点字符编码的知识,还是很有必要的. 1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit ...

  4. flask框架基本使用(1)(基本框架搭建与请求参数接收)

    #转载请留言联系 Flask 是一个 Python 实现的 Web 开发微框架. 1.搭建Flask开发环境 在你开发项目的时候,你拥有的项目越多,同时使用不同版本的 Python 工作的可能性也就越 ...

  5. java获取当前类名和方法名

    Description Below I present you two different ways to get the current Class: Using Thread Using getC ...

  6. poj 1389(离散化+计算几何)

    Area of Simple Polygons Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3412   Accepted ...

  7. springBoot Ribbon Hystrix

    1.依赖包引入 <!-- 引入关于 hystrix的依赖 --> <dependency> <groupId>org.springframework.cloud&l ...

  8. HDU 1394.Minimum Inversion Number-最小逆序数-完全版线段树(单点增减、区间求和)

    HDU1394.Minimum Inversion Number 这个题求最小逆序数,先建一个空的树,然后每输入一个值,就先查询一下,查询之后,更新线段树,然后遍历一遍,每次将第一个数放到最后之后,减 ...

  9. Codeforces 1028E. Restore Array

    题目直通车:http://codeforces.com/problemset/problem/1028/E 解法:设原数组为ar[],求ar中的最大值的下标ins,依次向前遍历一遍,每一个答案值都为前 ...

  10. 解决android模拟器连接本机服务器”Connection refused”问题

      在本机用模拟器连接 localhost 的服务器不成功,经查询是我反了一个小错误. android 模拟器其本身的localhost就是它自己的ip,而如果我要连接本机的localhost则需要将 ...