DEV Community

海前 王
海前 王

Posted on

透明窗体拖动

include // MFC 核心和标准组件

include // Windows API 函数

class CMyFrameWnd : public CFrameWnd {
public:
CMyFrameWnd() {
Create(NULL, _T("Transparent Draggable Window"), WS_POPUP, CRect(0, 0, 300, 200));
}

protected:
virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) override {
return TRUE; // 不创建子窗口
}

afx_msg void OnPaint() {
    CPaintDC dc(this); // device context for painting
    // 在这里可以进行绘图操作
}

afx_msg LRESULT OnNcHitTest(CPoint point) {
    LRESULT nHitTest = CFrameWnd::OnNcHitTest(point);
    return (nHitTest == HTCLIENT) ? HTCAPTION : nHitTest;
}

afx_msg void OnLButtonDown(UINT nFlags, CPoint point) {
    CFrameWnd::OnLButtonDown(nFlags, point);
    PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x, point.y));
}

afx_msg void OnSize(UINT nType, int cx, int cy) {
    CFrameWnd::OnSize(nType, cx, cy);
    Invalidate(); // 使窗口重新绘制
}

DECLARE_MESSAGE_MAP()
Enter fullscreen mode Exit fullscreen mode

};

BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd)
ON_WM_PAINT()
ON_WM_NCHITTEST()
ON_WM_LBUTTONDOWN()
ON_WM_SIZE()
END_MESSAGE_MAP()

class CMyApp : public CWinApp {
public:
virtual BOOL InitInstance() {
CMyFrameWnd* pFrame = new CMyFrameWnd;
m_pMainWnd = pFrame;

    pFrame->ShowWindow(SW_SHOW);
    pFrame->UpdateWindow();

    return TRUE;
}
Enter fullscreen mode Exit fullscreen mode

};

CMyApp theApp;

Top comments (0)