C/C++ 实现遮罩窗口

版权声明:转载请声明出处,谢谢~ https://blog.csdn.net/what951006/article/details/81666637

为什么会有这种需求?一般做做个的话是要使用模态对话框,模态对话框的父窗口会生成一个遮罩窗口,这就是用处,不让点击,可能就是这个一个简单的需求,我们要怎么样来实现?
一个遮罩窗口的实现代码
.h文件

class MaskWindow : public QObject//we must use 'QWidget 'as base class
{
    Q_OBJECT
public:
    explicit MaskWindow(QWidget *parent = nullptr);

    void SetModelDialog(BaseDialog * pWidget);

    void SetBGAlpha(unsigned char degrees);

    void CloseWindow();

//    virtual void mousePressEvent(QMouseEvent *event) override;

//  virtual void paintEvent(QPaintEvent *event) override;

//  virtual void MyPaintEvent(QPainter *p) override;
signals:
    void sig_ActiveWindow();

private:
    BaseDialog *m_pDlg;
    unsigned char  m_bTransColor;
public:
    QWidget *m_pParent;
    HWND m_hwnd;
};

.cpp文件


wchar_t g_szClassName[] = L"YUIEngine";


LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
    case WM_CREATE:
    {
        ::SetLayeredWindowAttributes(hwnd, RGB(255, 255, 255), 80, LWA_ALPHA);
    }
    break;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


MaskWindow::MaskWindow(QWidget *pWid)
    :QObject(pWid)
    , m_pDlg(nullptr)
    , m_bTransColor(125)
    , m_pParent(pWid)
{

    WNDCLASSEX wincl;        /* Data structure for the windowclass */

                             /* The Window structure */
    wincl.hInstance = GetModuleHandle(NULL);
    wincl.lpszClassName = g_szClassName;//+-69+ 
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof(WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
                                               /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    /* Register the window class, and if it fails quit the program */
    RegisterClassEx(&wincl);
    /* The class is registered, let's create the program*/
    m_hwnd = CreateWindowEx(
        WS_EX_LAYERED|WS_EX_TRANSPARENT,                   /* Extended possibilites for variation */
        g_szClassName,         /* Classname */
        L"MaskWindow",       /* Title Text */
        WS_POPUP, /* default window */
        pWid->x(),       /* Windows decides the position */
        pWid->y(),       /* where the window ends up on the screen */
        pWid->width(),                 /* The programs width */
        pWid->height(),                 /* and height in pixels */
        (HWND)pWid->winId(),        /* The window is a child-window to desktop */
        NULL,                /* No menu */
        GetModuleHandle(NULL),       /* Program Instance handler */
        NULL                 /* No Window Creation data */
    );
    if(pWid->isHidden())
        ShowWindow(m_hwnd, SW_HIDE);
    else
        ShowWindow(m_hwnd, SW_SHOW);

    //EnableWindow(m_hwnd, false);

}


void MaskWindow::SetModelDialog(BaseDialog * pWidget)
{
    m_pDlg = pWidget;
}

void MaskWindow::SetBGAlpha(unsigned char degrees)
{
    m_bTransColor = degrees;

}

void MaskWindow::CloseWindow()
{
    EnableWindow(m_hwnd, true);
    DestroyWindow(m_hwnd);
}

如这种背景hover效果
这里写图片描述
有兴趣可以自己测试一下。
更多文章:https://blog.csdn.net/what951006
powered by:小乌龟在大乌龟背上~

猜你喜欢

转载自blog.csdn.net/what951006/article/details/81666637