C#自动切换Windows窗口程序,如何才能调出主窗口?
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窗口程序,如何才能调出主窗口?的更多相关文章
- MFC窗口程序显示命令行输出窗口的方法
来源:http://blog.csdn.net/k_shmily/article/details/52596927 WINDOWS为你提供了一系列的API来完成这个功能,例如:ReadConsole, ...
- python + selenium webdriver 从主窗口A跳转至主窗口B后,无法定位窗口B的元素的问题
在做登录脚本的时候,如果只是单纯从登录页面进行元素定位的话,并不存在这个问题 但实际情况是,从首页A进入到登录页面B(并非弹出框),这时候在页面B无法定位到该页面的元素 问题:从页面A进入页面B,无法 ...
- [Qt Creator 快速入门] 第5章 应用程序主窗口
对于日常见到的应用程序而言,许多都是基于主窗口的,主窗口中包含了菜单栏.工具栏.状态栏和中心区域等.这一章会详细介绍主窗口的每一个部分,还会涉及资源管理.富文本处理.拖放操作和文档打印等相关内容.重点 ...
- C# 控制台程序如何能不显示窗口
创建好控制台程序后,选中项目,右键,属性,输出类型选为“windows 应用程序” 这样就没有dos窗口了
- VS2010 win32项目windows窗体程序 向导生成代码解析
目录: 1.Win32项目的windows窗体程序的向导生成了如下代码 2.手工生成代码如下 3.当消息队列中没有消息需要处理,我们可以利用这段时间处理我们自己的任务 1.Win32项目的window ...
- win32程序之窗口程序,以及消息机制
win32程序值窗口程序,以及消息机制 一丶简介 通过上一讲.我们了解了窗口其实是绘制出来的.而且是不断绘制的过程. 所以窗口的本质是绘制. 但是我们现在看到的窗口程序.都可以点击关闭按钮. 使用鼠标 ...
- Windows程序设计1(工具、编码、窗口)
一.几个常用小工具: 1. 编译器:CL.EXE 将源文件转变为目标文件(汇编语言). CL.EXE /c xxx.c 或 xx.cpp cl.exe -? 显示cl帮助 cl.exe ...
- WinForm 之 程序启动不显示主窗体
在 WinForm 程序启动时,不显示主窗体的实现方法主要有以下5种,第五种最简单,而且效果也不错,第四种方法也值得推荐. 实例代码如下: //隐藏窗体的方法1/5:不指定任何窗体为主窗体 //注意: ...
- Qt5 主窗口组成
1. 菜单栏 菜单是一系列命令的列表.为了实现菜单.工具栏按钮.键盘快捷键等命令的一致性,Qt使用动作(Action)来表示这些命令.Qt的菜单就是由一系列的QAction动作对象构成的列表,而菜单栏 ...
随机推荐
- java的io操作(将字符串写入到txt文件中)
import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java ...
- js中立即执行函数写法理解
在理解了一些函数基本概念后,回头看看( function(){…} )()和( function (){…} () )这两种立即执行函数的写法,最初我以为是一个括号包裹匿名函数, 并后面加个括号立即调 ...
- NYOJ 71 乘船问题【贪心】
时间复杂度O(n) 有n个人,第i个人的重量为w[i],每艘船的最大载重量均为c,且最多只能乘两个人.用最少的船装载所有人. 思路:从最轻的开始考虑,让最轻的和最重的一条船,若超出重量则可判定最重的只 ...
- Cosmos DB
类似的数据库还有Google的Spanner. 参考:官网说明 另一个介绍 Key capabilities As a globally distributed database service, A ...
- Spfa【p3385】【模板】负环(spfa)
顾z 你没有发现两个字里的blog都不一样嘛 qwq 题目描述 毒瘤数据要求判负环 分析: 还是融合了不少题解的思想的. 负环定义: 权值和为负的环 //在网络上并没有找到一个官方定义,暂且这么理解. ...
- HNOI2004 郁闷的出纳员(Splay)
郁闷的出纳员 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的 ...
- Linux基本命令参数
简单复习一下基本命令的参数 1.mkdir 两个参数: -p 递归创建该目录 mkdir -p /user/hadoop/test/raid -m 指定权限 mkdir -m 777 /raid ...
- 本地navicatl连接linux
首选你Linux服务器上要装配好了MySQL数据库.输入: # mysql -u root -proot mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@ ...
- AHOI 2009 中国象棋
题面 题目描述 这次小可可想解决的难题和中国象棋有关,在一个N行M列的棋盘上,让你放若干个炮(可以是0个),使得没有一个炮可以攻击到另一个炮,请问有多少种放置方法.大家肯定很清楚,在中国象棋中炮的行走 ...
- 线程流量控制工具之Semaphore
简介 Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源.很多年以来,我都觉得从字面上很难理解Semaphore所表达的含义,只能把它比作是 ...