faq37.htm
来自「C++builder学习资料C++builder」· HTM 代码 · 共 103 行
HTM
103 行
<HTML>
<HEAD>
<TITLE>Screen capture the desktop.</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Screen capture the desktop.
</H3>
<P>
This FAQ is an extension to the FAQ on how to <A HREF="faq36.htm">create a TCanvas
object for the entire screen</A>. This technique first creates a <TT>TCanvas</TT> object for
the screen. An <TT>Image</TT> control then calls the <TT>TCanvas::CopyRect</TT> function to
copy the screen image into the <TT>TImage</TT> object.
</P>
<pre>
<font color="navy">// First, get an HDC for the entire screen with the API GetDC function.</font>
<font color="navy">// Then create a Canvas based on the DC.</font>
HDC dc <b>=</b> GetDC<b>(</b><font color="blue">0</font><b>)</b><b>;</b>
Graphics<b>:</b><b>:</b>TCanvas <b>*</b>ScreenCanvas <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TCanvas<b>;</b>
ScreenCanvas<b>-></b>Handle <b>=</b> dc<b>;</b>
<font color="navy">// Resize the TImage control to match the size of the screen.</font>
<font color="navy">// Then copy from the screen Canvas to the TImage Canvas.</font>
Image1<b>-></b>Picture<b>-></b>Bitmap<b>-></b>Width <b>=</b> Screen<b>-></b>Width<b>;</b>
Image1<b>-></b>Picture<b>-></b>Bitmap<b>-></b>Height<b>=</b> Screen<b>-></b>Height<b>;</b>
TRect rect <b>=</b> Rect<b>(</b><font color="blue">0</font><b>,</b><font color="blue">0</font><b>,</b>Screen<b>-></b>Width<b>,</b> Screen<b>-></b>Height<b>)</b><b>;</b>
Image1<b>-></b>Picture<b>-></b>Bitmap<b>-></b>Canvas<b>-></b>CopyRect<b>(</b>rect<b>,</b> ScreenCanvas<b>,</b> rect<b>)</b><b>;</b>
<font color="navy">// cleanup</font>
<b>delete</b> ScreenCanvas<b>;</b>
ReleaseDC<b>(</b>NULL<b>,</b>dc<b>)</b><b>;</b>
</pre>
<P>
You can capture a specific window by obtaining a <TT>DC</TT> for that window instead
of getting a <TT>DC</TT> for the entire screen. This code captures the C++Builder main
window if it's visible.
</P>
<pre>
<font color="navy">// The BCB IDE window has the window class "TAppBuilder"</font>
HWND BCBHandle <b>=</b> FindWindow<b>(</b><font color="blue">"TAppBuilder"</font><b>,</b>NULL<b>)</b><b>;</b>
<b>if</b><b>(</b>BCBHandle<b>)</b>
<b>{</b>
<font color="navy">// Create a TCanvas for the BCB IDE window. GetWindowDC returns a DC for the</font>
<font color="navy">// whole window, including the menu, title bar and border.</font>
HDC dc <b>=</b> GetWindowDC<b>(</b>BCBHandle<b>)</b><b>;</b>
Graphics<b>:</b><b>:</b>TCanvas <b>*</b>ScreenCanvas <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TCanvas<b>;</b>
ScreenCanvas<b>-></b>Handle <b>=</b> dc<b>;</b>
<font color="navy">// need to create a TRect that is the same width and height as the ClipRect</font>
<font color="navy">// of the Canvas, but whose left and top members are zero.</font>
TRect rect <b>=</b> ScreenCanvas<b>-></b>ClipRect<b>;</b>
rect<b>.</b>Right <b>=</b> rect<b>.</b>Right <b>-</b> rect<b>.</b>Left<b>;</b>
rect<b>.</b>Bottom <b>=</b> rect<b>.</b>Bottom <b>-</b> rect<b>.</b>Top<b>;</b>
rect<b>.</b>Top <b>=</b> rect<b>.</b>Left <b>=</b> <font color="blue">0</font><b>;</b>
<font color="navy">// resize the bitmap of the Image, and then copy the contents of the</font>
<font color="navy">// BCB ide canvas to the bitmap's canvas. Then clean up the canvas and DC</font>
Image1<b>-></b>Picture<b>-></b>Bitmap<b>-></b>Width <b>=</b> rect<b>.</b>Right<b>;</b>
Image1<b>-></b>Picture<b>-></b>Bitmap<b>-></b>Height<b>=</b> rect<b>.</b>Bottom<b>;</b>
Image1<b>-></b>Picture<b>-></b>Bitmap<b>-></b>Canvas<b>-></b>CopyRect<b>(</b>rect<b>,</b>
ScreenCanvas<b>,</b>
ScreenCanvas<b>-></b>ClipRect<b>)</b><b>;</b>
<b>delete</b> ScreenCanvas<b>;</b>
ReleaseDC<b>(</b>BCBHandle<b>,</b>dc<b>)</b><b>;</b>
<b>}</b>
</pre>
<P>
<B>Note:</B> The <TT>ClipRect</TT> property of <TT>TCanvas</TT> is recalculated every time
the property is read. This is good, because the clipping rect of a window changes
every time the window is moved or resized. <TT>ClipRect</TT> re-reads the info so the
<TT>Canvas</TT> keeps up with changes to its underlying <TT>HWND</TT> and <TT>HDC</TT> (this
did not play a role in the example code)
</P>
<P>
<B>Note:</B> You must remember to call <TT>ReleaseDC</TT> for the <TT>HDC</TT> handle.
</P>
<P>
<B>Note:</B> Both <TT>GetDC</TT> and <TT>GetWindowDC</TT> take an <TT>HWND</TT> as their
argument. <TT>GetDC</TT> returns a <TT>DC</TT> for the client area of the specified window.
<TT>GetWindowDC</TT> returns a <TT>DC</TT> that can draw on both the non-client and client
areas of the window. In other words, you can paint the title bar and menu of a window if
you obtain the <TT>DC</TT> with <TT>GetWindowDC</TT>.
</P>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?