繁体   English   中英

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

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

我正在Windows 7中使用Visual Studio 2008在MFC中创建应用程序。我的应用程序启动和停止需要管理访问权限的服务。 当应用程序启动时,它没有管理权限。 但是,当我单击启动服务按钮时,它将获得管理访问权并执行操作。 我想知道如何在需要操作访问权限的按钮上设置管理图标? 我需要设置某种标志吗? 谢谢

从Windows Vista开始,您可以使用新标志之一将盾牌图标添加到按钮。 有一个启用它的宏,您可以像这样使用它:

Button_SetElevationRequiredState(hwnd, TRUE);

宏的文档位于http://msdn.microsoft.com/zh-cn/library/bb761865%28VS.85%29.aspx

有关如何执行许多与UAC相关的任务的概述,请参见http://msdn.microsoft.com/zh-cn/library/bb756990.aspx#BKMK_ShieldButton

还有CButton :: SetElevationRequired()可能做同样的事情,但更适合您的MFC项目。 您可以这样使用它:

ctl->SetElevationRequired(TRUE);

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

您还需要启用公共控件v6 DLL,您可以使用清单文件(在资源文件中带有或不包含要嵌入的条目)或在MSVC2005或更高版本的代码中使用#pragma指令来执行此操作。 。 MFC应用程序很可能已经有了一个清单,您可以对其进行修改,但是由于我无权访问MFC,因此我在这里无法为您提供帮助。

如果走清单路由,它应该看起来像这样,并且与您的应用程序具有相同的名称,但是在.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>

有关#pragma,请参见下面的代码。

有关使用v6通用控件的更多信息,请参见以下链接(从中可以找到上述信息): http : //msdn.microsoft.com/zh-cn/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;
}

暂无
暂无

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

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