faq33.htm
来自「C++builder学习资料C++builder」· HTM 代码 · 共 75 行
HTM
75 行
<HTML>
<HEAD>
<TITLE>Create memory DCs and BitBlt to the screen with BCB.</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 memory DCs and BitBlt to the screen with BCB.
</H3>
<P>
The VCL <TT>TBitmap</TT> class contains a <TT>Canvas</TT> that
encapsulates a memory DC for the bitmap. You can create a memory DC by creating
a <TT>TBitmap</TT> object and drawing on its canvas.
</P>
<pre>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>FormPaint<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
Graphics<b>:</b><b>:</b>TBitmap <b>*</b>bmp <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TBitmap<b>;</b>
bmp<b>-></b>Width <b>=</b> ClientWidth<b>;</b>
bmp<b>-></b>Height <b>=</b> ClientHeight<b>;</b>
bmp<b>-></b>Canvas<b>-></b>Rectangle<b>(</b><font color="blue">0</font><b>,</b><font color="blue">0</font><b>,</b>ClientWidth<b>/</b><font color="blue">2</font><b>,</b> ClientHeight<b>/</b><font color="blue">2</font><b>)</b><b>;</b>
Canvas<b>-></b>Draw<b>(</b><font color="blue">0</font><b>,</b><font color="blue">0</font><b>,</b>bmp<b>)</b><b>;</b>
<b>delete</b> bmp<b>;</b>
<b>}</b>
</pre>
<P>
<B>Notes:</B> The <TT>Canvas</TT> property of <TT>TBitmap</TT> is <TT>NULL</TT>
until you use the <TT>Canvas</TT> for the first time. In the code above, <TT>Canvas</TT> is <TT>NULL</TT>
until the <TT>Rectangle</TT> call. It doesn't matter how you access the <TT>Canvas</TT>.
The <TT>Canvas</TT> is created in the first call to the <TT>GetCanvas</TT> function
(the read function for the <TT>Canvas</TT> property).
</P>
<P>
The <TT>Draw</TT> method of <TT>TCanvas</TT> results in an API <TT>StretchBlt</TT>
function call (<TT>StretchBlt</tt> will be called if source graphic is a bitmap,
<TT>DrawIcon</TT> will be called if the graphic is an icon). <TT>StretchBlt</TT> blasts the
contents of the memory bitmap into the display canvas. The <TT>CopyRect</TT> method of
<TT>TCanvas</TT> also calls the <TT>StretchBlt</TT> function. However, <TT>CopyRect</TT> copies
from a source <TT>Canvas</TT> into another <TT>Canvas</TT>, and the source <TT>Canvas</TT>
does not have to be the same size as the destination.
</P>
<P>
Setting the <TT>Width</TT> and <TT>Height</TT> of the <TT>TBitmap</TT> object is
important when you are not loading a bitmap image into the object. If you do load
a bitmap into the object from a resource or BMP file, the <TT>Width</TT> and <TT>Height</TT>
properties will be set to match the loaded image. In this case, omit the assignments to
<TT>Height</TT> and <TT>Width</TT>. For example:
</P>
<pre>
Graphics<b>:</b><b>:</b>TBitmap <b>*</b>bmp <b>=</b> <b>new</b> Graphics<b>:</b><b>:</b>TBitmap<b>;</b>
bmp<b>-></b>LoadFromResourceName<b>(</b> <b>(</b><b>int</b><b>)</b> HInstance<b>,</b> <font color="blue">"IDD_BITMAP1"</font><b>)</b><b>;</b>
bmp<b>-></b>Canvas<b>-></b>Rectangle<b>(</b><font color="blue">10</font><b>,</b><font color="blue">10</font><b>,</b>ClientWidth<b>/</b><font color="blue">2</font><b>,</b> ClientHeight<b>/</b><font color="blue">2</font><b>)</b><b>;</b>
Canvas<b>-></b>Draw<b>(</b><font color="blue">0</font><b>,</b><font color="blue">0</font><b>,</b>bmp<b>)</b><b>;</b>
<b>delete</b> bmp<b>;</b>
</pre>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?