In my application I am trying to bring Outlook 2010 into focus and send it a CTRL-N (new email).
I have tried many different iterations of ShowWindow, FindWindow, SetFocus, SetForegroundWindow and SendMessage and can't seem to get any of them to work.
It works fine for Notepad, but not for Outlook... My code is:
using System.Runtime.InteropServices;
using System.Diagnostics;
const int kKeyDown = 0x0100;
const int kKeyUp = 0x0101;
const int kCtrl = 0x11;
const int kN = 0x4e;
Process[] prcOutlook = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process prcTempProc in prcOutlook)
{
if (prcTempProc.ProcessName == "OUTLOOK")
{
IntPtr windowToFind = prcTempProc.MainWindowHandle;
if (ShowWindow(windowToFind, 1))
{
SetFocus(wHndle);
int result = SendMessage(windowToFind, kKeyDown, kCtrl, 0);
result = SendMessage(windowToFind, kKeyDown, kN, 0);
result = SendMessage(windowToFind, kKeyUp, kCtrl, 0);
result = SendMessage(windowToFind, kKeyUp, kN, 0);
}
}
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
The code runs fine, it just never brings Outlook to focus to get the keystrokes...
Where am I going wrong?
Regards, Dean