简体   繁体   English

如何在MFC对话框按钮上设置管理权限图标?

[英]How to set Administrative right Icon on MFC Dialog Button?

I am creating an application in MFC using Visual Studio 2008 in windows 7. My application starts and stops a service which requires administrative access. 我正在Windows 7中使用Visual Studio 2008在MFC中创建应用程序。我的应用程序启动和停止需要管理访问权限的服务。 When application starts it don't have administrative access. 当应用程序启动时,它没有管理权限。 But when I click on start service button it get the administrative access and performs the action. 但是,当我单击启动服务按钮时,它将获得管理访问权并执行操作。 I am wondering how to set administrative icon on buttons whose actions need administrative access? 我想知道如何在需要操作访问权限的按钮上设置管理图标? Do I need to set some kind of Flags? 我需要设置某种标志吗? Thanks 谢谢

As of Windows Vista you can add the shield icon to a button by using one of the new flags. 从Windows Vista开始,您可以使用新标志之一将盾牌图标添加到按钮。 There is a macro to enable it, which you can use like this: 有一个启用它的宏,您可以像这样使用它:

Button_SetElevationRequiredState(hwnd, TRUE);

Documentation for the macro is at http://msdn.microsoft.com/en-us/library/bb761865%28VS.85%29.aspx 宏的文档位于http://msdn.microsoft.com/zh-cn/library/bb761865%28VS.85%29.aspx

See http://msdn.microsoft.com/en-us/library/bb756990.aspx#BKMK_ShieldButton for an overview of how to do many UAC related tasks. 有关如何执行许多与UAC相关的任务的概述,请参见http://msdn.microsoft.com/zh-cn/library/bb756990.aspx#BKMK_ShieldButton

There is also CButton::SetElevationRequired() which presumably does the same thing but fits more in line with your MFC project. 还有CButton :: SetElevationRequired()可能做同样的事情,但更适合您的MFC项目。 You could use it like this: 您可以这样使用它:

ctl->SetElevationRequired(TRUE);

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

You also need to enable the use of the common controls v6 DLL, which you can do either using a manifest file (with or witout an entry in your resource file to embed it) or using a #pragma directive in your code for MSVC2005 or later. 您还需要启用公共控件v6 DLL,您可以使用清单文件(在资源文件中带有或不包含要嵌入的条目)或在MSVC2005或更高版本的代码中使用#pragma指令来执行此操作。 。 Chances are with an MFC application that you will already have a manifest which you can modify, but I cannot help you there as I do not have access to MFC. MFC应用程序很可能已经有了一个清单,您可以对其进行修改,但是由于我无权访问MFC,因此我在这里无法为您提供帮助。

If you go the manifest route it should look something like this and have the same name as your application but with ".manifest" after the .exe, eg MyApp.exe.manifest: 如果走清单路由,它应该看起来像这样,并且与您的应用程序具有相同的名称,但是在.exe后带有“ .manifest”,例如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>

For the #pragma, see the code below. 有关#pragma,请参见下面的代码。

For more information about using v6 common controls see this link (which is where I got the above information from): http://msdn.microsoft.com/en-us/library/bb773175%28v=vs.85%29.aspx 有关使用v6通用控件的更多信息,请参见以下链接(从中可以找到上述信息): http : //msdn.microsoft.com/zh-cn/library/bb773175%28v=vs.85%29.aspx

A small win32 example which uses the pragma directive to enable common controls v6 and displays the elevation icon: 一个小的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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM