mirror of
https://github.com/peter-tanner/wmctrl-for-windows.git
synced 2024-11-30 11:10:16 +08:00
First version of wmctrl with -l and -a options
This commit is contained in:
parent
d9a06d63ea
commit
95e6b0d379
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
*.exe
|
18
Makefile
Normal file
18
Makefile
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
|
||||||
|
CSC=csc
|
||||||
|
CSC=mcs
|
||||||
|
|
||||||
|
PROG=wmctrl.exe
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
all: $(PROG) test
|
||||||
|
|
||||||
|
$(PROG): wmctrl.cs
|
||||||
|
$(CSC) wmctrl.cs
|
||||||
|
|
||||||
|
|
||||||
|
test:
|
||||||
|
$(PROG) -h
|
||||||
|
$(PROG) -l
|
||||||
|
$(PROG) -a gvim
|
56
_dev/CommandLineArgs.cs
Normal file
56
_dev/CommandLineArgs.cs
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
public class CommandLine
|
||||||
|
{
|
||||||
|
public static int SwitchToWindow(){
|
||||||
|
Console.WriteLine("SwitchToWindow");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int ListWindows(){
|
||||||
|
Console.WriteLine("ListWindows");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static int Main(string[] args)
|
||||||
|
{
|
||||||
|
int status=0; // Return status for Main
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
// --- Parsing arguments
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
int nArgs=args.Length;
|
||||||
|
Console.WriteLine("Number of command line parameters = {0}", args.Length);
|
||||||
|
|
||||||
|
int i=0;
|
||||||
|
while (i<nArgs) {
|
||||||
|
string s=args[i];
|
||||||
|
switch(s){
|
||||||
|
case "-s":
|
||||||
|
Console.WriteLine("s");
|
||||||
|
status=SwitchToWindow();
|
||||||
|
break;
|
||||||
|
case "-l":
|
||||||
|
Console.WriteLine("l");
|
||||||
|
status=ListWindows();
|
||||||
|
|
||||||
|
Console.WriteLine("Error:");
|
||||||
|
status=-1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Console.WriteLine(args[i]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
if (status!=0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics; // Process
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static void Main()
|
static void Main()
|
||||||
|
|
30
_dev/OpenAndType.cs
Normal file
30
_dev/OpenAndType.cs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
// Note: to compile with Mono you need: mcs /r:System.Windows.Forms.dll
|
||||||
|
//
|
||||||
|
//
|
||||||
|
using System; // For IntPtr
|
||||||
|
using System.Runtime.InteropServices; // DllImport
|
||||||
|
using System.Diagnostics; // Process
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic; // List<string>
|
||||||
|
using System.Windows.Forms; // SendKeys
|
||||||
|
// using System.Threading; // For Thread.Sleep
|
||||||
|
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
//dll import (can't be in method, but needs to be in class
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
public static extern int SetForegroundWindow(IntPtr point);
|
||||||
|
static void Main()
|
||||||
|
{
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
// ---
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
Process p = Process.Start("notepad.exe");
|
||||||
|
p.WaitForInputIdle();
|
||||||
|
IntPtr h = p.MainWindowHandle;
|
||||||
|
SetForegroundWindow(h);
|
||||||
|
SendKeys.SendWait("k");
|
||||||
|
// Thread.Sleep(1000);
|
||||||
|
IntPtr processFoundWindow = p.MainWindowHandle;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,48 +1,41 @@
|
||||||
//declarations
|
// //declarations
|
||||||
using system.IO;
|
using System; // For IntPtr
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices; // DllImport
|
||||||
using System.Diagnostics;
|
using System.Diagnostics; // Process
|
||||||
//more
|
// using System.Windows.Automation; // UIAutomationClient.dll
|
||||||
|
// using System.Threading; // For Thread.Sleep
|
||||||
//namespace here
|
|
||||||
|
|
||||||
//class here
|
|
||||||
|
|
||||||
//initialize method
|
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
|
//dll import (can't be in method, but needs to be in class
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
public static extern void SwitchToThisWindow(IntPtr hWnd);
|
||||||
static void Main()
|
static void Main()
|
||||||
{
|
{
|
||||||
//related .dll import
|
// --------------------------------------------------------------------------------
|
||||||
[DllImport("user32.dll")]
|
// ---
|
||||||
public static extern void SwitchToThisWindow(IntPtr hWnd);
|
// --------------------------------------------------------------------------------
|
||||||
|
String procName = "MATLAB";
|
||||||
String ProcWindow = "main";
|
|
||||||
//function which calls switchWindow() is here but not important
|
//function which calls switchWindow() is here but not important
|
||||||
|
// //
|
||||||
Process[] procs = Process.GetProcessesByName(ProcWindow);
|
Process[] procs = Process.GetProcessesByName(procName);
|
||||||
foreach (Process proc in procs)
|
int nProcs = procs.Length;
|
||||||
{
|
if (nProcs < 1) {
|
||||||
//switch to process by name
|
Console.WriteLine("No process found for name: {0}", procName);
|
||||||
|
}else{
|
||||||
|
// We'll use the first window we found
|
||||||
|
Process proc=procs[0];
|
||||||
|
if (nProcs >1) {
|
||||||
|
Console.WriteLine("{0} processes found with name: {0}",nProcs,procName);
|
||||||
|
Console.WriteLine("Using first process:");
|
||||||
|
Console.WriteLine("Process Name: {0} ID: {1} Title: {2}", proc.ProcessName, proc.Id, proc.MainWindowTitle);
|
||||||
|
}
|
||||||
|
// --- Switching to window using user32.dll function
|
||||||
SwitchToThisWindow(proc.MainWindowHandle);
|
SwitchToThisWindow(proc.MainWindowHandle);
|
||||||
|
|
||||||
Console.WriteLine("Process: {0} ID: {1} Window title: {2}", proc.ProcessName, proc.Id, proc.MainWindowTitle);
|
// --- Alternative method using AutomationElement
|
||||||
}
|
// AutomationElement element = AutomationElement.FromHandle(proc.MainWindowHandle);
|
||||||
|
// if (element != null) { element.SetFocus(); }
|
||||||
|
} // Proc Window found
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------
|
|
||||||
// ---
|
|
||||||
// --------------------------------------------------------------------------------
|
|
||||||
http://stackoverflow.com/questions/2315561/correct-way-in-net-to-switch-the-focus-to-another-application
|
|
||||||
// --------------------------------------------------------------------------------
|
|
||||||
// ---
|
|
||||||
// --------------------------------------------------------------------------------
|
|
||||||
AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
|
|
||||||
if (element != null)
|
|
||||||
{
|
|
||||||
element.SetFocus();
|
|
||||||
}
|
|
||||||
|
|
123
wmctrl.cs
Normal file
123
wmctrl.cs
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
using System; // For IntPtr
|
||||||
|
using System.Runtime.InteropServices; // DllImport
|
||||||
|
using System.Diagnostics; // Process
|
||||||
|
|
||||||
|
public class wmctrl
|
||||||
|
{
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
// --- Switch to Window
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
//dll import (can't be in method, but needs to be in class
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
public static extern void SwitchToThisWindow(IntPtr hWnd);
|
||||||
|
|
||||||
|
public static int SwitchToWindow(string procName){
|
||||||
|
// Getting window matching
|
||||||
|
Process[] procs = Process.GetProcessesByName(procName);
|
||||||
|
int nProcs = procs.Length;
|
||||||
|
if (nProcs < 1) {
|
||||||
|
Console.WriteLine("Error: No process found for name: {0}", procName);
|
||||||
|
return -1 ;
|
||||||
|
}else{
|
||||||
|
// We'll use the first window we found
|
||||||
|
Process proc=procs[0];
|
||||||
|
if (nProcs >1) {
|
||||||
|
Console.WriteLine("{0} processes found with name: {1}",nProcs,procName);
|
||||||
|
Console.WriteLine("Using first process:");
|
||||||
|
Console.WriteLine("Process Name: {0} ID: {1} Title: {2}", proc.ProcessName, proc.Id, proc.MainWindowTitle);
|
||||||
|
}
|
||||||
|
// --- Switching to window using user32.dll function
|
||||||
|
SwitchToThisWindow(proc.MainWindowHandle);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
// --- List Windows info
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
public static int ListWindows(){
|
||||||
|
Process[] processlist = Process.GetProcesses();
|
||||||
|
Console.WriteLine("ID: \t Name:\t Title:");
|
||||||
|
Console.WriteLine("-------------------------------------------------");
|
||||||
|
foreach (Process proc in processlist)
|
||||||
|
{
|
||||||
|
if (!String.IsNullOrEmpty(proc.MainWindowTitle))
|
||||||
|
{
|
||||||
|
Console.WriteLine("{0}\t {1}\t {2}", proc.Id,proc.ProcessName, proc.MainWindowTitle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
// --- Print command usage
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
public static void print_usage(){
|
||||||
|
Console.WriteLine("");
|
||||||
|
Console.WriteLine("usage: wmctrl [options] [args]");
|
||||||
|
Console.WriteLine("");
|
||||||
|
Console.WriteLine("options:");
|
||||||
|
Console.WriteLine(" -h : show this help");
|
||||||
|
Console.WriteLine(" -l : list windows");
|
||||||
|
Console.WriteLine(" -a <PNAME> : switch to the window of the process name <PNAME>");
|
||||||
|
Console.WriteLine("");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
// --- Main Program
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
public static int Main(string[] args)
|
||||||
|
{
|
||||||
|
int status=0; // Return status for Main
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
// --- Parsing arguments
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
int nArgs=args.Length;
|
||||||
|
if (nArgs==0){
|
||||||
|
Console.WriteLine("Error: insufficient command line arguments");
|
||||||
|
print_usage();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int i=0;
|
||||||
|
while (i<nArgs) {
|
||||||
|
string s=args[i];
|
||||||
|
switch(s){
|
||||||
|
case "-h": // Help
|
||||||
|
print_usage();
|
||||||
|
i=i+1;
|
||||||
|
break;
|
||||||
|
case "-a": // Switch to Window
|
||||||
|
if (i+1<nArgs) {
|
||||||
|
status=SwitchToWindow(args[i+1]);
|
||||||
|
i=i+2;
|
||||||
|
}else{
|
||||||
|
Console.WriteLine("Error: command line option -a needs to be followed by a process name.");
|
||||||
|
status=-1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "-l": // List Windows
|
||||||
|
status=ListWindows();
|
||||||
|
i++;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Console.WriteLine("Skipped argument: "+ args[i]);
|
||||||
|
i++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (status!=0) {
|
||||||
|
// If an error occured, print usage and exit
|
||||||
|
print_usage();
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user