tags:

views:

54

answers:

3

int x = 5; int y = 10;

y = y << 16; int coord = x | y;

NativeMethods.SendMessage(hwnd, WM_LBUTTONDOWN, new IntPtr(0), new IntPtr(coord)); NativeMethods.SendMessage(hwnd, WM_LBUTTONUP, new IntPtr(0), new IntPtr(coord));

Using the above code (ref: MSDN), I'm able to select a row in a datagridview in an external application. I would like to know how I can send a ctrl-a and ctrl-c to the same datagridview.

Still trying to connect to why the x and y variables are initialized to 5,10, and why y is left shifted by 16 and then | with x.

+1  A: 

What about this:

SendMessage( hwnd, WM_KEYDOWN, VK_CTRL, 0 );
SendMessage( hwnd, WM_KEYDOWN, 0x43, 0 );
// Ctrl and C keys are both pressed.
SendMessage( hwnd, WM_KEYUP, 0x43, 0 );
SendMessage( hwnd, WM_KEYUP, VK_CTRL, 0 );

0x43 being the C key (see http://msdn.microsoft.com/en-us/library/dd375731(v=VS.85).aspx)


Edit: If it doesn't work, try sending WM_COPY, which should be a better idea.

SendMessage( hwnd, WM_COPY, 0, 0 );
Bertrand Marron
Thank you. Testing it now.
Gooose
No luck. I tried the following SendMessage(hwnd, WM_KEYDOWN, new IntPtr(VK_CTRL), new IntPtr(0)); SendMessage(hwnd, WM_KEYDOWN, new IntPtr(0x43), new IntPtr(0)); SendMessage(hwnd, WM_KEYDOWN, new IntPtr(0x43), new IntPtr(0)); SendMessage(hwnd, WM_KEYDOWN, new IntPtr(VK_CTRL), new IntPtr(0)); I can see the row selected, but when I do a ctrl-v in notepad, I don't see the text copied
Gooose
I edited my answer, I've just came across `WM_COPY` on MSDN, try that.
Bertrand Marron
WM_Copy did not work. SendMessage(hwnd, WM_COPY, new IntPtr(0), new IntPtr(0))
Gooose
Are you sure you're sending it to the correct window?
Bertrand Marron
+1  A: 

You might actually need Windows Subclassing. Note this is not C++ Subclassing.

This technique sends messages from a particular Window Procedure (WndProc) to another WndProc, thus achieving what you seem to want.

Once setup it just works. MSDN is light on this information, thus the link above as a tutorial.

More info:

Subclassing Controls - MSDN

ActiveX Controls: Subclassing a Windows Control

** Subclassing Windows Forms Controls May be the most pertinent.

JustBoo
Thanks. 1001. :-)
JustBoo
A: 

Additional Links for "Windows Hooking." It's a technique to hook or trap messages and events in external applications.

Hooking

EasyHook

MSDN Hooks Good overview.

HTH

JustBoo