2

我使用以下代码在 Windows7 上实现一个没有框架和标题栏的窗口:

setWindowFlags(Qt::FramelessWindowHint);

我继续处理鼠标事件,使其可以拖动移动。但是还是有一些问题让我很困惑:

  1. 当我单击任务栏图标时,该应用程序无法像其他应用程序一样最小化或恢复。
  2. 窗户没有阴影

谁能帮我解决这个问题?

4

1 回答 1

1

可以使用DWM添加无框窗口上的阴影。虽然这似乎是半透明无框窗户的问题。

#include <windows.h>
#include <dwmapi.h>

extern "C"
{
    typedef HRESULT (WINAPI *t_DwmSetWindowAttribute)(HWND hwnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute);
    typedef HRESULT (WINAPI *t_DwmExtendFrameIntoClientArea)(HWND hwnd, const MARGINS *pMarInset);
}

void DwmSetWindowAttribute(HWND hwnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute)
{
    HMODULE shell;
    shell = LoadLibrary(L"dwmapi.dll");
    if(shell){
        t_DwmSetWindowAttribute set_window_attribute = reinterpret_cast<t_DwmSetWindowAttribute>(GetProcAddress (shell, "DwmSetWindowAttribute"));
        set_window_attribute(hwnd, dwAttribute, pvAttribute, cbAttribute);

        FreeLibrary (shell);
    }
}

void DwmExtendFrameIntoClientArea(HWND hwnd, const MARGINS *pMarInset){
    HMODULE shell;
    shell = LoadLibrary(L"dwmapi.dll");
    if(shell){
        t_DwmExtendFrameIntoClientArea extend_frame_into_client_area = reinterpret_cast<t_DwmExtendFrameIntoClientArea>(GetProcAddress(shell, "DwmExtendFrameIntoClientArea"));
        extend_frame_into_client_area(hwnd, pMarInset);

        FreeLibrary(shell);
    }
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::FramelessWindowHint);

    DWMNCRENDERINGPOLICY ncrp = DWMNCRP_ENABLED;
    DwmSetWindowAttribute((HWND)this->winId(), DWMWA_NCRENDERING_POLICY, &ncrp, sizeof(ncrp));
    const MARGINS margins = {-1, -1, -1, -1};
    DwmExtendFrameIntoClientArea((HWND)this->winId(), &margins);
}
于 2013-08-10T00:41:03.207 回答