如何在 Mfc C++ 中创建透明矩形?

How to create transparent Rectangle in Mfc c++?

我想创建一个完全透明的矩形我试试这个代码。知道怎么做吗?

 BOOL CHtmlDlgTestDlg::PreTranslateMessage(MSG* pMsg)
    {

        if (pMsg->message == WM_MOUSEMOVE && (pMsg->wParam & MK_LBUTTON))
        {
            CPoint p = pMsg->pt;
            ScreenToClient(&p);
            CRect r(10, 15, 380, 50);
            CDC* pCDC = GetDC();
            pCDC->Rectangle(r);
            CBrush brush;


            brush.CreateSolidBrush(RGB(255, 255, 0));
            pCDC->FillRect(&r, &brush);



            if (r.PtInRect(p))
            {
                ReleaseCapture();
                SendMessage(WM_NCLBUTTONDOWN, HTCAPTION, 0);
                SendMessage(WM_NCLBUTTONUP, HTCAPTION, 0);
                return 1;
            }

        }
        return CDHtmlDialog::PreTranslateMessage(pMsg);
    }

这是 mfc c++ 代码示例。

首先:如果你创建了一个实心画笔,然后用它做了 FillRect,你到底想怎么创建一个透明矩形?

如果你想用刷子做到这一点,你必须使用一个 "that not paints anything" 比如:

brush.CreateStockObject(NULL_BRUSH);

可能您想要一个具有不透明边框和透明内部的矩形。那样的话,除了空画笔,你需要创建的对象就是一个CPen。

CPen p(PS_SOLID, 0, RGB(255, 255, 0));
pCDC->SelectObject(&p);
pCDC->Rectangle(r);

其次,如果您不关心边框颜色(颜色是固定的;在 Google 图像中搜索 DrawEdge 以查看我的内容,我认为 DrawEdge 函数是最简单的方法均值)。