最簡(jiǎn)單的加速運(yùn)動(dòng)示例,直接拷貝代碼,即可用方向鍵控制控件移動(dòng)
商城ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
Public?Class?最簡(jiǎn)單的加速運(yùn)動(dòng)
Dim?左右,?上下?As?Integer
Dim?X,?Y,?VX,?VY,?VA?As?Double
Dim?WithEvents?時(shí)鐘?As?New?Timer
Dim?WithEvents?移動(dòng)控件?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
移動(dòng)控件.BackColor?=?Color.MediumPurple
移動(dòng)控件.Size?=?New?Size(60,?60)
Controls.Add(移動(dòng)控件)
時(shí)鐘.Interval?=?25
時(shí)鐘.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?時(shí)鐘_Tick(sender?As?Object,?e?As?EventArgs)?Handles?時(shí)鐘.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?-?移動(dòng)控件.Width?Then?X?=?Width?-?移動(dòng)控件.Width?-?2?:?VX?=?-VX
If?Y??Height?-?移動(dòng)控件.Height?Then?Y?=?Height?-?移動(dòng)控件.Height?-?2?:?VY?=?-VY
移動(dòng)控件.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("無法執(zhí)行文件 " ProcessPath, "錯(cuò)誤")
End Try
End Sub
'監(jiān)視程序就可以了,若果監(jiān)視別的窗體的話,用SPY++ 找到句柄,配合FindWindowEx,SendMessage根據(jù)其屬性做
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ShellAndWait("Notepad.exe")
MessageBox.Show("筆記本被關(guān)閉后我才會(huì)出現(xiàn)")
End Sub
End Class
窗體內(nèi)檢測(cè)按鍵
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True '注冊(cè)窗體的鍵盤事件
End Sub
在TextBox1中顯示按鍵代碼
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
TextBox1.Text = Str(e.KeyCode) + vbCrLf
End Sub
窗體沒有焦點(diǎn),用窗體監(jiān)測(cè)鍵盤輸入緩沖區(qū)可以解決,但這似乎可以用于監(jiān)測(cè)用戶輸入內(nèi)容,不要用于非法目的呀!所以就是知道也不會(huì)告訴你的。
參考方法如下,具體解釋已經(jīng)注解在代碼中;
/定義變量
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
static int hKeyboardHook = 0;
HookProc KeyboardHookProcedure;
/*************************
* 聲明API函數(shù)
* ***********************/
// 安裝鉤子 (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);
// 繼續(xù)下一個(gè)鉤子
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingC.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
// 取得當(dāng)前線程編號(hào)(線程鉤子需要用到)
[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記錄下來,搞個(gè)郵件發(fā)送程序發(fā)到自己的郵箱去)
return 0;//如果返回1,則結(jié)束消息,這個(gè)消息到此為止,不再傳遞。如果返回0或調(diào)用CallNextHookEx函數(shù)則消息出了這個(gè)鉤子繼續(xù)往下傳遞,也就是傳給消息真正的接受者
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
//鍵盤結(jié)構(gòu)
public struct KeyMSG
{
public int vkCode; //鍵值
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
// 安裝鉤子
public void HookStart()
{
if(hKeyboardHook == 0)
{
// 創(chuàng)建HookProc實(shí)例
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
// 設(shè)置線程鉤子
hKeyboardHook = SetWindowsHookEx( 13,KeyboardHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
//************************************
//鍵盤線程鉤子
//SetWindowsHookEx( 2,KeyboardHookProcedure, IntPtr.Zero, GetCurrentThreadId()); //GetCurrentThreadId()為要監(jiān)視的線程ID,你完全可以自己寫個(gè)方法獲取QQ的線程哦
//鍵盤全局鉤子,需要引用空間(using System.Reflection;)
//SetWindowsHookEx( 13,KeyboardHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
//
//關(guān)于SetWindowsHookEx (int idHook, HookProc lpfn, IntPtr hInstance, int threadId)函數(shù)將鉤子加入到鉤子鏈表中,說明一下四個(gè)參數(shù):
//idHook 鉤子類型,即確定鉤子監(jiān)聽何種消息,上面的代碼中設(shè)為2,即監(jiān)聽鍵盤消息并且是線程鉤子,如果是全局鉤子監(jiān)聽鍵盤消息應(yīng)設(shè)為13,
//線程鉤子監(jiān)聽鼠標(biāo)消息設(shè)為7,全局鉤子監(jiān)聽鼠標(biāo)消息設(shè)為14。
//
//lpfn 鉤子子程的地址指針。如果dwThreadId參數(shù)為0 或是一個(gè)由別的進(jìn)程創(chuàng)建的線程的標(biāo)識(shí),lpfn必須指向DLL中的鉤子子程。 除此以外,lpfn可
//以指向當(dāng)前進(jìn)程的一段鉤子子程代碼。鉤子函數(shù)的入口地址,當(dāng)鉤子鉤到任何消息后便調(diào)用這個(gè)函數(shù)。
//
//hInstance應(yīng)用程序?qū)嵗木浔?。?biāo)識(shí)包含lpfn所指的子程的DLL。如果threadId 標(biāo)識(shí)當(dāng)前進(jìn)程創(chuàng)建的一個(gè)線程,而且子程代碼位于當(dāng)前
//進(jìn)程,hInstance必須為NULL??梢院芎?jiǎn)單的設(shè)定其為本應(yīng)用程序的實(shí)例句柄。
//
//threadedId 與安裝的鉤子子程相關(guān)聯(lián)的線程的標(biāo)識(shí)符。如果為0,鉤子子程與所有的線程關(guān)聯(lián),即為全局鉤子。
//************************************
// 如果設(shè)置鉤子失敗
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.");
}