标题: 关于调用API函数 [打印本页] 作者: 心ゝ疼ㄚòひ 时间: 2012-4-13 20:08 标题: 关于调用API函数 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Diagnostics;
namespace LockScreen2
{
public partial class frmLockScreen : Form
{
public frmLockScreen()
{
InitializeComponent();
}
public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
private string filePath = Application.StartupPath + @"\config.ini";
private string pwd;
private int i = 0;
public string IniReadValue(string Section, string Key, string FilePath)
{
StringBuilder temp = new StringBuilder(255);
int i = LockScreen.Win32API.GetPrivateProfileString(Section, Key, "", temp, 255, FilePath);
return temp.ToString();
}
#region
HookProc KeyBoardProcedure;
static int hHook = 0;
public const int WH_KEYBOARD = 13;
public struct KeyBoardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
public void HookStart()
{
if (hHook == 0)
{
KeyBoardProcedure = new HookProc(this.KeyBoardHookProc );
hHook = LockScreen.Win32API.SetWindowsHookEx(WH_KEYBOARD, KeyBoardProcedure, LockScreen.Win32API.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
//hHook = LockScreen.Win32API.SetWindowsHookEx(2, KeyboardHookProcedure, IntPtr.Zero,
// GetCurrentThreadId());
if (hHook == 0)
{
HookClear();
}
}
}
public void HookClear()
{
bool rsetKeyboard = true;
if (hHook != 0)
{
rsetKeyboard = LockScreen.Win32API.UnHookWindowsHookEx(hHook);
hHook = 0;
}
if (!rsetKeyboard)
{
throw new Exception("取消钩子失败!");
}
}
public static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode >= 0)
{
KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
if (kbh.vkCode == 91)
{
return 1;
}
if (kbh.vkCode == 92)
{
return 1;
}
if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control)
{
return 1;
}
//if()
}
return LockScreen.Win32API.CallNextHookEx(hHook, nCode, wParam, lParam);
}
#endregion