advice on sending mouse movement, clicks to a minimized window

  • Thread starter Thread starter garyusenet
  • Start date Start date
G

garyusenet

I have a programme written in C++, the programme is unmanaged and is an
executable, i don't have any source code. I'm writing a C# program.

I want to
(a) start the programme minimized.
(b) send keystrokes to the programme, and mouse strokes.

Can you please tell me how I would do this assuming the programme is
called "c:\programme.exe"

What i need to do is :
launch the programme, move the mouse to a certain position, click,
enter some keyboard strokes, press enter, then move the mouse, click
again.

*Also one other thing that i would like to know is if it is possible to
start the programme and not list it as running on the taskbar.

Thanks!

Gary-
 
Just to clarify the program i want to start minimized is the C++
executable.

TIA Gary-
 
Just to further clarify - i would like to complete all the mouse
movement, and keyboard strokes while the program is minimized / not
visible.

And for future reference I will re read my post a number of times, so i
dont repeat this process of having to clarify my original post twice!

Thankyou,

Gary-
 
launch the programme, move the mouse to a certain position, click,
enter some keyboard strokes, press enter, then move the mouse, click
again.

This is a difficult task.

To launch the program, use Process.Start

Instead of running minimized, I think you should try repositioning it
offscreen. I'd try interop with the win32 function SetWindowPosition,
but maybe there's a way to do it in .net. This function can also hide
it. But some applications might behave differently if they're
hidden/minimized, which is why that might not work. Another way to
hide it from the taskbar is win32 function GetWindowLong(GWL_EXSTYLE)
to give it WS_EX_TOOLWINDOW.

To find its window, I've used interop with the win32 function
"FindWindow". And I've used this to navigate down to the appropriate
dialogs/windows.

To enter keyboard strokes and click buttons, it's nicer instead if you
post the appropriate messages direct to the controls. To learn which
messages to send where, you should read a lot of MSDN about common
controls and their messages, eg. BN_CLICKED and EM_SETTEXT and so on.
There's a lot of reading for you to do. I don't know the .net calls
for sending messages to a window. You could use interop with
PostMessage.


Otherwise, if those aren't working, you can spoof mouse movement and
keyboard strokes at a lower level. This is a worse solution, more
clunky, so avoid it if possible. Here's some example code to get you
started.


[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct MOUSEINPUT
{ public static MOUSEINPUT Zero()
{ MOUSEINPUT mi;

mi.type1=INPUT_MOUSE;mi.dx1=0;mi.dy1=0;mi.mouseData1=0;mi.dwFlags1=0;mi.time1=0;mi.dwExtraInfo1=IntPtr.Zero;

mi.type2=INPUT_MOUSE;mi.dx2=0;mi.dy2=0;mi.mouseData2=0;mi.dwFlags2=0;mi.time2=0;mi.dwExtraInfo2=IntPtr.Zero;
return mi;
}
public int Size() {return (int)Count()*28;}
public UInt32 Count() {if (dwFlags2!=0) return 2; else if
(dwFlags1!=0) return 1; else return 0;}
public Int32 type1, dx1,dy1, mouseData1, dwFlags1, time1; public
IntPtr dwExtraInfo1;
public Int32 type2, dx2,dy2, mouseData2, dwFlags2, time2; public
IntPtr dwExtraInfo2;
}
const Int32 INPUT_MOUSE=0;
const Int32 MOUSEEVENTF_LEFTDOWN=0x0002;
const Int32 MOUSEEVENTF_LEFTUP = 0x0004;
const Int32 MOUSEEVENTF_ABSOLUTE = 0x8000;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern UInt32 SendInput(UInt32 nInputs, ref MOUSEINPUT
pInputs, Int32 cbSize);


public Form1()
{ InitializeComponent();
}

private void button2_Click(object sender,EventArgs e)
{ MessageBox.Show("click2");
}

int state=1;

private void button1_Click(object sender,EventArgs e)
{ state=1;
}

private void timer1_Tick(object sender,EventArgs e)
{ if (state==0) return;
MOUSEINPUT mi = MOUSEINPUT.Zero();
if (state==1)
{ Point pt=new Point(button2.Width/2,button2.Height/2),
oldpos=Cursor.Position;
Cursor.Position = button2.PointToScreen(pt);
mi.dwFlags1=MOUSEEVENTF_LEFTDOWN;
uint r=SendInput(mi.Count(), ref mi, mi.Size());
if (r==mi.Count()) Console.Beep();
state=3; return;
}
if (state==3)
{ mi.dx1=0; mi.dy1=0; mi.dwFlags1=MOUSEEVENTF_LEFTUP; uint
r=SendInput(1,ref mi, mi.Size());
if (r==1) Console.Beep();
state=0; return;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top