faq56.htm

来自「C++builder学习资料C++builder」· HTM 代码 · 共 56 行

HTM
56
字号


<HTML>

<HEAD>

   <TITLE>Extract the individual red, blue, and green intensities from a TColor object</TITLE>

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

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

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



<TR>

<TD>

<H3>

Extract the individual red, blue, and green intensities from a TColor object </H3>



<P>

Use the API functions <TT>GetRValue</TT>, <TT>GetBValue</TT>, and <TT>GetGValue</TT>. Here

an example that incrementally changes the background color of a form by extracting and then

modifying the individual color intensities (note: to use the code, set the Color of form to

clBlue, clNavy, or any of the other color constants that are not system color names, such as

clBtnFace)

</P>

<pre>

<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>

<b>{</b>

    BYTE nRed   <b>=</b> GetRValue<b>(</b>Color<b>)</b><b>;</b>

    BYTE nBlue  <b>=</b> GetBValue<b>(</b>Color<b>)</b><b>;</b>

    BYTE nGreen <b>=</b> GetGValue<b>(</b>Color<b>)</b><b>;</b>



    nRed <b>+</b><b>=</b><font color="blue">10</font><b>;</b>

    nBlue <b>-</b><b>=</b><font color="blue">10</font><b>;</b>

    nGreen <b>*</b><b>=</b> <font color="blue">1.05</font><b>;</b>

    Color <b>=</b><b>(</b>TColor<b>)</b> RGB<b>(</b>nRed<b>,</b> nGreen<b>,</b> nBlue<b>)</b><b>;</b>

<b>}</b>

</pre>

<P>

<B>Note:</B> The <TT>Get</TT> functions return an intensity between 0 (black) and 255 (max

intensity. The return type is <TT>BYTE</TT> because the individual intensities fit in

one byte. When you set an intensity that is less than 0 or greater than 255, the RGB macro

chops of the extra bits in your value (it only retains the lower 8 bits of your intensity).

</P>

<P>

<B>Note:</B> Technically, the <TT>Get</TT> functions are not really functions, they are C

style macros. You can see their implementaion in <TT>INCLUDE\WIN32\WINGDI.H</TT>. One

consequence of using macros is that the functions are not type safe. You can pass a char * to

the macros, and they won't warn you that what you are doing doesn't make much sense.

</P>                                              



</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

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