How to set the administrative law icon on the MFC dialog button?

I am creating an application in MFC using Visual Studio 2008 on Windows 7. My application starts and stops a service that requires administrative access. When the application starts, it does not have administrator access. But when I click the "Start" button, it gets administrative access and performs the action. I am wondering how to set an admin icon on buttons whose actions need administrative access? Do I need to set any flags? Thanks

+5
source share
1 answer

With Windows Vista, you can add a screen icon to a button using one of the new flags. To enable it, there is a macro that you can use as follows:

Button_SetElevationRequiredState(hwnd, TRUE);

The documentation for the macro is at http://msdn.microsoft.com/en-us/library/bb761865%28VS.85%29.aspx

See http://msdn.microsoft.com/en-us/library/bb756990.aspx#BKMK_ShieldButton for an overview of how to perform many UAC-related tasks.

There is also CButton :: SetElevationRequired (), which seems to do the same, but fits more according to your MFC project. You can use it as follows:

ctl->SetElevationRequired(TRUE);

See http://msdn.microsoft.com/en-us/library/bb386824%28v=VS.90%29.aspx

v6 DLL, ( ), #pragma MSVC2005 . , MFC , , , MFC.

, , , ".manifest" .exe, . MyApp.exe.manifest:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
    version="1.0.0.0"
    processorArchitecture="*"
    name="CompanyName.ProductName.YourApplication"
    type="win32"
/>
<description>Your application description here.</description>
<dependency>
    <dependentAssembly>
        <assemblyIdentity
            type="win32"
            name="Microsoft.Windows.Common-Controls"
            version="6.0.0.0"
            processorArchitecture="*"
            publicKeyToken="6595b64144ccf1df"
            language="*"
        />
    </dependentAssembly>
</dependency>
</assembly>

#pragma . .

v6 . ( ): http://msdn.microsoft.com/en-us/library/bb773175%28v=vs.85%29.aspx

win32, pragma v6 :

#include <windows.h>
#include <commctrl.h>
#include <stdio.h>

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE instance,
                    HINSTANCE previnst,
                    LPSTR args,
                    int wndState)
{
    int i;
    MSG messages;
    WNDCLASSEX wincl;
    ZeroMemory(&wincl, sizeof(wincl));
    wincl.hInstance = instance;
    wincl.lpszClassName = L"WindowsApp";
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof wincl;
    wincl.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);

    InitCommonControls();

    wincl.hIcon   = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor(NULL, IDC_ARROW);

    if (!RegisterClassEx (&wincl))
        return 0;

    HWND hwnd = CreateWindow(L"WindowsApp", L"Windows App", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL, instance, NULL);

    HWND hButton = CreateWindow(L"BUTTON", L"Do something", WS_TABSTOP | WS_VISIBLE | WS_CHILD, 10, 10, 200, 23, hwnd, NULL, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);

    SendMessage(hButton, WM_SETFONT, (LPARAM) GetStockObject(DEFAULT_GUI_FONT), FALSE);
    Button_SetElevationRequiredState(hButton, TRUE);

    ShowWindow(hwnd, wndState);

    while (GetMessage(&messages, NULL, 0, 0) > 0)
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    return messages.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
+6

All Articles