Right button on desktop/other programs - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Right button on desktop/other programs

walhartwalhart Posts: 3Member
Greetings.

I'm making a program that needs to simulate the mouse right button click on the desktop or another program, I tried to use the onmousedown passing the standard parameters and it didn't worked outside the main program, the event is being called, I tested it with a timer and a message, but it don't work on the desktop, do I need to set some special parameters or use some kind of API?

Thanks

Thiago

Comments

  • Srki_KiSrki_Ki Posts: 26Member
    You must find window handle to witch you want to send WM_RBUTTONDOWN message. Use FindWindow, EnumWindows or some other method to get window handles you want and then send message to that window

    [code]
    PostMessage(W, WM_RBUTTONDOWN, wParam, lParam);[/code]

    W is window hadle to witch you are sending a message.

    WM_RBUTTONDOWN is message that is sent. (if you want you can send any other message)

    wParam for WM_RBUTTONDOWN message tels witch control key were pressed when the right mouse button was pressed
    MK_CONTROL Set if the CTRL key is down.
    MK_LBUTTON Set if the left mouse button is down.
    MK_MBUTTON Set if the middle mouse button is down.
    MK_RBUTTON Set if the right mouse button is down.
    MK_SHIFT Set if the SHIFT key is down.

    lParam for WM_RBUTTONDOWN message contains X and Y position of cursor when the right mouse button was pressed
    xPos = LOWORD(lParam)
    yPos = HIWORD(lParam)
  • walhartwalhart Posts: 3Member
    I've done it, but it didn't worked, I used the GetWindow function to get the window handle, but it still didn't worked to use the right click, how do I get the HWND of the program that is being executed, and what must I pass in the wParam of the PostMessage?

    Thanks :)
  • Srki_KiSrki_Ki Posts: 26Member
    There is alway more than one application runnig so I dont know wich app is "executed". I'm not sure how to find window handle of main window of the app your program executed, if that is what you want. You can try to use [b]CreateProcess[/b] to start other app wich will return you thread id. With thread id you can use [b]EnumThreadWindows[/b] functions to find non-child windows associated with a thread. I have never used these function so you have to find out your own how to use them. There is some docs in Delphi (Windows SDK help) wich could help you. Wen you have window handle you can use PostMessage and it MUST AND WILL work :)

    wParam contains info wich is some thing like (TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
    [b]Shift[/b]: TShiftState; X, Y: Integer);) Shift parameter in Delphi mouse events. There are 5 constants wich describes shift states:
    MK_CONTROL Set if the CTRL key is down.
    MK_LBUTTON Set if the left mouse button is down.
    MK_MBUTTON Set if the middle mouse button is down.
    MK_RBUTTON Set if the right mouse button is down.
    MK_SHIFT Set if the SHIFT key is down.
    So, if you want to simulate right mouse button with CTRL key button pressed you set wParam to MK_CONTROL.
    I will try to find out how [b]CreateProcess[/b] and [b]EnumThreadWindows[/b] works so if you need more help don't hastate to ask :)
  • walhartwalhart Posts: 3Member
    Really thanks man, It's like almost two years that I don't do something in delphi, I changed job and also, unfortunatelly, program language, now I'm working with genexus and the last thing that I did in Delphi was a small FTP program to get and send files to a FTP server, You don't know how much you are helping me :)

    Thiago
  • Srki_KiSrki_Ki Posts: 26Member
    : Really thanks man, It's like almost two years that I don't do something in delphi, I changed job and also, unfortunatelly, program language, now I'm working with genexus and the last thing that I did in Delphi was a small FTP program to get and send files to a FTP server, You don't know how much you are helping me :)
    :
    : Thiago
    :
    Hi... i have learned how CreateProcess works. For example, if you wan to start exe file you write this

    [code]
    procedure StartExe(aExeFileName: PAnsiChar; R: TProcessInformation;): Boolean;
    var
    S:TStartupInfo;
    begin
    FillChar(S,SizeOf(S),0);
    FillChar(R,SizeOf(R),0);
    S.cb:=SizeOf(S);
    Result:=CreateProcess(nil, aExeFileName, nil, nil, False,
    CREATE_DEFAULT_ERROR_MODE, nil, nil, S, R);
    If Result then WaitForInputIdle(R.hProcess, INFINITE);
    end;
    [/code]

    If exe file was started StartExe will return [b]True[/b] and [b]R[/b] will be filled with process information. There is another way to find thread id. If you know window handle to wich you want to send message you can use this code

    [code]
    ThreadID:=GetWindowThreadProcessId(WinHandle);
    [/code]

    When you have thread id you call [b]PostThreadMessage[/b] function. It will send message to the main thread of just started app.

    [code]
    PostThreadMessage(ThreadId, Message_id, wParam, lParam);
    [/code]

    I have succeeded to close other apps, to change window names... but key and mouse messages didn't worked. I think that window wich need to recive mouse or key messages must have focus but I still don't know how to do it.
    You can try to do this:

    [code]
    procedure SendMessageToApp(aExeName: PAnsiChar; aMSG:Cardinal; wParam, lParam: Integer);
    var P: TProcessInformation;
    T: Cardinal;
    begin
    T:=GetCurrentThreadId;
    StartExe(aExeName, R);
    AttachThreadInput(T, R.dwThreadId, True);
    PostThreadMessage(T, WM_KILLFOCUS, 0, 0);
    PostThreadMessage(R.dwThreadId, WM_SETFOCUS, 0, 0);
    PostThreadMessage(R.dwThreadId, aMSG, wParam, lParam);
    AttachThreadInput(T, R.dwThreadId, False);
    end;
    [/code]
Sign In or Register to comment.