화면의 모든 픽셀에서 색상을 가져 오는 색상 선택 도구를 구현하려고합니다. 전 세계 마우스 훅을 사용하여 WM_MOUSEMOVE를 청취하여 마우스를 이동하고 마우스 클릭을 확인 (WM_LBUTTONDOWN)하거나 취소 (WM_RBUTTONDOWN)하여 색상을 업데이트 할 계획입니다.마우스 훅 연결 해제
나는 주위에 많은 자습서 중 하나 따랐다 내가 (프로세스가 작동하는 경우 그냥 테스트하기 위해, 콘솔 응용 프로그램에서)이 함께했다 :
static IntPtr hook;
static bool click;
static NativeMethods.LowLevelHookStruct llhs;
static void Main(string[] args)
{
hook = NativeMethods.SetWindowsHookEx(NativeMethods.WH_MOUSE_LL, MouseHookCallback, (IntPtr)null, 0);
if (hook != IntPtr.Zero)
{
Console.WriteLine("Hook Set");
while (!Console.KeyAvailable) {
Console.WriteLine("{0} {1} {2}", hook, llhs.pt.x, llhs.pt.y);
if(click) Console.WriteLine("click!");
click = false;
System.Threading.Thread.Sleep(250);
}
}
}
및
public static IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
NativeMethods.LowLevelHookStruct hookStruct = (NativeMethods.LowLevelHookStruct)Marshal.PtrToStructure(lParam, typeof(NativeMethods.LowLevelHookStruct));
if (NativeMethods.MouseMessages.WM_MOUSEMOVE == (NativeMethods.MouseMessages)wParam)
{
llhs = hookStruct;
}
if (NativeMethods.MouseMessages.WM_LBUTTONDOWN == (NativeMethods.MouseMessages)wParam)
{
click = true;
}
else if (NativeMethods.MouseMessages.WM_RBUTTONDOWN == (NativeMethods.MouseMessages)wParam)
{
}
}
return NativeMethods.CallNextHookEx(hook, nCode, wParam, lParam);
}
을 NativeMethods는 단순히 모든 DllImport 관련 항목을 유지하는 클래스입니다. 내가 콘솔 응용 프로그램을 실행하면
는, 마우스 커서가 몇 초에 대한 걸리면, 콘솔 내가이 얻을 - 커서 는 동안에도이
Hook Set
3945554872 0 0
3945554872 0 0
3945554872 0 0
3945554872 0 0
...
디버그에에가는 붙어 , 그것은 내 훅이 결코 한 번도 호출되지 않는다. 잘못된 정보 일 수 있습니까?
질문 제목에서 태그를 제거했습니다. 왜 [여기] (http://stackoverflow.com/help/tagging)를 읽어 보시기 바랍니다. – dymanoid
아, 잊어 버렸어. 감사합니다 :) – SirePi
WH_MOUSE_LL 후크는 Windows가 안전하게 후크 콜백을 호출 할 수 있도록 메시지 루프가 필요합니다. .NET 프로그램에서 Application.Run(). 가장 쉽게 Winforms 또는 WPF 프로젝트 또는이 작업을 수행하는 많은 라이브러리 중 하나에서 가져 왔습니다. –