2011-02-04 39 views
1

我正在使用SetWindowPosMoveWindow調整窗口大小和居中。它工作正常,但在Windows Media Player或控制面板等多個窗口中,關閉窗口並再次打開時,新的大小/移動不會反映出來。當我手動調整大小時,下次打開窗口時會反映更改。即使我撥打UpdateWindow,這些更改也不會反映出來。有什麼我需要發送窗口,以便更改得到保存? RedrawWindow有幫助嗎?謝謝?SetWindowPos/MoveWindow持續問題

+0

的問題是,這些API明確設置窗口大小(我不知道目標應用程序甚至會收到一個事件 - 但我可能是錯的),所以在內部,窗口大小不會被設置爲當應用程序未被最小化時被應用程序'記住'。我不知道如何解決這個問題,但我會研究一些類似於SendMessage API的東西。 – Rob 2011-02-04 02:56:43

+0

有趣的想法是,當我以編程方式調整大小時,我手動將鼠標懸停在窗口邊緣,直到獲取調整大小鼠標光標,然後我只需單擊向下(而不是單擊並拖動),然後關閉窗口,新大小以編程方式設置的內容已保存。不知何故,我需要發送一些消息,這是手動調整大小時發送的消息。 – user577240 2011-02-04 03:02:13

回答

4

你應該使用GetWindowPlacementSetWindowPlacement功能,而不是獲取和改變窗口的恢復,最小化,最大化和位置。這可確保窗口大小由應用程序正確保存,以便在下次啓動時恢復它們。

由於您使用C#,你需要P /從Windows API調用這些功能:

const int SW_HIDE = 0; 
const int SW_SHOWNORMAL = 1; 
const int SW_SHOWMINIMIZED = 2; 
const int SW_SHOWMAXIMIZED = 3; 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl); 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl); 

[StructLayout(LayoutKind.Sequential)] 
struct RECT 
{ 
    public int left; 
    public int top; 
    public int right; 
    public int bottom; 
} 

[StructLayout(LayoutKind.Sequential)] 
struct WINDOWPLACEMENT 
{ 
    public int length; 
    public int flags; 
    public int showCmd; 
    public Point ptMinPosition; 
    public Point ptMaxPosition; 
    public RECT rcNormalPosition; 
}