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. 终于遇到了传说的ie 6 img 3px的bug

    最近在做一个网站,基本上已经算完成,就开始完善细节部分了. IE6可能是微软最为YD 的一款浏览器了吧,至今还没有退出历史的舞台,尽管google都宣布不在支持它了. 因为该死的ie6,虽死但是牢牢地 ...

  2. android的百度地图开发(二) 定位

    参考:http://blog.csdn.net/mr_wzc/article/details/51590485 第一步,初始化LocationClient类 //获取地图控件引用 mMapView = ...

  3. Win32环境下代码注入与API钩子的实现(转)

    本文详细的介绍了在Visual Studio(以下简称VS)下实现API钩子的编程方法,阅读本文需要基础:有操作系统的基本知识(进程管理,内存管理),会在VS下编写和调试Win32应用程序和动态链接库 ...

  4. phpstudy无法访问主页,提示You don't have permission to access / on this server解决办法

    1.输入localhost提示:You don't have permission to access / on this server. 新版phpStudy为了安全,取消Apache和nginx列 ...

  5. (7)C#流程控制

    一.判断语句 if if可以单独使用,else不能单独使用 ; ) { Console.WriteLine("aaa); } Console.WriteLine("xxx" ...

  6. CF988 C. Equal Sums【map+pair/hash/任选两个序列,两个序列都除去他们中的一个数,使的总和相同】

    [链接]:CF988C [题意]:在n个序列中任选两个序列,两个序列都除去他们中的一个数,使的总和相同 [分析]:map<int,pair<int,int> > mp,从0~m ...

  7. 给出一个string字符串,统计里面出现的字符个数

    给出一个string字符串,统计里面出现的字符个数 解决方案: 使用algorithm里面的count函数,使用方法是count(begin,end,'c'),其中begin指的是起始地址,end指的 ...

  8. STL+Floyd【p1690】贪婪的Copy

    Description Copy从卢牛那里听说在一片叫yz的神的领域埋藏着不少宝藏,于是Copy来到了这个被划分为个区域的神地.卢牛告诉了Copy这里共有个宝藏,分别放在第Pi个(1<=Pi&l ...

  9. 基于Bootstrap的下拉框插件bootstrap-select

    写在前面: 在这次的项目中,没有再使用liger-ui做为前端框架了,改为了Bootstrap,这次也好接触下新的技术,在学习的过程中发现,Bootstrap的一些组件基本都是采用class的形式,就 ...

  10. sqlsever 和oracle的参数

    StringBuilder strSql = new StringBuilder(); strSql.Append("insert into YXZY_TSDQWH("); str ...