Initial commit
This commit is contained in:
6
DD2Switcher/App.config
Normal file
6
DD2Switcher/App.config
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
|
||||
</startup>
|
||||
</configuration>
|
82
DD2Switcher/DD2Switcher.csproj
Normal file
82
DD2Switcher/DD2Switcher.csproj
Normal file
@@ -0,0 +1,82 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{2AC26899-8E27-4B96-85A9-C387186EAD27}</ProjectGuid>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<RootNamespace>DD2Switcher</RootNamespace>
|
||||
<AssemblyName>DD2Switcher</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<Deterministic>true</Deterministic>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Deployment" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Form1.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Form1.Designer.cs">
|
||||
<DependentUpon>Form1.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="HotKeyManager.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<EmbeddedResource Include="Properties\Resources.resx">
|
||||
<Generator>ResXFileCodeGenerator</Generator>
|
||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||
<SubType>Designer</SubType>
|
||||
</EmbeddedResource>
|
||||
<Compile Include="Properties\Resources.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
</Compile>
|
||||
<None Include="Properties\Settings.settings">
|
||||
<Generator>SettingsSingleFileGenerator</Generator>
|
||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
||||
</None>
|
||||
<Compile Include="Properties\Settings.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Settings.settings</DependentUpon>
|
||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
40
DD2Switcher/Form1.Designer.cs
generated
Normal file
40
DD2Switcher/Form1.Designer.cs
generated
Normal file
@@ -0,0 +1,40 @@
|
||||
namespace DD2Switcher
|
||||
{
|
||||
partial class Form1
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.components = new System.ComponentModel.Container();
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(800, 450);
|
||||
this.Text = "Form1";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
12
DD2Switcher/Form1.cs
Normal file
12
DD2Switcher/Form1.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace DD2Switcher
|
||||
{
|
||||
public partial class Form1 : Form
|
||||
{
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
106
DD2Switcher/HotKeyManager.cs
Normal file
106
DD2Switcher/HotKeyManager.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace DD2Switcher {
|
||||
public static class HotKeyManager {
|
||||
private static volatile MessageWindow _wnd;
|
||||
private static volatile IntPtr _hwnd;
|
||||
private static readonly ManualResetEvent _windowReadyEvent = new ManualResetEvent(false);
|
||||
|
||||
private static int _id;
|
||||
|
||||
static HotKeyManager() {
|
||||
var messageLoop = new Thread(delegate() { Application.Run(new MessageWindow()); });
|
||||
messageLoop.Name = "MessageLoopThread";
|
||||
messageLoop.IsBackground = true;
|
||||
messageLoop.Start();
|
||||
}
|
||||
|
||||
public static event EventHandler<HotKeyEventArgs> HotKeyPressed;
|
||||
|
||||
public static int RegisterHotKey(Keys key, KeyModifiers modifiers) {
|
||||
_windowReadyEvent.WaitOne();
|
||||
var id = Interlocked.Increment(ref _id);
|
||||
_wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key);
|
||||
return id;
|
||||
}
|
||||
|
||||
public static void UnregisterHotKey(int id) {
|
||||
_wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id);
|
||||
}
|
||||
|
||||
private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key) {
|
||||
RegisterHotKey(hwnd, id, modifiers, key);
|
||||
}
|
||||
|
||||
private static void UnRegisterHotKeyInternal(IntPtr hwnd, int id) {
|
||||
UnregisterHotKey(_hwnd, id);
|
||||
}
|
||||
|
||||
private static void OnHotKeyPressed(HotKeyEventArgs e) {
|
||||
if (HotKeyPressed != null) HotKeyPressed(null, e);
|
||||
}
|
||||
|
||||
[DllImport("user32", SetLastError = true)]
|
||||
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
|
||||
|
||||
[DllImport("user32", SetLastError = true)]
|
||||
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
|
||||
|
||||
private delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key);
|
||||
|
||||
private delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id);
|
||||
|
||||
private class MessageWindow : Form {
|
||||
private const int WM_HOTKEY = 0x312;
|
||||
|
||||
public MessageWindow() {
|
||||
_wnd = this;
|
||||
_hwnd = Handle;
|
||||
_windowReadyEvent.Set();
|
||||
}
|
||||
|
||||
protected override void WndProc(ref Message m) {
|
||||
if (m.Msg == WM_HOTKEY) {
|
||||
var e = new HotKeyEventArgs(m.LParam);
|
||||
OnHotKeyPressed(e);
|
||||
}
|
||||
|
||||
base.WndProc(ref m);
|
||||
}
|
||||
|
||||
protected override void SetVisibleCore(bool value) {
|
||||
// Ensure the window never becomes visible
|
||||
base.SetVisibleCore(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class HotKeyEventArgs : EventArgs {
|
||||
public readonly Keys Key;
|
||||
public readonly KeyModifiers Modifiers;
|
||||
|
||||
public HotKeyEventArgs(Keys key, KeyModifiers modifiers) {
|
||||
Key = key;
|
||||
Modifiers = modifiers;
|
||||
}
|
||||
|
||||
public HotKeyEventArgs(IntPtr hotKeyParam) {
|
||||
var param = (uint)hotKeyParam.ToInt64();
|
||||
Key = (Keys)((param & 0xffff0000) >> 16);
|
||||
Modifiers = (KeyModifiers)(param & 0x0000ffff);
|
||||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum KeyModifiers {
|
||||
Alt = 1,
|
||||
Control = 2,
|
||||
Shift = 4,
|
||||
Windows = 8,
|
||||
NoRepeat = 0x4000
|
||||
}
|
||||
}
|
130
DD2Switcher/Program.cs
Normal file
130
DD2Switcher/Program.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Threading;
|
||||
|
||||
namespace DD2Switcher {
|
||||
internal static class Program {
|
||||
[DllImport("user32.dll")]
|
||||
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
public static extern IntPtr GetForegroundWindow();
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
public static extern bool SetForegroundWindow(IntPtr hWnd);
|
||||
|
||||
[DllImport("User32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
|
||||
|
||||
static Process[] games = Process.GetProcessesByName("Dundefgame");
|
||||
static Process activeGame = games[0];
|
||||
static int defaultAffinity = 0b100000000000;
|
||||
|
||||
private static void AdjustAffinities() {
|
||||
int fullAffinity = 0b111111111111;
|
||||
int i = 0;
|
||||
foreach (Process game in games) {
|
||||
if (game != activeGame) {
|
||||
var processAffinty = defaultAffinity >> i;
|
||||
fullAffinity = fullAffinity & ~processAffinty;
|
||||
game.ProcessorAffinity = new IntPtr(processAffinty);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
activeGame.ProcessorAffinity = new IntPtr(fullAffinity);
|
||||
}
|
||||
|
||||
private static void AdjustPriorities() {
|
||||
foreach (Process game in games) {
|
||||
game.PriorityClass = ProcessPriorityClass.Idle;
|
||||
}
|
||||
activeGame.PriorityClass = ProcessPriorityClass.High;
|
||||
}
|
||||
|
||||
private static void NerfAll() {
|
||||
int i = 0;
|
||||
foreach (Process game in games) {
|
||||
game.ProcessorAffinity = new IntPtr(defaultAffinity >> i);
|
||||
game.PriorityClass = ProcessPriorityClass.Idle;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private static void SwitchToGame(int index) {
|
||||
SetForegroundWindow(games[index].MainWindowHandle);
|
||||
activeGame = games[index];
|
||||
AdjustAffinities();
|
||||
AdjustPriorities();
|
||||
}
|
||||
|
||||
private static void SwitchMainGame() {
|
||||
IntPtr foregroundWindow = GetForegroundWindow();
|
||||
Process foregroundGame = null;
|
||||
int foregroundGameIndex = -1;
|
||||
bool exists = false;
|
||||
|
||||
foreach (Process game in games) {
|
||||
if (foregroundWindow == game.MainWindowHandle) {
|
||||
exists = true;
|
||||
foregroundGame = game;
|
||||
foregroundGameIndex = Array.IndexOf(games, game);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (exists) {
|
||||
Process tempGame = games[0];
|
||||
games[0] = foregroundGame;
|
||||
games[foregroundGameIndex] = tempGame;
|
||||
}
|
||||
}
|
||||
|
||||
[STAThread]
|
||||
private static void Main() {
|
||||
HotKeyManager.RegisterHotKey(Keys.D1, KeyModifiers.Alt);
|
||||
HotKeyManager.RegisterHotKey(Keys.D2, KeyModifiers.Alt);
|
||||
HotKeyManager.RegisterHotKey(Keys.D3, KeyModifiers.Alt);
|
||||
HotKeyManager.RegisterHotKey(Keys.D4, KeyModifiers.Alt);
|
||||
HotKeyManager.RegisterHotKey(Keys.D5, KeyModifiers.Alt);
|
||||
HotKeyManager.RegisterHotKey(Keys.Q, KeyModifiers.Alt);
|
||||
HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed;
|
||||
|
||||
static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) {
|
||||
switch (e.Key) {
|
||||
case Keys.D1:
|
||||
SwitchToGame(0);
|
||||
break;
|
||||
case Keys.D2:
|
||||
SwitchToGame(1);
|
||||
break;
|
||||
case Keys.D3:
|
||||
SwitchToGame(2);
|
||||
break;
|
||||
case Keys.D4:
|
||||
SwitchToGame(3);
|
||||
break;
|
||||
case Keys.D5:
|
||||
SwitchMainGame();
|
||||
break;
|
||||
case Keys.Q:
|
||||
NerfAll();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (true) {
|
||||
Thread.Sleep(2000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user