如何在C ++中为win32控件绘制边框

时间:2012-01-24 20:22:06

标签: c++ winapi

我想在C ++ win32 api中为所有者绘制一个红色边框到EDIT或Push按钮。没有MFC请。 我已经走到了这一步。绘制一个黑色边框,但大多数(如果不是全部)hButtonDC,hButtonBitmap都是未声明的。

PAINTSTRUCT ps;
HDC hdc;
HBRUSH hBrush;
BeginPaint(hwndButton2, &ps);

//  Create memory DC to contain hButtonBitmap

hButtonDC = CreateCompatibleDC(ps.hdc);
hButtonBitmap = SelectObject(hButtonDC, hButtonBitmap);

//  Create second memory DC where the button borders will be drawn and select into this DC an empty bitmap with the
//  size of the button bitmap

hMemDC = CreateCompatibleDC(ps.hdc);
hBitmap = CreateCompatibleBitmap(ps.hdc, ps.rcPaint.right, ps.rcPaint.bottom);
hBitmap = SelectObject(hMemDC, hBitmap);

//  Copy hButtonDC into hMemDC

BitBlt(hMemDC, 0, 0,  ps.rcPaint.right, ps.rcPaint.bottom, hButtonDC, 0, 0, SRCCOPY);

//  Paint the button borders with black pixels (1 pixel width)

PatBlt(hMemDC, 0, 0, ps.rcPaint.right - 1, 1, BLACKNESS);
PatBlt(hMemDC, ps.rcPaint.right - 1, 0, 1, ps.rcPaint.bottom, BLACKNESS);
PatBlt(hMemDC,  0, ps.rcPaint.bottom - 1, ps.rcPaint.right , 1, BLACKNESS);
PatBlt(hMemDC, 0, 0, 1, ps.rcPaint.bottom - 1, BLACKNESS);

//  Paint the button with drawn borders to its window DC, ps.hdc .

BitBlt(ps.hdc, 0, 0, ps.rcPaint.right, ps.rcPaint.bottom, hMemDC, 0, 0, SRCCOPY); 

//  Delete hBitmap e hMemDC

DeleteObject(SelectObject(hMemDC, hBitmap));
DeleteDC(hMemDC);

//  Delete hButtonDC

SelectObject(hButtonDC, hButtonBitmap);
DeleteDC(hButtonDC);

EndPaint(hWnd, &ps);

0 个答案:

没有答案