Je suis en train de développer une appli avec une fenêtre dont le cadre est personnalisé, comme le sont par exemple office, Opera, Chrome, avec la transparence Aero.

J'ai réussi à créer une telle fenêtre, voir la pièce jointe.

Le problème est que quand la fenêtre est maximisée : la transparence disparait, et je me retrouve avec du noir à la place. De plus, les bordures dépassent.
J'ai fait un screen mais ce n'est pas super-visible

Comment puis-je faire pour rétablir la transparence lorsque la fenêtre est maximisée ?

Code source :

AMainWindow.h
Code c++ : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
#ifndef ___AMainWindow
#define ___AMainWindow
 
#include <QtGui/QWidget>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#ifdef Q_OS_WIN
#include "AWindowsHelper.h"
#endif
 
class AMainWindow : public QWidget {
    Q_OBJECT
 
public:
    AMainWindow(QWidget* = 0);
 
private:
	QLabel* windowTitle;
	QPushButton* systemMenu;
 
#ifdef Q_OS_WIN
private:
	AWindowsHelper* wHelper;
 
protected:
	bool winEvent(MSG*, long*);
	void paintEvent(QPaintEvent*);
#endif
};
 
#endif

AWindowsHelper.h
Code c++ : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 
#ifndef ___AWindowsHelper
#define ___AWindowsHelper
 
#include <QtGui/QWidget>
#include <windows.h>
#include <windowsx.h>
 
class AWindowsHelper : public QObject {
    Q_OBJECT
 
public:
    AWindowsHelper(QObject* = 0);
 
	enum AeroState {
		AeroUnaviable,
		AeroUnaviableThemed,
		AeroClassic,
		AeroBasic,
		AeroAviable
	};
 
	bool winEvent(MSG*, long*);
	void paintEvent(QPaintEvent*);
	AeroState aeroState();
 
private:
	typedef struct {
		int cxLeftWidth;
		int cxRightWidth;
		int cyTopHeight;
		int cyBottomHeight;
	} WMargins;
 
	typedef BOOL (WINAPI *T_IsAppThemed)(void);
	typedef HRESULT (WINAPI *T_DwmIsCompositionEnabled)(BOOL* pfEnabled);
	typedef BOOL (WINAPI *T_DwmDefWindowProc)(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT* plResult);
	typedef HRESULT (WINAPI *T_DwmExtendFrameIntoClientArea)(HWND hwnd, WMargins* pMarInset);
 
	T_IsAppThemed F_IsAppThemed;
	T_DwmIsCompositionEnabled F_DwmIsCompositionEnabled;
	T_DwmDefWindowProc F_DwmDefWindowProc;
	T_DwmExtendFrameIntoClientArea F_DwmExtendFrameIntoClientArea;
 
	AeroState cachedState;
	QWidget* w;
	bool dirty;
};
 
#endif

AMainWindow.cc
Code c++ : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
#include "AMainWindow.h"
#include <QtGui/QStyle>
 
AMainWindow::AMainWindow(QWidget* parent) : QWidget(parent) {
	setWindowTitle("Crash Test");
	setWindowFlags(Qt::Window);
#ifdef Q_OS_WIN
	wHelper = new AWindowsHelper(this);
#endif
 
	QLabel* windowTitle = new QLabel(this);
	windowTitle->setText(QString::fromUtf8("<b>Qt custom frame</b> - Crash test"));
 
	int frameWidth = style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth);
	int titleHeight = style()->pixelMetric(QStyle::PM_TitleBarHeight);
	windowTitle->setGeometry(frameWidth + 20, 0, width() - 2 * frameWidth - 20, titleHeight);
	windowTitle->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
 
	systemMenu = new QPushButton(this);
	systemMenu->setGeometry(frameWidth, frameWidth, 16, titleHeight - 2 * frameWidth);
}
 
#ifdef Q_OS_WIN
bool AMainWindow::winEvent(MSG* m, long* l) {
	return wHelper->winEvent(m, l);
}
 
