⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faq36.htm

📁 C++builder学习资料C++builder
💻 HTM
字号:
<HTML>

<HEAD>

   <TITLE>Create a TCanvas object for the entire screen (or any other window).</TITLE>

   <META NAME="Author" CONTENT="Harold Howe">

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

<TABLE  BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">



<TR>

<TD>









<H3>

Create a TCanvas object for the entire screen (or any other window).

</H3>

<P>

The <TT>GetDC</TT> API function allows you to create a device context for any

window. You can then assign this device context to the <TT>Handle</TT> property

of a newly created <TT>TCanvas</TT> object.

</P>

<pre>

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>

ScreenCanvas<b>-></b>Pen<b>-></b>Width <b>=</b> <font color="blue">10</font><b>;</b>

ScreenCanvas<b>-></b>Pen<b>-></b>Color <b>=</b> clRed<b>;</b>

ScreenCanvas<b>-></b>MoveTo<b>(</b><font color="blue">0</font><b>,</b><font color="blue">0</font><b>)</b><b>;</b>

ScreenCanvas<b>-></b>LineTo<b>(</b>Screen<b>-></b>Width<b>,</b>Screen<b>-></b>Height<b>)</b><b>;</b>

<b>delete</b> ScreenCanvas<b>;</b>

ReleaseDC<b>(</b>NULL<b>,</b>dc<b>)</b><b>;</b>

</pre>

<P>

You can draw on a specific window by obtaining the window handle and then passing

that handle to the <TT>GetDC</TT> call. This code draws directly onto the Object

Inspector if its open.

</P>

<pre>

HWND OIHandle <b>=</b> FindWindow<b>(</b>NULL<b>,</b><font color="blue">"Object Inspector"</font><b>)</b><b>;</b>

<b>if</b><b>(</b>OIHandle<b>)</b>

<b>{</b>

    HDC dc <b>=</b> GetWindowDC<b>(</b>OIHandle<b>)</b><b>;</b>

    Graphics<b>:</b><b>:</b>TCanvas <b>*</b>OICanvas <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TCanvas<b>;</b>

    OICanvas<b>-></b>Handle <b>=</b> dc<b>;</b>

    OICanvas<b>-></b>Pen<b>-></b>Width <b>=</b> <font color="blue">10</font><b>;</b>

    OICanvas<b>-></b>Pen<b>-></b>Color <b>=</b> clRed<b>;</b>

    OICanvas<b>-></b>MoveTo<b>(</b><font color="blue">0</font><b>,</b><font color="blue">0</font><b>)</b><b>;</b>

    OICanvas<b>-></b>LineTo<b>(</b>OICanvas<b>-></b>ClipRect<b>.</b>Right<b>,</b>OICanvas<b>-></b>ClipRect<b>.</b>Bottom<b>)</b><b>;</b>

    <b>delete</b> OICanvas<b>;</b>

    ReleaseDC<b>(</b>OIHandle<b>,</b>dc<b>)</b><b>;</b>

<b>}</b>

</pre>

<P>

<B>Note:</B> Use this code with caution. It works fine, but drawing directly

onto another window is something that most programs should avoid.

</P>

<P>

<B>Note:</B> You must remember to call <TT>ReleaseDC</TT> for the <TT>HDC</TT> handle.

<TT>TCanvas</TT> does not release the device context when you delete the <TT>TCanvas</TT>

pointer. You can inspect <TT>TCanvas::Destroy</TT> to prove this for yourself. (So what

about <TT>TForm</TT>? Does it call <TT>ReleaseDC</TT> for the <TT>HDC</TT> that was used

to create its <TT>Canvas</TT> property? No. <TT>TForm</TT> uses a descendent of

<TT>TCanvas</TT> called <TT>TControlCanvas</TT>. <TT>TControlCanvas</TT> automatically

calls <TT>ReleaseDC</TT> in its destructor. <TT>TGraphicControl</TT> also uses

<TT>TControlCanvas</TT> (look in <TT>TGraphicControl::Create</TT>)).

</P>





</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -