5

I've got a C# problem,

I am able to simulate a click to the current window, but I would like to do it if the window is minimized or hidden.

Any ideas?

tlovidiu
  • 53
  • 1
  • 3
  • Will you accept C++ answers? I have a c++ snippet but not C# at the present time. – Serdalis Apr 23 '12 at 11:46
  • Yes, C++ is good too :), but then I'd need to get the handle of the window, but it's ok :) – tlovidiu Apr 23 '12 at 11:50
  • possible duplicate of [How to send a mouse click event to a hidden window?](http://stackoverflow.com/questions/1621274/how-to-send-a-mouse-click-event-to-a-hidden-window) – Renatas M. Apr 23 '12 at 11:56

1 Answers1

6

Here is a fully working snippet which will give you the window handle, target sub window and post a message to that subwindow.

#include "TCHAR.h"
#include "Windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
    HWND hwndWindowTarget;
    HWND hwndWindowNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if (hwndWindowNotepad)
    {
        // Find the target Edit window within Notepad.
        hwndWindowTarget = FindWindowEx(hwndWindowNotepad, NULL, L"Edit", NULL);
        if (hwndWindowTarget) {
            PostMessage(hwndWindowTarget, WM_CHAR, 'G', 0);
        }
    }

    return 0;
}

At the moment it will send the G character to notepad "Untitled" (open a new notepad, do nothing.

You can find the sub-window using spy++ which comes with visual studio.

Here is an example using SendInput to send mouse events:

#include "TCHAR.h"
#include "Windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
    POINT pt;
    pt.x = 300;
    pt.y = 300;

    HWND hwndWindowTarget;
    HWND hwndWindowNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if (hwndWindowNotepad)
    {
        // Find the target Edit window within Notepad.
        hwndWindowTarget = FindWindowEx(hwndWindowNotepad, NULL, L"Edit", NULL);
        if (hwndWindowTarget) {
            PostMessage ( hwndWindowTarget, WM_RBUTTONDOWN, 0, (pt.x) & (( pt.y) << 16) );
            PostMessage ( hwndWindowTarget, WM_RBUTTONUP, 0, (pt.x ) & (( pt.y) << 16) );
        }
    }

    return 0;
}

Sorry this took so long, here's the C# version:

using System;
using System.Runtime.InteropServices;

namespace Sandbox
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        static extern IntPtr FindWindowEx(IntPtr hWnd, string lpClassName, string lpWindowName, string lParam);

        public const Int32 WM_CHAR = 0x0102;
        public const Int32 WM_KEYDOWN = 0x0100;
        public const Int32 WM_KEYUP = 0x0101;
        public const Int32 VK_RETURN = 0x0D;

        public const string windowName = "Untitled - Notepad";

        static void Main(string[] args)
        {
            Console.WriteLine($"Finding {windowName}");
            var hwndWindowNotepad = FindWindow(null, "Untitled - Notepad");

            if (hwndWindowNotepad != 0)
            {
                // Find the target Edit window within Notepad.
                var hwndWindowTarget = FindWindowEx(hwndWindowNotepad, null, "Edit", null);
                if (hwndWindowTarget != 0)
                {
                    Console.WriteLine($"Sending Char to {windowName}");
                    PostMessage(hwndWindowTarget, WM_CHAR, 'G', 0);
                }
            }
        }
    }
}
Serdalis
  • 10,296
  • 2
  • 38
  • 58
  • This is very helpful, what's the code for the mouse left down and up to use with PostMessage? :) – tlovidiu Apr 23 '12 at 11:58
  • If you want, I actually have a C# example of that code using SendInput, I'll post it. – Serdalis Apr 23 '12 at 11:59
  • It would be great in C# :) Can you please? But with a mouse button – tlovidiu Apr 23 '12 at 12:00
  • apologies, it was old code using `mouse_event` which is superseded. I'll work on converting both to C# though, but it may take a bit :) – Serdalis Apr 23 '12 at 12:08
  • you will hoever, find a very nice mouse input simulator here http://stackoverflow.com/questions/5094398/how-to-programatically-mouse-move-click-right-click-and-keypress-etc-in-winfor – Serdalis Apr 23 '12 at 12:13