void AMainWindow::paintEvent(QPaintEvent* e) {
	wHelper->paintEvent(e);
}
#endif

AWindowsHelper.cc
Code c++ : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
 
#include "AWindowsHelper.h"
#include <QtCore/QLibrary>
#include <QtCore/QSysInfo>
#include <QtGui/QStyle>
#include <QtGui/QApplication>
#include <QtGui/QLabel>
#include <QtGui/QPainter>
#include "AMainWindow.h"
 
AWindowsHelper::AWindowsHelper(QObject* parent) : QObject(parent) {
	// Loads library symbols
	cachedState = AeroUnaviable;
	dirty = true;
 
	if(QSysInfo::WindowsVersion >= QSysInfo::WV_VISTA) cachedState = AeroClassic;
 
	if(QSysInfo::WindowsVersion <= QSysInfo::WV_XP) {
		// Version older than XP doesn't support anything.
		return;
	}
 
	// UxTheme : XP and later
	QLibrary uxLib(QString::fromUtf8("uxtheme"));
 
	F_IsAppThemed = (T_IsAppThemed)uxLib.resolve("IsAppThemed");
	if(F_IsAppThemed == 0) return;
	cachedState = F_IsAppThemed() == TRUE ? AeroUnaviableThemed : AeroUnaviable;
 
	if(QSysInfo::WindowsVersion == QSysInfo::WV_XP || QSysInfo::WindowsVersion == QSysInfo::WV_2003) return;
 
	// DwmApi : Vista and later
	QLibrary dwmLib(QString::fromUtf8("dwmapi"));
 
	F_DwmIsCompositionEnabled = (T_DwmIsCompositionEnabled)dwmLib.resolve("DwmIsCompositionEnabled");
	F_DwmDefWindowProc = (T_DwmDefWindowProc)dwmLib.resolve("DwmDefWindowProc");
	F_DwmExtendFrameIntoClientArea = (T_DwmExtendFrameIntoClientArea)dwmLib.resolve("DwmExtendFrameIntoClientArea");
 
	w = qobject_cast<QWidget*>(parent);
	if(w == 0) return;
 
	w->setAttribute(Qt::WA_TranslucentBackground);
	w->setAttribute(Qt::WA_NativeWindow);
}
 
