Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MouseProc OneShot

Status
Not open for further replies.

Saul775

Programmer
Aug 9, 2004
11
US
Hello:

I'm trying to accomplish a "One-Shot-Hook" on a WM_LBUTTONUP. When I mean one-shot, I mean I just want to hook the WM_LBUTTONUP ONCE and attain the x- and y-coordinates of the click. After the button is up on a specific window, I want the hook to be removed. I've completed an algorithm that does the job, but I'm not sure if it's the most efficient or most acceptable. Perhaps there is a better way than I've adapted. Any ideas and comments would be greatly appreciated. I've attached the DLL code as follows...

Code:
#pragma data_seg(".HKT")
HWND hWnd = NULL;
HHOOK hhk = NULL;
BOOL bCaptured = FALSE;
POINT pt = {0, 0};
#pragma data_seg()

BOOL WINAPI UnhookWindow()
{
  BOOL bResult;
  bResult = UnhookWindowsHookEx(hhk);
  return bResult;
}

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
  if ((nCode == HC_ACTION) && (wParam == WM_LBUTTONUP))
  {
	// ALWAYS UNHOOKFIRST!  WHY?
    if (!UnhookWindow())
    {
      MessageBox(NULL, "Error unhooking.", "Error", MB_ICONEXCLAMATION | MB_OK);
      bCaptured = TRUE;
      return TRUE;
    }

    MOUSEHOOKSTRUCT* mhs;
    mhs = (MOUSEHOOKSTRUCT*)lParam;
    pt = mhs->pt;
    bCaptured = TRUE;
    return TRUE;
  }
  return CallNextHookEx(hhk, nCode, wParam, lParam);
}

BOOL WINAPI OneShotHookWindow(HWND hWndToHook, POINT* passedPt)
{
  hWnd = hWndToHook;
  hhk = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseProc, GetModuleHandle("AddMultiDLL.dll"), GetWindowThreadProcessId(hWnd, NULL));
  if (!hhk)
  {
    MessageBox(NULL, "Failed to set a hook.", "Error", MB_OK);
    return 0;
  }

  while (!bCaptured)
  {
    // JUST WAIT UNTIL FIRST INSTANCE OF WM_LBUTTONUP
  }

  *passedPt = pt;
  return 1;
}

Thank you once again for your comments.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top