One-day Windows API training (27) setbkmode Function

Source: Internet
Author: User
Tags drawtext
When the red string is output, the black color of the background turns white, which destroys the background. So what method should I use to keep the background unchanged and output a red string? For example, the text color of the button is black and the background is gray. In this case, you need to use the setbkmode function to set the output mode of the drawtext function. There are two output modes for the display device: opaque and transparent. Opaque is used to display the text background with the color of the current background, while transparent uses transparent output, that is, the text background remains unchanged.

The setbkmode function declaration is as follows:
Wingdiapi int winapi setbkmode (_ in HDC, _ in int mode );
HDCIs the handle of the current device.
ModeIs the mode to be set.
The function return value is the previous set mode.

An example of calling this function is as follows:
#001 //
#002 // The output is displayed on the interface.
#003 //
#004 // Cai junsheng 2007/09/01 QQ: 9073204 Shenzhen
#005 //
#006 void ccaiwinmsg: ondraw (HDC)
#007 {
#008 //
#009 STD: wstring strshow (_ T ("Implementation of the C ++ window class, 2007-08-27 "));
#010
#011 // set the color of the output string.
#012 colorref crold = settextcolor (HDC, RGB (255, 0, 0 ));
#013
#014 rect rctext;
#015 rctext. Left = 10;
#016 rctext. Top = 10;
#017 rctext. Right = 300;
#018 rctext. Bottom = 80;
#019
#020 // create a black paint brush,
#021 hbrush = createsolidbrush (RGB (0, 0, 0 ));
#022
#023 // fill the Quadrilateral color with a black paint brush.
#024 fillrect (HDC, & rctext, hbrush );
#025
#026 // Delete the painter.
#027 deleteobject (hbrush );
#028
#029
#030 rctext. Left = 10;
#031 rctext. Top = 10;
#032 rctext. Right = 300;
#033 rctext. Bottom = 40;
#034
#035 // display the middle position of the string in the Quadrilateral.
#036 drawtext (HDC, strshow. c_str (), (INT) strshow. Length (), & rctext,
#037 dt_center | dt_vcenter | dt_singleline | dt_end_ellipsis );
#038
#039
#040 rctext. Left = 10;
#041 rctext. Top = 40;
#042 rctext. Right = 300;
#043 rctext. Bottom = 80;
#044 // set transparent background
#045 int noldmode = setbkmode (HDC, transparent );
#046
#047 // display the middle position of the string in the Quadrilateral.
#048 drawtext (HDC, strshow. c_str (), (INT) strshow. Length (), & rctext,
#049 dt_center | dt_vcenter | dt_singleline | dt_end_ellipsis );
#050
#051 // restore the original mode.
#052 setbkmode (HDC, noldmode );
#053
#054 // restore the original color.
#055 settextcolor (HDC, crold );
#056}

The program is running as follows:

Opaque is used for the first display.
The second display is displayed in transparent mode.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.