2011-06-20 12 views
4

私はこれを使用して取得するウィンドウの高さと幅にしようとしています:の取得ウィンドウのリージョン

[DllImport("User32.dll", SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] 
    public static extern IntPtr GetForegroundWindow(); 

    [DllImport("user32.dll")] 
    private static extern bool GetWindowRect(IntPtr handle, out Rectangle rect); 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     Rectangle bonds = new Rectangle(); 
     GetWindowRect(GetForegroundWindow(), out bonds); 
     Bitmap bmp = new Bitmap(bonds.Width, bonds.Height); 
     Graphics memoryGraphics = Graphics.FromImage(bmp); 
     IntPtr dc = memoryGraphics.GetHdc(); 
     PrintWindow(GetForegroundWindow(), dc, 0x1); 
     memoryGraphics.ReleaseHdc(dc); 
     bmp.Save("C:\\Test.gif", ImageFormat.Gif); 
    } 

が、bonds.widthと高さは、実際のウィンドウ以上です。

例:私のウィンドウは100x150、bond.heightは400のようなもので、bonds.widthは500と同じです。画像の角にあるウィンドウからなる400x500のサイズの画像を取得します。画像が(悪い)ウィンドウ

NOTEより道大きいので、残りは黒です:私は、ウィンドウが、それは私にとって良いaerolessだされていることを気にしません。

だから、どんな提案、または多分域を得るためのより良い方法はありますか?

+0

画面は今理にかなって、画面の左上に相対 –

答えて

2

あなたはScreenToClientを使用して座標を変換する必要があります。

GetWindowRectは、画面の左上にウィンドウ領域に対して返す(0,0)。

+0

Ohhhh、(のみVistaの/のWindows7)奇妙なスケールです。私はあなたの解決策を試みます。 – user779444

+0

それは画面の左上を基準にしてポイントを返すと言いますが、まだ高さ/幅が必要なので、2つを組み合わせる必要がありますか? – user779444

+0

@User yeah、GetWindowRectの呼び出しの後にScreenToClientを使用して、通常のように幅と高さを得るために減算するだけです。 –