Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li>First of all you have to get the right window. As sharptooth already noted you should use <code>GetForegroundWindow</code> instead of <code>GetDesktopWindow</code>. You have done it right in your <a href="http://pastebin.com/m2e334a4a" rel="noreferrer">improved version</a>.</li> <li>But then you have to resize your bitmap to the actual size of the DC/Window. You haven't done this yet.</li> <li>And then make sure you don't capture some fullscreen window!</li> </ol> <p>When I executed your code, my Delphi IDE was captured and as it is on fullscreen by default, it created the illusion of a fullscreen screenshot. (Even though your code is mostly correct)</p> <p>Considering the above steps, I was successfully able to create a single-window screenshot with your code.</p> <p>Just a hint: You can <code>GetDC</code> instead of <code>GetWindowDC</code> if you are only interested in the client area. (No window borders)</p> <p><strong>EDIT:</strong> Here's what I made with your code:</p> <p><strong>You should not use this code! Look at the improved version below.</strong></p> <pre><code>procedure TForm1.Button1Click(Sender: TObject); const FullWindow = True; // Set to false if you only want the client area. var hWin: HWND; dc: HDC; bmp: TBitmap; FileName: string; r: TRect; w: Integer; h: Integer; begin form1.Hide; sleep(500); hWin := GetForegroundWindow; if FullWindow then begin GetWindowRect(hWin,r); dc := GetWindowDC(hWin) ; end else begin Windows.GetClientRect(hWin, r); dc := GetDC(hWin) ; end; w := r.Right - r.Left; h := r.Bottom - r.Top; bmp := TBitmap.Create; bmp.Height := h; bmp.Width := w; BitBlt(bmp.Canvas.Handle, 0, 0, w, h, DC, 0, 0, SRCCOPY); form1.Show ; FileName := 'Screenshot_'+FormatDateTime('mm-dd-yyyy-hhnnss',now()); bmp.SaveToFile(Format('C:\Screenshots\%s.bmp', [FileName])); ReleaseDC(hwin, DC); bmp.Free; end; </code></pre> <p><strong>EDIT 2:</strong> As requested I'm adding a better version of the code, but I'm keeping the old one as a reference. You should seriously consider using this instead of your original code. It'll behave much nicer in case of errors. (Resources are cleaned up, your form will be visible again, ...) </p> <pre><code>procedure TForm1.Button1Click(Sender: TObject); const FullWindow = True; // Set to false if you only want the client area. var Win: HWND; DC: HDC; Bmp: TBitmap; FileName: string; WinRect: TRect; Width: Integer; Height: Integer; begin Form1.Hide; try Application.ProcessMessages; // Was Sleep(500); Win := GetForegroundWindow; if FullWindow then begin GetWindowRect(Win, WinRect); DC := GetWindowDC(Win); end else begin Windows.GetClientRect(Win, WinRect); DC := GetDC(Win); end; try Width := WinRect.Right - WinRect.Left; Height := WinRect.Bottom - WinRect.Top; Bmp := TBitmap.Create; try Bmp.Height := Height; Bmp.Width := Width; BitBlt(Bmp.Canvas.Handle, 0, 0, Width, Height, DC, 0, 0, SRCCOPY); FileName := 'Screenshot_' + FormatDateTime('mm-dd-yyyy-hhnnss', Now()); Bmp.SaveToFile(Format('C:\Screenshots\%s.bmp', [FileName])); finally Bmp.Free; end; finally ReleaseDC(Win, DC); end; finally Form1.Show; end; end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload