VC++和C#的pin two programs to Taskbar

在VC++里面要使用loadlibrary的方式来使用该API,我看过MSDN后,尝试自己将头文件的定义里面加入这个API,并且comment了shell32.dll,但是还是提示找不到外部变量。

http://msdn.microsoft.com/en-us/library/dd378422(VS.85).aspx

后面google了一下,得到下面的文章

http://code.logos.com/blog/c_1/

以下为代码

不过需要注意的是,C++和C#两种方法设置的APPID不能通用,也就是不能将C++和 C#的程序pin到同一个icon上,原因未明。

当然一个办法是:

用C++去 Load C#写的dll,这个DLL的作用是使用C#的方式去设置AppID

 

 

下面同一般使用DLL的方法:

typedef  HRESULT (__stdcall *SETCURRENTPROCESSEXPLICITAPPUSERMODELIDPROC)(PCWSTR AppID);


// 给这个进程赋予一个指定的APPID,这样win7就会把他们放到一个ICON上

void  SetAppUserModelId()
{
    // try to load Shell32.dll
    HMODULE hmodShell32 = LoadLibrary(L"shell32.dll" );
    if  (hmodShell32 != NULL)
    {
        // see if the function is exposed by the current OS
        SETCURRENTPROCESSEXPLICITAPPUSERMODELIDPROC pfnSetCurrentProcessExplicitAppUserModelID =
            reinterpret_cast<SETCURRENTPROCESSEXPLICITAPPUSERMODELIDPROC>(GetProcAddress(hmodShell32,
                "SetCurrentProcessExplicitAppUserModelID" ));
        if  (pfnSetCurrentProcessExplicitAppUserModelID != NULL)
        {
            pfnSetCurrentProcessExplicitAppUserModelID(L"YourCompany.YourApp.1" );
        }
 
        FreeLibrary(hmodShell32);
    }
}


To the C# WPF application, add similar code (with a version check):

C# WPF的ap也是添加类似的方法

///   <summary>

///  Sets the Windows 7 application user model ID that will apply to the entire process. This identifier allows an application to

///  group its associated processes and windows under a single taskbar button.

///   </summary>

///   <param name="appId"> The application user model ID. </param>

public  static  void  SetApplicationUserModelId(string  appId)
{
    // check for Windows 7
    Version  version = Environment .OSVersion.Version;
    if  ((version.Major > 6) || (version.Major == 6 && version.Minor >= 1))
    {
        SafeNativeMethods .SetCurrentProcessExplicitAppUserModelID(appId);
    }
}
 
[SuppressUnmanagedCodeSecurity ]
internal  static  class  SafeNativeMethods
{
    [DllImport ("shell32.dll" )]
    public  static  extern  void  SetCurrentProcessExplicitAppUserModelID([MarshalAs(UnmanagedType .LPWStr)] string AppID);
}


 

需要在代码里面尽快调用这个方法——要在界面出现之前。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值