玩网页游戏挂机,又不想让别人看见你电脑的运行。所以写了个后台运行的程序。
先谁HotKey注册类,用来调用com组件来显示与隐藏窗体:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ntsvs
{
static class Hotkey
{
//系统api#region 系统api
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool RegisterHotKey(IntPtr hWnd, int id, HotkeyModifiers fsModifiers,Keys vk);
[DllImport("user32.dll")]
static extern bool UnregisterHotKey(IntPtr hWnd, int id);
/** <summary>
/// 注册快捷键
/// </summary>
/// <param name="hWnd">持有快捷键窗口的句柄</param>
/// <param name="fsModifiers">组合键</param>
/// <param name="vk">快捷键的虚拟键码</param>
/// <param name="callBack">回调函数</param>
public static void Regist(IntPtr hWnd, HotkeyModifiers fsModifiers, Keys vk, HotKeyCallBackHanlder callBack)
{
int id = keyid++;
if (!RegisterHotKey(hWnd, id, fsModifiers, vk))
throw new Exception("regist hotkey fail.");
keymap[id] = callBack;
}
/** <summary>
/// 注销快捷键
/// </summary>
/// <param name="hWnd">持有快捷键窗口的句柄</param>
/// <param name="callBack">回调函数</param>
public static void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack)
{
foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap)
{
if (var.Value == callBack)
UnregisterHotKey(hWnd, var.Key);
}
}
/** <summary>
/// 快捷键消息处理
/// </summary>
public static void ProcessHotKey(System.Windows.Forms.Message m)
{
if (m.Msg == WM_HOTKEY)
{
int id = m.WParam.ToInt32();
HotKeyCallBackHanlder callback;
if (keymap.TryGetValue(id, out callback))
{
callback();
}
}
}
const int WM_HOTKEY = 0x312;
static int keyid = 10;
static Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>();
public delegate void HotKeyCallBackHanlder();
}
enum HotkeyModifiers
{
MOD_ALT = 0x1,
MOD_CONTROL = 0x2,
MOD_SHIFT = 0x4,
MOD_WIN = 0x8
}
}
然后是主窗体formMain formmain里添加了一个webBrowiser控件,一个textbox控件,一个button控件。在webBrowiser里可以登录到页游的网址,按快捷键就可以隐藏网页了。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ntsvs
{
public partial class Form1 : Form
{
bool isVisible = false;
public Form1()
{
InitializeComponent();
//注册热键Shift+S,Id号为100。HotKey.KeyModifiers.Shift也可以直接使用数字4来表示。
Hotkey.Regist(this.Handle, HotkeyModifiers.MOD_ALT, Keys.P, Test3);//最大化窗口
Hotkey.Regist(this.Handle, HotkeyModifiers.MOD_ALT, Keys.B, Test2);//最小化
Hotkey.Regist(this.Handle, HotkeyModifiers.MOD_ALT, Keys.L, Test1);//还原
Hotkey.Regist(this.Handle, HotkeyModifiers.MOD_ALT, Keys.A, Test0);//隐藏
Test0();
}
//0-隐藏窗口
void Test0()
{
this.Activate();
Class1.ShowWindowAsync(this.Handle, 0);
}
//1-还原窗口
void Test1()
{
this.Activate();
Class1.ShowWindowAsync(this.Handle, 1);
}
//2-最小化窗口,
void Test2()
{
this.Activate();
Class1.ShowWindowAsync(this.Handle, 2);
}
//3-最大化窗口,
void Test3()
{
this.Activate();
Class1.ShowWindowAsync(this.Handle, 3);
}
private void Form1_Shown(object sender, EventArgs e)
{
this.Visible = isVisible;
}
///
/// 监视Windows消息
/// 重载WndProc方法,用于实现热键响应
///
///
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
Hotkey.ProcessHotKey(m);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
this.webBrowser1.Url = new Uri(textBox1.Text);
this.webBrowser1.Update();
}
catch { }
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
Hotkey.UnRegist(this.Handle, Test3);
Hotkey.UnRegist(this.Handle, Test2);
Hotkey.UnRegist(this.Handle, Test1);
Hotkey.UnRegist(this.Handle, Test0);
webBrowser1.Stop();
webBrowser1.Dispose();
Environment.Exit(0);
}
catch { }
}
}
}
感谢每一位阅读此篇文章的人,希望可以帮到你。