CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2007
    Posts
    15

    Post Mouse control in Windows

    Hi all

    I would like to handle mouse in Windows. For instance, I want to have some functions
    *** gogtoxy(x,y)
    //then mouse go to (x,y), like you use physical mouse move
    *** left_click(), right_click()
    //then mouse click
    Just plain function like this.

    Thanks so much

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Mouse control in Windows

    To move mouse cursor use SetCursorPos function.
    To emulate mouse click, use SendInput. SendInput may be used lso for cursor movement.

  3. #3
    Join Date
    Apr 2007
    Posts
    15

    Re: Mouse control in Windows

    Quote Originally Posted by Alex F View Post
    To move mouse cursor use SetCursorPos function.
    To emulate mouse click, use SendInput. SendInput may be used lso for cursor movement.
    Can you tell more, about library ang the way to implement.
    When we resize the solution of screen display, will the code change or....
    Thank you

  4. #4
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Mouse control in Windows

    You can get the screen resolution using the function GetDeviceCaps with HORZRES and VERTRES
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  5. #5
    Join Date
    Mar 2003
    Posts
    30

    Re: Mouse control in Windows

    Quote Originally Posted by Coaxecva View Post
    Can you tell more, about library ang the way to implement.
    When we resize the solution of screen display, will the code change or....
    Thank you
    You don't need any extra libraries, its functionality that comes with the windows' api.
    Just include <windows.h> and you are ready to use SetCurPos()

  6. #6
    Join Date
    Apr 2007
    Posts
    15

    Re: Mouse control in Windows

    Sorry, I'm a newbie in trying this fun learning. I found this struct of SetCurPos().
    I will try but it can not work.

    /*****************************************************/
    int SetCurPos (
    [InAttribute] IntPtr hwnd,
    [InAttribute] int x,
    [InAttribute] int y,
    [OutAttribute] array<VSIME_ERR>^ perr
    )

    /////////////////////////////////////////////////////////////////////

    HRESULT IVsIME::SetCurPos(
    [in] HWND hwnd,
    [in] int x,
    [in] int y,
    [out] VSIME_ERR * perr
    );
    /*****************************************************/

    ***I have tried :

    #include "windows.h"
    int main()
    {
    int* hwnd;
    SetCurPos(hwnd,10,10);
    return 0;
    }

    ***It raises an error:
    Error 1 error C3861: 'SetCurPos': identifier not found

    ***I dont know what hwnd is .

    Any ones have tried before, instruct me to use it, pls.
    Thank you.

  7. #7
    Join Date
    Apr 2007
    Posts
    15

    Re: Mouse control in Windows

    I have done in Mouse_move
    just SetCurPos(10,10); )
    But about double click and right click?
    And I dont understand clearly this "int* hwnd;"

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Mouse control in Windows

    Are you trying to control the mouse position within your application (i.e. the one that you are coding for) or another application?

  9. #9
    Join Date
    Apr 2007
    Posts
    15

    Re: Mouse control in Windows

    my "control mouse" means that using function control mouse event instead of physical mouse.
    I think SetCurPos(10,10); can work on all application so far.

    I have found function mouse click (right and left also).
    mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);

    But about double click? Need your help!!!

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Mouse control in Windows

    Quote Originally Posted by Coaxecva View Post
    Sorry, I'm a newbie in trying this fun learning. I found this struct of SetCurPos().
    I will try but it can not work.

    /*****************************************************/
    int SetCurPos (
    [InAttribute] IntPtr hwnd,
    [InAttribute] int x,
    [InAttribute] int y,
    [OutAttribute] array<VSIME_ERR>^ perr
    )

    /////////////////////////////////////////////////////////////////////

    HRESULT IVsIME::SetCurPos(
    [in] HWND hwnd,
    [in] int x,
    [in] int y,
    [out] VSIME_ERR * perr
    );
    /*****************************************************/

    ***I have tried :

    #include "windows.h"
    int main()
    {
    int* hwnd;
    SetCurPos(hwnd,10,10);
    return 0;
    }

    ***It raises an error:
    Error 1 error C3861: 'SetCurPos': identifier not found

    ***I dont know what hwnd is .

    Any ones have tried before, instruct me to use it, pls.
    Thank you.
    You need to build a Windows application to use the Windows API. You need to pass SetCurPos a valid window handle. All you passed it was an uninitialized int pointer.

    Time to get a good tutorial and work through the basics.

  11. #11
    Join Date
    Apr 2007
    Posts
    15

    Re: Mouse control in Windows

    Quote Originally Posted by GCDEF View Post
    You need to build a Windows application to use the Windows API. You need to pass SetCurPos a valid window handle. All you passed it was an uninitialized int pointer.

    Time to get a good tutorial and work through the basics.
    How can we do with double clicks?

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Mouse control in Windows

    Quote Originally Posted by Coaxecva View Post
    How can we do with double clicks?
    Do you know anything about writing Windows apps?

  13. #13
    Join Date
    Apr 2007
    Posts
    15

    Re: Mouse control in Windows

    Sr, I dont know.

    But I can do mouse move and mouse click.

    #include "windows.h"
    int main()
    {
    SetCurPos(10,10); /// move to 10,10

    mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); //check left_mouse down, right mouse is quite similar.
    mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); //check left mouse up, right mouse is also like this
    return 0;
    }

    I would like to know how to do double clicks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured