One-day Windows API training (29) SelectObject and deleteobject Functions

Source: Internet
Author: User
Tags drawtext
Properties of Windows display devices: bitmap, paint brush, Font, paint brush, and area. If you want to set them to the current device, you need to use the SelectObject function, such as the font settings described above, this function will be used. When you create a bitmap, Windows allocates a memory space in the memory to save the bitmap data. When you create a font, a memory space will be allocated to save the font. If the program is only allocated, rather than deleted, it will cause more and more memory usage and finally be exported to the Windows building. If you forget to delete it, memory leakage occurs. Therefore, when creating display device resources, remember to delete them. Otherwise, the longer your program runs, the unstable system. Remember to use the deleteobject function to delete them and release the occupied memory to the system.

The SelectObject and deleteobject functions are declared as follows:

Wingdiapi hgdiobj winapi SelectObject (_ in HDC, _ in hgdiobj H );
Wingdiapi bool winapi deleteobject (_ in hgdiobj Ho );
HDCIs the handle of the current device.
H,HoIt is a device object. In fact, it is the memory address.

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-09-04 "));
#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 // set the new font.
#048 hgdiobj holdfont = SelectObject (HDC, getfont ());
#049
#050 // display the string in the middle of the Quadrilateral.
#051 drawtext (HDC, strshow. c_str (), (INT) strshow. Length (), & rctext,
#052 dt_center | dt_vcenter | dt_singleline | dt_end_ellipsis );
#053
#054 // restore the original font.
#055 hgdiobj hfont = SelectObject (HDC, holdfont );
#056 deleteobject (hfont );
#057
#058 // restore the original mode.
#059 setbkmode (HDC, noldmode );
#060
#061 // restore the original color.
#062 settextcolor (HDC, crold );
#063}

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.