2011-01-27 45 views
0

我有一個用C#編寫的WPF 4.0應用程序,目前使用System.Windows.Forms.Help.ShowHelp()來顯示應用程序的Windows幫助文件。如何使用System.Windows.Forms.Help.ShowHelp()來控制幫助窗口的大小?

我希望能夠在打開時控制幫助查看器的初始大小。目前它默認爲最近使用的尺寸。

我該如何做到這一點?

+1

,我不認爲你可以。我認爲這個想法是它記住了用戶的偏好。但是,如果您可以找出**哪個**存儲了首選項,則可以在調用「ShowHelp」之前覆蓋它。 – ChrisF 2011-01-27 17:31:20

回答

4

這是可能的。一類添加到項目,並粘貼此代碼:

using System; 
using System.Text; 
using System.Drawing; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
using System.Diagnostics; 

static class Utils { 
    public static void MoveHelpWindow(Rectangle rc) { 
     EnumThreadWndProc callback = (hWnd, lp) => { 
      // Check if this is the help window 
      StringBuilder sb = new StringBuilder(260); 
      GetClassName(hWnd, sb, sb.Capacity); 
      if (sb.ToString() != "HH Parent") return true; 
      MoveWindow(hWnd, rc.Left, rc.Top, rc.Width, rc.Height, false); 
      return false; 
     }; 
     foreach (ProcessThread pth in Process.GetCurrentProcess().Threads) { 
      EnumThreadWindows(pth.Id, callback, IntPtr.Zero); 
     } 
    } 
    // P/Invoke declarations 
    private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp); 
    [DllImport("user32.dll")] 
    private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp); 
    [DllImport("user32.dll")] 
    private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen); 
    [DllImport("user32.dll")] 
    private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int w, int h, bool repaint); 
} 

你使用這樣的BeginInvoke的調用是非常重要的:

Help.ShowHelp(this, @"file://c:\windows\help\bcmwlhlp.chm"); 
this.BeginInvoke(new MethodInvoker(() => Utils.MoveHelpWindow(new Rectangle(0, 0, 300, 200)))); 
+0

不錯。恭喜 ! – baalazamon 2011-01-27 18:49:58

相關問題