有点标题党的感觉~~这东西充其量只能说是按键辅助工具,和大名鼎鼎的按键精灵比起来实在是小巫见大巫了。
前两天放假无聊,天气太冷不想出门,玩起了很多年前玩过的一款游戏:骑士。太久没玩,手指灵活度不够,玩的是一塌糊涂。于是想起了大名鼎鼎的按键精灵,下载,写脚本,再上游戏,解放了双手的感觉就是不一样,轻松+惬意。今天是新年上班第一天,比较有空,想起来这件事,秉承重复造轮子的指导方针,经过一下午的google,大概功能已经写好,能够满足骑士中战士这个职业最基本的需求。以下是代码:
{
// Fields
private static int hHook;
private HookProc KeyBoardHookProcedure;
static Dictionary<string, Thread> dict = new Dictionary<string, Thread>();
// Methods
static hook() { }
public hook() { }
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("USER32.DLL")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
public void Hook_Clear()
{
bool flag = true;
if (hHook != 0)
{
flag = UnhookWindowsHookEx(hHook);
hHook = 0;
}
if (!flag)
{
throw new Exception("取消hook失败!");
}
}
public void Hook_Start()
{
if (hHook == 0)
{
this.KeyBoardHookProcedure = new HookProc(hook.KeyBoardHookProc);
hHook = SetWindowsHookEx(13, this.KeyBoardHookProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
if (hHook == 0)
{
MessageBox.Show("设置Hook失败!");
this.Hook_Clear();
}
}
}
[DllImport("User32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public static void 战士抖杀()
{
while (true)
{
byte num1 = (byte)Keys.E;
keybd_event(num1, 0, 0, 0);
Thread.Sleep(100);
keybd_event(num1, 0, 2, 0);
Thread.Sleep(100);
keybd_event(num1, 0, 0, 0);
Thread.Sleep(100);
keybd_event(num1, 0, 2, 0);
Thread.Sleep(100);
byte num2 = (byte)Keys.D4;
keybd_event(num2, 0, 0, 0);
Thread.Sleep(100);
keybd_event(num2, 0, 2, 0);
Thread.Sleep(100);
keybd_event(num1, 0, 0, 0);
Thread.Sleep(100);
keybd_event(num1, 0, 2, 0);
Thread.Sleep(100);
keybd_event(num1, 0, 0, 0);
Thread.Sleep(100);
keybd_event(num1, 0, 2, 0);
Thread.Sleep(100);
byte num4 = (byte)Keys.R;
keybd_event(num4, 0, 0, 0);
Thread.Sleep(100);
keybd_event(num4, 0, 2, 0);
Thread.Sleep(100);
}
}
public static int KeyBoardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
KeyBoardHookStruct input = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
if (input.vkCode == (int)Keys.F11)
{
IntPtr ptr = GetForegroundWindow();
if (ptr != IntPtr.Zero)
{
if (dict.Count < 1)
{
ThreadStart threadStart = new ThreadStart(战士抖杀);
Thread thread = new Thread(threadStart);
thread.Start();
dict.Clear();
dict.Add("战士抖杀", thread);
}
}
return 1;
}
else if (input.vkCode == (int)Keys.F12)
{
foreach (var th in dict)
{
Thread thread = th.Value;
if (thread != null && thread.IsAlive)
{
if (!thread.Join(3))
{
thread.Abort();
}
}
}
dict.Clear();
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern bool UnhookWindowsHookEx(int idHook);
// Nested Types
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public class KeyBoardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
public KeyBoardHookStruct() { }
}
}
本文链接
总是忘记语法,复制下来记录一下。
<TextBox Grid.Column="1" Text="{Binding Path=(ListBox.SelectedValue).(DoMain:GroupModel.RowID),ElementName=mylistbox}"
IsReadOnly="True" Width="100" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<ListBox x:Name="mylistbox" Grid.Row="2" Grid.ColumnSpan="2" SelectionMode="Single" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}">
<ListBox.Template>
<ControlTemplate>
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" >
<WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="ListBoxItem.PreviewMouseDoubleClick"
Handler="ListBoxItemDoubleClick" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type DoMain:GroupModel}">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Image Source="{Binding Ico,Mode=OneWay,Converter={StaticResource imageConverter}}"/>
<Label Grid.Row="1" Content="{Binding GroupName}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
本文链接
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace study
{
public class Program
{
/// <summary>
/// ref关键字可以让一个值类型的输入按引用传递
/// </summary>
private static void refFunction(int x,ref int a)
{
a = a * x;
}
/// <summary>
/// out关键字的使用
/// 使用out关键字,输入参数str在传递给outKeyWord()方法前不必进行初始化,如果没有关键字,是无法编译的
/// </summary>
private static void outKeyWord(out string str)
{
//输出参数str在方法返回前必须被赋值
str = "a string";
}
static void Main(string[] args)
{
//***********ref关键字的使用
int x = 12, a = 5;
Console.WriteLine(a);//输出:5
refFunction(x, ref a);
Console.WriteLine(a);//输出:60
//***********out关键字的使用
string str;//不必初始化
outKeyWord(out str);//输出:a string
Console.WriteLine(str);
Console.ReadKey();
}
}
}
原创文章,转载请注明出处:http://www.cnblogs.com/hongfei/archive/2013/01/04/2844624.html
本文链接