Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#11705 closed enhancement (fixed)

Patch to enable adding of the UAC symbol to buttons

Reported by: cspencer Owned by:
Priority: normal Milestone: 3.0.0
Component: wxMSW Version: stable-latest
Keywords: Cc:
Blocked By: Blocking:
Patch: yes


Attached is a patch to enable adding of the UAC symbol to buttons on Windows Vista and later.

A couple of notes:

  1. I wasn't sure what to name the style, I chose wxBU_UAC, but perhaps something more generic would be better?
  2. The symbol is only shown if common controls is enabled in the manifest, but I don't know of any way to detect if this is enabled, so space is always reserved even if it won't be shown. This doesn't strike me as a big deal though as you wouldn't really want to use wxMSW without common controls anyway right?

Attachments (2)

screenshot.png download (10.8 KB) - added by cspencer 8 years ago.
Screenshot showing effect on XP, 7 without CC and 7 with CC.
uac.patch download (7.3 KB) - added by cspencer 8 years ago.

Download all attachments as: .zip

Change History (13)

Changed 8 years ago by cspencer

Screenshot showing effect on XP, 7 without CC and 7 with CC.

comment:1 Changed 8 years ago by vadz

  • Milestone set to 3.0
  • Status changed from new to confirmed

Thanks for the patch, it's nice to have support for Windows versions less than 10 year old :-)

I agree that wxBU_UAC is not a great name though, I'd definitely prefer something more generic. What about wxBU_AUTH_NEEDED? It's still not great but IMHO is better. If you have any better ideas, please use them.

Concerning the patch itself, just a couple of minor remarks:

  • I don't think we use SNDMSG anywhere else so why start here. Just do the usual ::SendMessage(GetHwnd(), ...).
  • Concerning BCM_SETSHIELD being possibly not defined: first, thanks for thinking about this! But IMHO it would be slightly cleaner to add
    #ifndef BCM_SETSHIELD 
        #define BCM_SETSHIELD 0x1c60

to the beginning of the file.

  • Also thanks for updating the docs but could you please also add @since{2.9.1} to the new style documentation? And please feel free to include changes to docs/changes.txt crediting you with this improvement in your patch.


comment:2 Changed 8 years ago by cspencer

Hi Vadim.

  • I have changed it to wxBU_AUTH_NEEDED. It's better than anything I could come up with..
  • I didn't really think about BCM_SETSHIELD being undefined, it's undefined in the mingw headers :) It's also missing from the headers in my copy Visual Studio 2008 weirdly, I had to find the actual value on Google.
  • Changed SNDMSG to SendMessage, I'm not so familiar with the Windows API, I just copied it from the Button_SetElevationRequiredState macro (which is also missing from the headers).
  • I've updated the documentation accordingly.


comment:3 Changed 8 years ago by jatupper

For my applications, I'll want to add / remove the shield on demand. If a flag is the way to go, could wxBU_AUTH_NEEDED be made to work with SetWindowStyleFlag? (There is a button in my app which requires elevation only when other widgets in its window are set a certain way.)

comment:4 Changed 8 years ago by vadz

Good point, thinking more about it I agree that it makes more sense to have wxButton::SetRequiresAuth(bool) (again, better names welcome!) both because it can be turned on and off more easily and also because it could be implemented for the other systems (including previous versions of Windows although it probably wouldn't be necessary there) via wxButton::SetBitmapLabel() an appropriate icon.

So, Chris, sorry to bother you (the updated patch is perfect BTW) but what do you think about replacing this flag with a method?

Thanks again!

comment:5 follow-up: Changed 8 years ago by cspencer

Good idea jatupper, hadn't thought of that. I'll get right on it..

Vadim, I'm not sure I see the advantage in making it a method instead of a flag. A flag seems more appropriate, and it's certainly not difficult to switch it using SetWindowStyle(). And how would it make it easier to implement for other systems?

comment:6 in reply to: ↑ 5 Changed 8 years ago by vadz

Replying to cspencer:

And how would it make it easier to implement for other systems?

In fact it would make it easier to implement for all systems. If you want to be able to trigger the shield icon from SetWindowStyle() you need a function to call from there, e.g. you're going to have something like this

if ( (styleNew & wxBU_AUTH_NEEDED) != (m_style & wxBU_AUTH_NEEDED) )
  DoSetAuthNeeded(!(styleNew & wxBU_AUTH_NEEDED));


And I simply propose to make this DoSetAuthNeeded() public and forget the flag.

The only advantage of a flag I can see is that it could be set from XRC but I'm not sure if this is really important as this will be probably often decided during run-time anyhow.

comment:7 Changed 8 years ago by cspencer

Yeah, didn't consider the unnecessary added complexity to make it toggleable until I started writing it.

I'll submit a new patch shortly.

comment:8 Changed 8 years ago by cspencer

Ok, new patch has functions for switching the UAC symbol on and off. I think I've changed all the files I need to. Let me know if I should be doing anything differently.

Changed 8 years ago by cspencer

comment:9 Changed 8 years ago by VZ

  • Resolution set to fixed
  • Status changed from confirmed to closed

(In [63421]) Add support for showing "elevation" icon in wxMSW buttons.

Add, document and test in the widgets sample wxButton::SetAuthNeeded().

Closes #11705.

comment:10 Changed 8 years ago by vadz

Thanks, committed with minor changes:

  • Added default true parameter to the function.
  • Don't hardcode 16 but use wxSystemSettings::GetMetric(wxSYS_SMALLICON_X).
  • Don't use boolean parameters in the size compuation methods.

Thanks again for implementing this!

comment:11 Changed 8 years ago by cspencer

Great. Thanks Vadim. :)

Note: See TracTickets for help on using tickets.