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. Vue全局异常捕获

    之前没注意过这么个小技巧 , 可能在Vue文档里也有  暂时先记下了 方便摘要 Vue全局配置 errorHandler可以进行全局错误收集,我们可以根据这个特性对前端异常做这样的处理:业务错误直接写 ...

  2. python--io

    import io # io模块里面主要使用StringIo和BytesIo # StringIo f = io.StringIO() # 像使用open函数创建文件一样,生成一个句柄 # 可以直接向 ...

  3. bootstrapValidator 版本差异问题导致的submitHandler失效问题

    我用过的两个版本: v0.5.2-dev,0.4.5 这里针对于提交方法进行说明一下,如下代码: <script> $(function () { $("#addUserForm ...

  4. js正则常用的一些东西

    mdn的正则文档 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions js正则表达式的分 ...

  5. 厦门海沧区磁盘只有1TB的解决方案

    厦门海沧区磁盘只有1TB的解决方案 1.为WINDOWS 2008 r2 服务器增加5个1T的硬盘 2.打开命令提示符,并键入 diskpart. 3.在“DISKPART”提示符下,键入 list ...

  6. Ubuntu安装java环境

    Ubuntu安装java环境 1.添加ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 2.安装oracle-j ...

  7. (13)C#数组

    如果需要使用同一类型的多个对象,数组是一种数据结构,它包含同一类型的多个元素 一.一维数组 1.数组的声明方法 int [] a; 因为数组是引用类型,引用类型有个特点是:声明变量时不会马上分配一个内 ...

  8. codevs 2837 考前复习——01背包

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description Aiden马上要考试了,可他还没怎么复习,于是他 ...

  9. java collection get 方法

    Collections unmodifiableList

  10. XTU | 人工智能入门复习总结

    写在前面 本文严禁转载,只限于学习交流. 课件分享在这里了. 还有人工智能标准化白皮书(2018版)也一并分享了. 绪论 人工智能的定义与发展 定义 一般解释:人工智能就是用 人工的方法在 **机器( ...