我有一个继承自 的类CStatic
,它是所有者绘制并计算窗口应使用的区域SetWindowRgn
。在我的DrawItem
覆盖中,我尝试使用FillRgn
与计算相同的区域传递,但该方法返回 FALSE。文档说_
返回值:如果函数成功,则非零;否则为 0。
但是当我GetLastError
的消息是“操作成功完成”时。
窗口的区域肯定会很好,因为我已经设法接收WM_LBUTTONUP
消息。
这是我的课:
.h 文件
class Country : public CStatic
{
DECLARE_DYNAMIC(Country)
public:
Country(HBITMAP hBm, CPoint point);
virtual ~Country();
protected:
DECLARE_MESSAGE_MAP()
CPoint m_point;
HBITMAP m_hBm;
BITMAP m_bm;
HDC m_dcBm;
HRGN m_hRgn;
void CreateWindowRgn();
public:
void CreateCountryWindow(CWnd * pParent);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
};
.cpp
void Country::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRgn * clientRgn = CRgn::FromHandle(m_hRgn);
CBrush blackBrush(RGB(0,0,0));
if(!(pDC->FillRgn(clientRgn, &blackBrush)))
{
AfxMessageBox(GetLastErrorMessage());
}
}
void Country::CreateCountryWindow(CWnd * pParent)
{
if(Create(
_T(""),
WS_CHILD | WS_VISIBLE | SS_NOTIFY | SS_OWNERDRAW,
CRect(m_point.x, QUESTION_PANEL_HEIGHT + m_point.y, m_bm.bmWidth, m_bm.bmHeight),
pParent))
{
ShowWindow(SW_SHOW);
CreateWindowRgn();
}
}
void Country::CreateWindowRgn()
{
HRGN hTmpRgn;
m_hRgn = CreateRectRgn(0,0,m_bm.bmWidth, m_bm.bmHeight);
for(int i = 0; i < m_bm.bmWidth; i++)
{
for(int j = 0; j < m_bm.bmHeight; j++)
{
COLORREF c = GetPixel(m_dcBm,i,j);
if(GetRValue(c) < 10 && GetGValue(c) < 10 && GetBValue(c) < 10)
{
hTmpRgn = CreateRectRgn(i, j, i+1, j+1);
CombineRgn(m_hRgn, m_hRgn, hTmpRgn, RGN_XOR);
DeleteObject(hTmpRgn);
}
}
}
SetWindowRgn(m_hRgn, TRUE);
}
void Country::OnLButtonUp(UINT nFlags, CPoint point)
{
AfxMessageBox(_T("Hello World"));
CStatic::OnLButtonUp(nFlags, point);
}