VC implementation in Listbox Select background bitmap

xiaoxiao2021-03-06  62

VC implementation Select background bitmap in ListBox Description How to add a replacement background bitmap in ListBox and provide source code for reference. This article source code running renderings are as follows:

First, the basic principle:

Construct a ListBox subclass CMylistbox, when refreshing the back, select the background bitmap. The background mode needs to be set to transparency. When you write the text, the back of the text will not change white, but a background color; the brush must also be empty, so after each refresh, the background is will not change.

It is particularly important to note that when your Listbox has a scroll bar, you need to reset the size of the sizes of the backrest map, minus the corresponding scroll strip area, otherwise, the base map covers the scroll bar.

Second, realize code

Hereinafter, a few overload functions of the CMYListbox class:

// Refresh when you choose ITEM changes;

Void cmylistbox :: onselchange () {ielectchange = true;

Void CMYListBox :: ONLBUTTONDOWN (Uint Nflags, cpoint point) {if (iSelectchange) {LectChange = false; invalidate ();} clistbox :: online;}

// Setting the brush, background mode, text color hbrush cmylistbox :: CTLCOLOR (CDC * PDC, uint nctlcolor) {PDC-> setBkmode (transparent); // Set background mode transparent PDC-> setTextColor (RGB (64, 32, 0)); // Set the text color; Return M_Brhollow; // Set hollow painting;} // Refresh when rolling vertically; Void CMylistbox :: ONVScroll (uint nsbcode, uint npos, cscrollbar * pscrollbar) {invalidate (); CListBox :: OnVScroll (nSBCode, nPos, pScrollBar);} // refresh horizontal scrolling; void CMyListBox :: OnHScroll (UINT nSBCode, UINT nPos, CScrollBar * pScrollBar) {Invalidate (); CListBox :: OnHScroll (nSBCode, nPos , pscrollbar;

// bit background picture out; BOOL CMyListBox :: OnEraseBkgnd (CDC * pDC) {BITMAP bm; m_bmp.GetBitmap (& bm); m_pbmCurrent = & m_bmp; CDC dcMem; dcMem.CreateCompatibleDC (pDC); CBitmap * pOldBitmap = dcMem.SelectObject (m_pbmcurrent); PDC-> Bitblt (0, 0, bm.bmwidth, bm.bmheight, & dcmem, 0, 0, srcopy); DCMem.selectObject (PoldbitMap); Return Clistbox :: OneRaseBkGnd (PDC);

For the size of the background bitmap, we first judge the size of the area before.

M_ListBox.getClientRect (LPRECT);

Then determine if the background bitmap size is required, you can call the hbitmap cmylistboxdlg :: getSizeBitmap (hbitmap hbitmap, int w, int h) function, which can reduce your HbitMap to the appropriate size (W, H). Some other functions are not described. Description, some people may be overloaded by the ListBox's DrawItem function, the principle of this method is different, each Item is drawn, not as a whole, so it may be slower when refresh, but maybe The jitter of the program interface will be less.

I have already sorted out, it may also be partially optimized. If you are interested, you can leave a message below.

转载请注明原文地址:https://www.9cbs.com/read-85186.html

New Post(0)