1

I'm trying to send WM_APPCOMMAND message to foreground window, but it's not working. Can someone explain to me how to do it correctly?

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern int GetForegroundWindow();

public const int WM_APPCOMMAND = 0x319;
public const int APPCOMMAND_UNDO = 34;

SendMessage(GetForegroundWindow(), WM_APPCOMMAND, 0, WM_UNDO * 65536);

EDIT:
Hans Passant: Most AppCommands are mapped to dedicated keys on the keyboard and automatically triggered when you press such a key. Since very few keyboards actually have an Undo key, the odds that an app will respond to APPCOMMAND_UNDO are zilch.

Thank you Hans. This is the answer.

mgulan
  • 795
  • 1
  • 14
  • 33
  • Define 'not working'. Also, did you check the results of GetForegroundWindow to see if you get a handle? And if so, if it is the right handle? Maybe you can use one of the [tools mentioned in this question](http://stackoverflow.com/questions/2423869/wincheat-winspy-like-tool-for-c-builder-exes) to help you check that. – GolezTrol Jan 16 '14 at 15:25
  • It's not doing "Undo" in foreground window... I also have this code: SendMessage(GetForegroundWindow(), WM_SYSCOMMAND, SC_MINIMIZE, 0); and it's working, so I suppose GetForegroundWindow() is returning right handle. – mgulan Jan 16 '14 at 15:30
  • Are you sure you've got the right value for WM_UNDO? Says it is 0x304 in the [docs](http://msdn.microsoft.com/en-us/library/windows/desktop/bb761693%28v=vs.85%29.aspx). – Jack Hughes Jan 16 '14 at 15:34
  • Sorry, I meant APPCOMMAND_UNDO: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646275%28v=vs.85%29.aspx#appcommand_undo Yes, the value is 34... I was following this tutorial: https://kalshagar.wikispaces.com/Remote+controlling+Windows+media+player+in+C – mgulan Jan 16 '14 at 15:41
  • It's not guaranteed that every application will implement that message. There is no rule that states that it should. – GolezTrol Jan 16 '14 at 16:03
  • 2
    Most AppCommands are mapped to dedicated keys on the keyboard and automatically triggered when you press such a key. Since very few keyboards actually have an Undo key, the odds that an app will respond to APPCOMMAND_UNDO are zilch. – Hans Passant Jan 16 '14 at 16:23

0 Answers0