2012-12-07 23 views
2

我正在創建一個wpf應用程序來發送Outlook約會。在這個應用程序中,我打開Outlook選擇名稱對話框來選擇約會的收件人。以下是我的代碼:如何從wpf應用程序和outlook運行時打開outlook select name對話框

Outlook.SelectNamesDialog selectNameDialog = 
      outlookApp.Session.GetSelectNamesDialog(); 
     selectNameDialog.SetDefaultDisplayMode(
      Outlook.OlDefaultSelectNamesDisplayMode.olDefaultMeeting); 

     foreach (var recipient in recipientsList) 
     { 
      if (string.IsNullOrEmpty(recipient)) 
       continue; 
      Outlook.Recipient confRoom = 
       selectNameDialog.Recipients.Add(recipient); 
      // Explicitly specify Recipient.Type. 
      confRoom.Type = (int)Outlook.OlMeetingRecipientType.olRequired; 
     } 

     selectNameDialog.Recipients.ResolveAll(); 

     selectNameDialog.Display(); 

我的問題是,當我打開選擇名稱對話框,它工作正常,如果Outlook未運行。但是如果Outlook正在運行,並且我從應用程序單擊打開此對話框,它將在我的應用程序的後面以及Outlook窗口之上打開。即使前景正在運行,我也需要在應用程序的頂部顯示它。任何幫助將高度讚賞將這個對話放在首位。提前致謝。

回答

1

您可以嘗試獲取展望流程並將他的窗戶向前推進。沒有.NET鉤子這樣做,你將需要使用原生的Win32 DLL。

[Flags()] 
    private enum SetWindowPosFlags : uint 
    { 
     SynchronousWindowPosition = 0x4000, 
     DeferErase = 0x2000, 
     DrawFrame = 0x0020, 
     FrameChanged = 0x0020, 
     HideWindow = 0x0080, 
     DoNotActivate = 0x0010, 
     DoNotCopyBits = 0x0100, 
     IgnoreMove = 0x0002, 
     DoNotChangeOwnerZOrder = 0x0200, 
     DoNotRedraw = 0x0008, 
     DoNotReposition = 0x0200, 
     DoNotSendChangingEvent = 0x0400, 
     IgnoreResize = 0x0001, 
     IgnoreZOrder = 0x0004, 
     ShowWindow = 0x0040, 
    } 

    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool SetForegroundWindow(IntPtr hWnd); 

    [DllImport("user32.dll", SetLastError = true)] 
    static extern bool BringWindowToTop(IntPtr hWnd); 

    static readonly IntPtr HWND_TOP = new IntPtr(0); 
    static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); 

    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags); 

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

    int bring_window_to_front_mode = 1; // Various option here, try them out 
    Boolean TopMost = false; 

     System.Diagnostics.Process[] prcs = System.Diagnostics.Process.GetProcessesByName("XBMCLauncher"); 
     foreach (var proc in prcs) 
     { 
      Log.LogLine("Main Window Handle {0}", p.MainWindowHandle.ToInt32()); 
      switch (bring_window_to_front_mode) 
      { 
       case 1: 
        if (TopMost) 
        { 
         Log.LogLine("SetWindowPos TopMost {0}", p.MainWindowHandle.ToInt32()); 
         SetWindowPos(p.MainWindowHandle, HWND_TOPMOST, 50, 50, 500, 500, SetWindowPosFlags.IgnoreMove | SetWindowPosFlags.IgnoreResize); 
        } 
        else 
        { 
         Log.LogLine("SetWindowPos {0}", p.MainWindowHandle.ToInt32()); 
         SetWindowPos(p.MainWindowHandle, HWND_TOP, 50, 50, 500, 500, SetWindowPosFlags.IgnoreMove | SetWindowPosFlags.IgnoreResize); 
        } 
        break; 
       case 2: 
        Log.LogLine("BringWindowToTop {0}", p.MainWindowHandle.ToInt32()); 
        BringWindowToTop(p.MainWindowHandle); 
        break; 
       case 3: 
        Log.LogLine("SetForegroundWindow {0}", p.MainWindowHandle.ToInt32()); 
        SetForegroundWindow(p.MainWindowHandle); 
        break; 
       case 4: 
        Log.LogLine("SetFocus {0}", p.MainWindowHandle.ToInt32()); 
        SetFocus(p.MainWindowHandle); 
        break; 
      } 
     } 

當然的try/catch ...

相關問題