bool AWindowsHelper::winEvent(MSG* msg, long* result) {
	bool fCallDwp = true;
	LRESULT lRet = 0;
	HWND hWnd = w->winId();
	UINT message = msg->message;
 
	fCallDwp = !F_DwmDefWindowProc(hWnd, message, msg->wParam, msg->lParam, &lRet);
 
	if(message != WM_ACTIVATE && message != WM_NCCALCSIZE && message != WM_NCHITTEST && message != WM_PAINT) return false;
 
	if(message == WM_ACTIVATE) {
		RECT rcClient;
		GetWindowRect(hWnd, &rcClient);
 
		SetWindowPos(hWnd, NULL, rcClient.left, rcClient.top,
			rcClient.right - rcClient.left,
			rcClient.bottom - rcClient.top,
			SWP_FRAMECHANGED
			);
 
		int frameWidth = w->style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth);
 
		WMargins mar;
		mar.cxLeftWidth = frameWidth;
		mar.cxRightWidth = frameWidth;
		mar.cyBottomHeight = frameWidth;
		mar.cyTopHeight = w->style()->pixelMetric(QStyle::PM_TitleBarHeight);
 
		F_DwmExtendFrameIntoClientArea(hWnd, &mar);
 
		fCallDwp = true;
		lRet = 0;
	}
 
	if(message == WM_PAINT) {
		w->update();
 
		fCallDwp = true;
		lRet = 0;
	}
 
	if((message == WM_NCCALCSIZE) && (msg->wParam == TRUE)) {
		NCCALCSIZE_PARAMS* ncsize = reinterpret_cast<NCCALCSIZE_PARAMS*>(msg->lParam);
 
		ncsize->rgrc[0].left += 0;
		ncsize->rgrc[0].top += 0;
		ncsize->rgrc[0].right -= 0;
		ncsize->rgrc[0].bottom -= 0;
 
		lRet = 0;
		fCallDwp = false;
	}
 
	if((message == WM_NCHITTEST) && (lRet == 0)) {
		LRESULT lDwmRet = lRet;
		//F_DwmDefWindowProc(hWnd, message, msg->wParam, msg->lParam, &lDwmRet);
 
		if(lDwmRet != HTCLOSE &&
			lDwmRet != HTMINBUTTON &&
			lDwmRet != HTMAXBUTTON &&
			lDwmRet != HTHELP) {
 
			POINT hitPoint = { GET_X_LPARAM(msg->lParam), GET_Y_LPARAM(msg->lParam) };
			RECT rcWindow;
			GetWindowRect(hWnd, &rcWindow);
 
			int frameWidth = w->style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth);
 
			short xPos = 1;
			short yPos = 2;
 
			if(hitPoint.x > rcWindow.left && hitPoint.x < (rcWindow.left + frameWidth)) {
				xPos = 0;
			}
 
			if(hitPoint.x < rcWindow.right && hitPoint.x > (rcWindow.right - frameWidth)) {
				xPos = 2;
			}
 
			if(hitPoint.y > rcWindow.top && hitPoint.y < (rcWindow.top + frameWidth)) {
				yPos = 0;
			}
 
			if(hitPoint.y > (rcWindow.top + frameWidth) && hitPoint.y < (rcWindow.top + w->style()->pixelMetric(QStyle::PM_TitleBarHeight))) {
				yPos = 1;
			}
 
			if(hitPoint.y < rcWindow.bottom && hitPoint.y > (rcWindow.bottom - frameWidth)) {
				yPos = 3;
			}
 
			if(xPos == 0 && yPos == 0) lRet = HTTOPLEFT;
			else if(xPos == 1 && yPos == 0) lRet = HTTOP;
			else if(xPos == 2 && yPos == 0) lRet = HTTOPRIGHT;
			else if(xPos == 0 && yPos == 1) lRet = HTLEFT;
			else if(xPos == 1 && yPos == 1) lRet = HTCAPTION;
			else if(xPos == 2 && yPos == 1) lRet = HTRIGHT;
			else if(xPos == 0 && yPos == 2) lRet = HTLEFT;
			else if(xPos == 1 && yPos == 2) lRet = HTCLIENT;
			else if(xPos == 2 && yPos == 2) lRet = HTRIGHT;
			else if(xPos == 0 && yPos == 3) lRet = HTBOTTOMLEFT;
			else if(xPos == 1 && yPos == 3) lRet = HTBOTTOM;
			else if(xPos == 2 && yPos == 3) lRet = HTBOTTOMRIGHT;
 
			QWidget* wAt = QApplication::widgetAt(hitPoint.x, hitPoint.y);
			if(wAt != 0 && wAt != w && qobject_cast<QLabel*>(wAt) == 0) {
				lRet = HTCLIENT;
			}
 
			fCallDwp = false;
		}
	}
 
	*result = lRet;
	return !fCallDwp;
}
 
void AWindowsHelper::paintEvent(QPaintEvent*) {
	QPainter p(w);
 
	p.setPen(Qt::NoPen);
	p.setBrush(QApplication::palette().background());
 
	int frameWidth = w->style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth);
 
	p.drawRect(frameWidth, w->style()->pixelMetric(QStyle::PM_TitleBarHeight), w->width() - 2 * frameWidth,
		w->height() - frameWidth - w->style()->pixelMetric(QStyle::PM_TitleBarHeight));
}

Merci d'avance

[EDIT]
Chose étonnante, le cadre transparent maximisé fonctionne si aucun widget n'est placé sur ma fenêtre (ce qui n'est pas trop mon interêt...)

[EDIT]
Les pièces jointes (devenues inutiles) ont été supprimées.