This was a POC to find out if I can do this prior to wrapping it into the main codestream. I didn't realise that about the thread ID - however, I understood that setting it to 0 is meant to track all threads - but doesn't...?? How would I identify the threadID of the excel windows that are open? Thanks for the tips - much appreciated!! Cheers OJ
Rob Kennedy <[EMAIL PROTECTED]> wrote: Owen Gardner wrote: > WOW this one seems to be a toughee... > > I am trying to detect the mouse movement over an excel window using > hooks, Gee, I can't *imagine* why *that* would tough! OK, what's your goal? > and I can't see where I'm going wrong: > > step 1: little DLL written that passes a handle into a method > StartHook, shown below...: > > function StartHook(aHWnd : HWnd):string;stdcall; > begin > oLabel := aLabel; > aMouseHook := SetWindowsHookEx(WH_MOUSE, aMouseHookProc, > HInstance, bHWnd); > result := 'Mouse Hook started'; > end; I don't see you checking whether the hook is really tied in. You assign a value to aMouseHook, but you never check its value. API functions *can* fail, and you need to check for that possibility in your code. Also, I'm wary about you returning a string in your StartHook function. Also that it seems to use six global variables, and it doesn't use its parameter. Note that the fourth parameter to SetWindowsHookEx is a thread ID, not a window handle. They're not the same things. > step 2: test app whips through the existing windows using the below > code, and when it finds our excel spreadsheet calls the starthook > method: > > procedure TForm2.Button1Click(Sender: TObject); > var > NextHandle: Hwnd; > NextTitle: array[0..260] of char; > nextClassname : array[0..260] of char; > begin > NextHandle := GetWindow(Application.Handle, GW_HWNDFIRST); > while (NextHandle > 0) do > begin > GetWindowText(NextHandle, NextTitle, 255); > if copy(nextTitle,1,15) = 'Microsoft Excel' then > begin > messagedlg(StartHook(label1, NextHandle),mtInformation, > [mbOK],0); > break; > end; > NextHandle := GetWindow(NextHandle, GW_HWNDNEXT); > end; > end; MSDN explicitly discourages using GetWindow to iterate through the system's windows. It tell you to use EnumWindows instead. Instead of making lots of temporary strings, use the StrLComp function to compare a portion of the array to your target string: const Target = 'Microsoft Excel'; if StrLComp(NextTitle, Target, Length(Target)) = 0 then begin // strings match end; -- Rob ----------------------------------------------------- Home page: http://groups.yahoo.com/group/delphi-en/ To unsubscribe: [EMAIL PROTECTED] Yahoo! Groups Links --------------------------------- Yahoo! Model Search - Could you be the next catwalk superstar? Check out the competition now [Non-text portions of this message have been removed] ------------------------ Yahoo! Groups Sponsor --------------------~--> Fair play? Video games influencing politics. Click and talk back! http://us.click.yahoo.com/T8sf5C/tzNLAA/TtwFAA/i7folB/TM --------------------------------------------------------------------~-> ----------------------------------------------------- Home page: http://groups.yahoo.com/group/delphi-en/ To unsubscribe: [EMAIL PROTECTED] Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/delphi-en/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/

