最简单的加速运动示例,直接拷贝代码,即可用方向键控制控件移动
商城ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
Public Class 最简单的加速运动
Dim 左右, 上下 As Integer
Dim X, Y, VX, VY, VA As Double
Dim WithEvents 时钟 As New Timer
Dim WithEvents 移动控件 As New Label
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
VA = 0.5
X = 300.0
Y = 300.0
移动控件.BackColor = Color.MediumPurple
移动控件.Size = New Size(60, 60)
Controls.Add(移动控件)
时钟.Interval = 25
时钟.Enabled = True
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then 上下 = -1
If e.KeyCode = Keys.Down Then 上下 = 1
If e.KeyCode = Keys.Left Then 左右 = -1
If e.KeyCode = Keys.Right Then 左右 = 1
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.Up Then 上下 = 0
If e.KeyCode = Keys.Down Then 上下 = 0
If e.KeyCode = Keys.Left Then 左右 = 0
If e.KeyCode = Keys.Right Then 左右 = 0
End Sub
Private Sub 时钟_Tick(sender As Object, e As EventArgs) Handles 时钟.Tick
VX = Math.Min(VX + 左右 * VA, 10)
VY = Math.Min(VY + 上下 * VA, 10)
X += VX
Y += VY
If X 0 Then X = 2 : VX = -VX
If Y 0 Then Y = 2 : VY = -VY
If X Width - 移动控件.Width Then X = Width - 移动控件.Width - 2 : VX = -VX
If Y Height - 移动控件.Height Then Y = Height - 移动控件.Height - 2 : VY = -VY
移动控件.Location = New Point(X, Y)
End Sub
End Class
以记事本为例
Public Class Form1
Public Sub ShellAndWait(ByVal ProcessPath As String)
Dim objProcess As System.Diagnostics.Process
Try
objProcess = New System.Diagnostics.Process()
objProcess.StartInfo.FileName = ProcessPath
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
objProcess.Start()
objProcess.WaitForExit()
objProcess.Close()
Catch
MessageBox.Show("无法执行文件 " ProcessPath, "错误")
End Try
End Sub
'监视程序就可以了,若果监视别的窗体的话,用SPY++ 找到句柄,配合FindWindowEx,SendMessage根据其属性做
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ShellAndWait("Notepad.exe")
MessageBox.Show("笔记本被关闭后我才会出现")
End Sub
End Class
窗体内检测按键
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True '注册窗体的键盘事件
End Sub
在TextBox1中显示按键代码
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
TextBox1.Text = Str(e.KeyCode) + vbCrLf
End Sub
窗体没有焦点,用窗体监测键盘输入缓冲区可以解决,但这似乎可以用于监测用户输入内容,不要用于非法目的呀!所以就是知道也不会告诉你的。
参考方法如下,具体解释已经注解在代码中;
/定义变量
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
static int hKeyboardHook = 0;
HookProc KeyboardHookProcedure;
/*************************
* 声明API函数
* ***********************/
// 安装钩子 (using System.Runtime.InteropServices;)
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingC.StdCall)]
public static extern int SetWindowsHookEx(int idHook,HookProc lpfn, IntPtr hInstance, int threadId);
// 卸载钩子
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingC.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
// 继续下一个钩子
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingC.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
// 取得当前线程编号(线程钩子需要用到)
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();
//钩子子程:就是钩子所要做的事情
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
if (nCode = 0)
{
/****************
//线程键盘钩子判断是否按下键
Keys keyData = (Keys)wParam;
if(lParam.ToInt32() 0)
{
// 键盘按下
}
if(lParam.ToInt32() 0)
{
// 键盘抬起
}
****************/
/****************
//全局键盘钩子判断是否按下键
wParam = = 0x100 // 键盘按下
wParam = = 0x101 // 键盘抬起
****************/
KeyMSG m = (KeyMSG) Marshal.PtrToStructure(lParam, typeof(KeyMSG));//键盘
// 在这里添加你想要做是事情(比如把键盘nCode记录下来,搞个邮件发送程序发到自己的邮箱去)
return 0;//如果返回1,则结束消息,这个消息到此为止,不再传递。如果返回0或调用CallNextHookEx函数则消息出了这个钩子继续往下传递,也就是传给消息真正的接受者
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
//键盘结构
public struct KeyMSG
{
public int vkCode; //键值
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
// 安装钩子
public void HookStart()
{
if(hKeyboardHook == 0)
{
// 创建HookProc实例
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
// 设置线程钩子
hKeyboardHook = SetWindowsHookEx( 13,KeyboardHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
//************************************
//键盘线程钩子
//SetWindowsHookEx( 2,KeyboardHookProcedure, IntPtr.Zero, GetCurrentThreadId()); //GetCurrentThreadId()为要监视的线程ID,你完全可以自己写个方法获取QQ的线程哦
//键盘全局钩子,需要引用空间(using System.Reflection;)
//SetWindowsHookEx( 13,KeyboardHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
//
//关于SetWindowsHookEx (int idHook, HookProc lpfn, IntPtr hInstance, int threadId)函数将钩子加入到钩子链表中,说明一下四个参数:
//idHook 钩子类型,即确定钩子监听何种消息,上面的代码中设为2,即监听键盘消息并且是线程钩子,如果是全局钩子监听键盘消息应设为13,
//线程钩子监听鼠标消息设为7,全局钩子监听鼠标消息设为14。
//
//lpfn 钩子子程的地址指针。如果dwThreadId参数为0 或是一个由别的进程创建的线程的标识,lpfn必须指向DLL中的钩子子程。 除此以外,lpfn可
//以指向当前进程的一段钩子子程代码。钩子函数的入口地址,当钩子钩到任何消息后便调用这个函数。
//
//hInstance应用程序实例的句柄。标识包含lpfn所指的子程的DLL。如果threadId 标识当前进程创建的一个线程,而且子程代码位于当前
//进程,hInstance必须为NULL。可以很简单的设定其为本应用程序的实例句柄。
//
//threadedId 与安装的钩子子程相关联的线程的标识符。如果为0,钩子子程与所有的线程关联,即为全局钩子。
//************************************
// 如果设置钩子失败
if(hKeyboardHook == 0 )
{
HookStop();
throw new Exception("SetWindowsHookEx failed.");
}
}
}
// 卸载钩子
public void HookStop()
{
bool retKeyboard = true;
if(hKeyboardHook != 0)
{
retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
hKeyboardHook = 0;
}
if (!( retKeyboard))
throw new Exception("UnhookWindowsHookEx failed.");
}