faq35.htm
来自「C++builder学习资料C++builder」· HTM 代码 · 共 100 行
HTM
100 行
<HTML>
<HEAD>
<TITLE>Paint on the contents of a TImage control.</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Paint on the contents of a TImage control.
</H3>
<P>
<TT>TImage</TT> provides a <TT>Canvas</TT> property that allows you to draw on the contents
of the image. Changes made to the image become a permanent part of the image. If you
copy the resulting image to the clipboard or save the image to a file, your changes
will appear right with the image. Here is a code snippet that draws an arrow onto
an image.
<pre>
<b>const</b> TPoint Arrow1<b>[</b><b>]</b> <b>=</b> <b>{</b> <b>{</b><font color="blue">80</font><b>,</b><font color="blue">105</font><b>}</b> <b>,</b> <b>{</b><font color="blue">106</font><b>,</b><font color="blue">66</font><b>}</b><b>,</b> <b>{</b><font color="blue">80</font><b>,</b><font color="blue">87</font><b>}</b><b>,</b><b>{</b><font color="blue">94</font><b>,</b><font color="blue">98</font><b>}</b><b>}</b><b>;</b>
Image1<b>-></b>Canvas<b>-></b>Pen<b>-></b>Color <b>=</b> clBlue<b>;</b>
Image1<b>-></b>Canvas<b>-></b>Pen<b>-></b>Width <b>=</b> <font color="blue">3</font><b>;</b>
Image1<b>-></b>Canvas<b>-></b>MoveTo<b>(</b>Arrow1<b>[</b><font color="blue">0</font><b>]</b><b>.</b>x<b>,</b>Arrow1<b>[</b><font color="blue">0</font><b>]</b><b>.</b>y<b>)</b><b>;</b>
Image1<b>-></b>Canvas<b>-></b>LineTo<b>(</b>Arrow1<b>[</b><font color="blue">1</font><b>]</b><b>.</b>x<b>,</b>Arrow1<b>[</b><font color="blue">1</font><b>]</b><b>.</b>y<b>)</b><b>;</b>
Image1<b>-></b>Canvas<b>-></b>MoveTo<b>(</b>Arrow1<b>[</b><font color="blue">0</font><b>]</b><b>.</b>x<b>,</b>Arrow1<b>[</b><font color="blue">0</font><b>]</b><b>.</b>y<b>)</b><b>;</b>
Image1<b>-></b>Canvas<b>-></b>LineTo<b>(</b>Arrow1<b>[</b><font color="blue">2</font><b>]</b><b>.</b>x<b>,</b>Arrow1<b>[</b><font color="blue">2</font><b>]</b><b>.</b>y<b>)</b><b>;</b>
Image1<b>-></b>Canvas<b>-></b>MoveTo<b>(</b>Arrow1<b>[</b><font color="blue">0</font><b>]</b><b>.</b>x<b>,</b>Arrow1<b>[</b><font color="blue">0</font><b>]</b><b>.</b>y<b>)</b><b>;</b>
Image1<b>-></b>Canvas<b>-></b>LineTo<b>(</b>Arrow1<b>[</b><font color="blue">3</font><b>]</b><b>.</b>x<b>,</b>Arrow1<b>[</b><font color="blue">3</font><b>]</b><b>.</b>y<b>)</b><b>;</b>
</pre>
<P>
<B>Note:</B> You can only draw on the <TT>Canvas</TT> property of <TT>TImage</TT> if the
<TT>Picture</TT> is empty or contains a bitmap as its graphic. You cannot draw on the
<TT>Canvas</TT> of <TT>TImage</TT> if the image contains an icon or a metafile as its
graphic. The <TT>GetCanvas</TT> read method of <TT>TImage</TT> demonstrates why.
</P>
<pre>
<font color="navy">// function converted to C++</font>
TCanvas <b>*</b> TImage<b>:</b><b>:</b>GetCanvas<b>(</b><b>)</b>
<b>{</b>
Graphics<b>:</b><b>:</b>TBitmap <b>*</b>Bitmap<b>;</b>
<b>if</b> <b>(</b>Picture<b>-></b>Graphic <b>==</b> NULL<b>)</b>
<b>{</b>
Bitmap <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TBitmap
Bitmap<b>-></b>Width <b>=</b> Width<b>;</b>
Bitmap<b>-></b>Height <b>=</b> Height<b>;</b>
<font color="navy">// assign new bitmap to the Graphic property. This calls the</font>
<font color="navy">// SetGraphic write method of TPicture. SetGraphic calls Assign,</font>
<font color="navy">// equivalent to calling Picture->Graphic->Assign(Bitmap);</font>
Picture<b>-></b>Graphic <b>=</b> Bitmap<b>;</b>
<font color="navy">// Delete the temp bitmap pointer. Because Assign was called,</font>
<font color="navy">// the contents of the HBITMAP is retained in Picture->Graphic</font>
<b>delete</b> Bitmap<b>;</b>
<b>}</b>
<b>if</b> <b>(</b><font color="navy">/* Picture->Graphic is a TBitmap object */</font> <b>)</b>
<b>return</b> Picture<b>-></b>Bitmap<b>-></b>Canvas<b>;</b>
<b>else</b>
<b>throw</b> EInvalidOperation<b>(</b> <font color="navy">/* some args here maybe */</font><b>)</b><b>;</b>
<b>}</b>
</pre>
<P>
Notice that if the <TT>Picture</TT> property is empty, a new bitmap object is created for
you to draw in. If a bitmap is loaded, then <TT>TImage::GetCanvas</TT> simply returns the
<TT>Canvas</TT> of the <TT>Bitmap</TT>. The code generates an exception if the
<TT>Picture</TT> is an icon or a metafile.
</P>
<P>
<B>Note:</B> As a result of how <TT>TImage::GetCanvas</TT> works, there is no
difference beteen <TT>Image->Canvas</TT> and <TT>Image->Picture->Bitmap->Canvas</TT>.
</P>
<P>
<B>Note:</B> The width and the height of an <TT>Image</TT> control's drawing canvas are
identical to the original bitmap size. This may differ from the <TT>Width</TT> and
<TT>Height</TT> of the image if the bitmap has been stretched. For example, place an
<TT>Image</TT> control onto a new form. Size the form so it is 500 x 400 pixels. Set the
<TT>Align</TT> property of the <TT>Image</TT> control to <TT>alClient</TT>,
and load in the CHEMICAL.BMP splash screen that ships with BCB. Finally, change
the <TT>Stretch</TT> property to true to make the picture expand to fit the entire form.
At this point, <TT>Image1->Width</TT> will equal <TT>Form1->ClientWidth</TT>, but
<TT>Image1->Picture->Width</TT> will be the width of CHEMICAL.BMP (240).
</P>
<P>
<B>Note:</B> When you draw on an image, try to use only the standard Windows
colors or a color that is already conained in the palette of the Image.
</P>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?