2

I want to get the outlook Active window handle (hwnd) in C#. I want to use this for SendMessage() method which takes hwnd as first parameter. Outlook is open and not minimized. Tried to do like this.. dynamic winHwnd= Globals.ThisAddIn.Application.ActiveWindow(); Not working as type mismatches . Even if i convert it doesn't work. Could some one suggest me to get this handler..

  • Are you doing this from within your plugin which has been loaded by Outlook or by some other external application? – CathalMF Apr 22 '16 at 08:08
  • Yes . I am doing within my outlook add-in which is been loaded by outlook.I have a button the the ribbon . this button is a drop down button and i am able to get the control of the button but not able to execute. So i thought i can perform a mouse click i.e WM_LBUTTONDOWN in sendmessage() method. For that i want the outlook handler. – Soumya Chiniwar Apr 22 '16 at 08:11

3 Answers3

2

You probably want to use the GetActiveWindow api function.

[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();

IntPtr handle = GetActiveWindow();

Try and minimize the window to see if you are getting the correct handle.

private const int SW_SHOWMINIMIZED = 2;

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow)

ShowWindowAsync(hWnd, SW_SHOWMINIMIZED);
CathalMF
  • 9,705
  • 6
  • 70
  • 106
  • I did this. When i debug handle has some value but when i send this handle in SendMessage() method to click the button it is not happening. Do i have to take to button control also which is in ribbon. – Soumya Chiniwar Apr 22 '16 at 08:52
  • @SoumyaChiniwar Updated answer on how to minimise a window based on the handle received. You can use this to test if you have received the correct handle. – CathalMF Apr 22 '16 at 09:02
  • Yes i tried with what you said. It is having the correct handler. It is minimizing the window. I want to click on button that is on ribbon. Can you suggest me how to i perform that. – Soumya Chiniwar Apr 22 '16 at 11:19
1

Maybe you could try to use generic approach by using system Api FindWindow to get the window you interested by it's name:

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

 IntPtr hWnd = (IntPtr)FindWindow(windowName, null);

From ActiveWindow you could try:

dynamic activeWindow = Globals.ThisAddIn.Application.ActiveWindow();
    IntPtr outlookHwnd = new OfficeWin32Window(activeWindow).Handle;
VitaliyK
  • 279
  • 1
  • 8
  • I don't have to find window because we already have handler. Is there a way to do with ActiveWindow() which actually has the outlook handler? – Soumya Chiniwar Apr 22 '16 at 08:28
  • I tired this i don't find OfficeWin32Window . Instead i used ' IntPtr windowHandle = new IWin32Window(win).Handle;' which is throwing me error --- "Cannot create an instances of abstract class". – Soumya Chiniwar Apr 22 '16 at 08:42
  • try implementation from here: https://github.com/JakeGinnivan/VSTOContrib/blob/master/src/VSTOContrib.Core/OfficeWin32Window.cs – VitaliyK Apr 22 '16 at 08:43
  • I already have a add-in . Inside which i am getting the control of a button using redemption. Thing is it is a drop down item and i cannot perform execute() on it. I want to do mouse click using WM_LBUTTONDOWN. – Soumya Chiniwar Apr 22 '16 at 11:05
-1

Cast the Inspector object to the IOleWindow interface and call IOleWindow::GetWindow. The same will work for the Explorer object.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78