SetBkMode function Understanding

Source: Internet
Author: User


When outputting a string, it is found that the background color of the output string differs from the color of the control, so that the output is damaging to the background. What do you need to do to keep the background constant and to output a red string? For example, the text color of the button is black, and the background is gray. This requires the use of the SetBkMode function to set the output mode of the DrawText function, showing a total of two output modes of the device: opaque and transparent. Opaque's way is to use the current background of the brush color output display text background, and transparent is the use of transparent output, that is, the text background is not changed.



Wingdiapi int WINAPI setbkmode (__in hdc hdc, __in int mode);
HDCis a handle to the current device.
ModeIs the mode to set.
The function return value is the pattern that was previously set.

An example of calling this function is as follows:

void Ccaiwinmsg::ondraw (HDC hdc)
{
//
Std::wstring strshow (_t ("Implementation of the C + + window class, 2007-08-27"));

Sets the color of the output string.
COLORREF crold = SetTextColor (Hdc,rgb (255,0,0));

RECT Rctext;
Rctext.left = 10;
Rctext.top = 10;
Rctext.right = 300;
Rctext.bottom = 80;

Create a black paint brush,
Hbrush hbrush = CreateSolidBrush (RGB (0, 0, 0));

Fill the four-sided color with a black paint brush.
FillRect (Hdc,&rctext,hbrush);

Remove the brush.
DeleteObject (Hbrush);


Rctext.left = 10;
Rctext.top = 10;
Rctext.right = 300;
Rctext.bottom = 40;

Displays the string in the middle of the quad.
DrawText (Hdc,strshow.c_str (), (int) strshow.length (), &rctext,
dt_center| dt_vcenter| dt_singleline| Dt_end_ellipsis);

Rctext.left = 10;
Rctext.top = 40;
Rctext.right = 300;
Rctext.bottom = 80;
Set Transparent Background
int noldmode = SetBkMode (hdc,transparent);

Displays the string in the middle of the quad.
DrawText (Hdc,strshow.c_str (), (int) strshow.length (), &rctext,
dt_center| dt_vcenter| dt_singleline| Dt_end_ellipsis);

Restore the original mode.
SetBkMode (Hdc,noldmode);

Restores the original color.
SetTextColor (Hdc,crold);
}


Sets the transparent background.
/* Transparent Understanding: A string or graphic that is output from a form or control has a background color. The form or control also has a background color. These two background colors are generally different,
When you export a graphic or string, the two backgrounds (the background color of the form or control, the string of characters, or the background color of the graphic) overlap, affecting aesthetics, in order not to let the two
Background overlap, you can set the background mode to transparent (transparent), that is, the output of the string or graphics background color is not visible. It can be understood that: the output of the
The background color of a string or graphic is considered an object, a form or control is considered another object, and a string or graphic blocks the form or control before the form or control (because the output
A string or graphic is drawn on a form or control, so you can do so, but you can see the form or control through a string or graphic. That is, the back of a string or graphic
The object that the scene represents is transparent.


*/



The first display is displayed using the opaque method.
The second display is displayed using the transparent method.
The effect diagram of this program is as follows

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.