PDA

View Full Version : Vista/Win7 UAC Shield Icon on QPushButton?



ChrisW67
28th April 2010, 08:13
With a standard Windows app, linked to Common Controls 6, you do:

GetDlgItem(IDC_SOMEBUTTONID)->SendMessage(BCM_SETSHIELD, 0, TRUE); and the appropriately coloured shield icon is added to your button.

Is there a way to get the "official" Windows UAC shield icon on to a QPushButton?
Is shipping an icon for each of Vista and Win 7 (different colours) and manually applying it the only way with Qt?

wysota
29th April 2010, 10:14
Did you try using the above call with QPushButton? You would have to get the winID() of the button, then convert it to Windows handle to have something you can send a message to and then send the message.

ChrisW67
30th April 2010, 07:38
Yes I had, but I just went back and tried again (I initially missed the second step). I've added this incantation to the Windows application manifest:

...
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
... and this snippet at the front of the application main

INITCOMMONCONTROLSEX iccex;
iccex.dwSize = sizeof(iccex);
iccex.dwICC = ICC_STANDARD_CLASSES;
BOOL ret = ::InitCommonControlsEx(&iccex);
if (ret != 0)
qDebug() << "::InitCommonControlsEx() success";
and this in the constructor for the form:

static const UINT BCM_SETSHIELD = 0x160C; //Elevated button

WId id = widget->winId();
(void) ::SendMessage(id, BCM_SETSHIELD, 0, TRUE);
}

and still no shield icon (Win7). I am guessing that Qt is drawing the control without reference to Windows; but it just a guess.

At the moment it looks like my best option is to load the shield icon resource from the Windows file that contains it and try to get it into a QIcon (via a QPixmap perhaps). Haven't the time to try this right now though.

wysota
30th April 2010, 08:06
I was hoping that handling this message was done by Windows and not by the button object but it looks like it's not the case.

ChrisW67
10th May 2010, 09:14
For future reference the answer was lurking in QStyle:


QIcon icon = QApplication::style()->standardIcon(QStyle::SP_VistaShield);
QPixmap pixmap = icon.pixmap(32, 32, QIcon::Normal, QIcon::On);

QLabel label;
label.setPixmap(pixmap);
The icon is null on non-Vista/7 systems.