如何防止应用程序在 Windows 7 中被固定?
我试图阻止用户将我的 .NET 应用程序固定到任务栏。我在 Old New Thing 上找到了一些代码,它的作用只是那。然而,它是用C++编写的。
#include <shellapi.h>
#include <propsys.h>
#include <propkey.h>
HRESULT MarkWindowAsUnpinnable(HWND hwnd)
{
IPropertyStore *pps;
HRESULT hr = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&pps));
if (SUCCEEDED(hr)) {
PROPVARIANT var;
var.vt = VT_BOOL;
var.boolVal = VARIANT_TRUE;
hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
pps->Release();
}
return hr;
}
BOOL
OnCreate(HWND hwnd, LPCREATESTRUCT lpcs)
{
MarkWindowAsUnpinnable(hwnd);
return TRUE;
}
我把它转换成 C# 的运气很差。有人可以帮忙吗?
I am trying to prevent the user from pinning my .NET app to the taskbar. I've found some code on the Old New Thing that does just that. However, it is in C++.
#include <shellapi.h>
#include <propsys.h>
#include <propkey.h>
HRESULT MarkWindowAsUnpinnable(HWND hwnd)
{
IPropertyStore *pps;
HRESULT hr = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&pps));
if (SUCCEEDED(hr)) {
PROPVARIANT var;
var.vt = VT_BOOL;
var.boolVal = VARIANT_TRUE;
hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
pps->Release();
}
return hr;
}
BOOL
OnCreate(HWND hwnd, LPCREATESTRUCT lpcs)
{
MarkWindowAsUnpinnable(hwnd);
return TRUE;
}
I am having very little luck converting it to c#. Can someone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以下载 Windows API 代码包,其中包含必要的 p/invoke 调用您需要将帖子中的代码翻译为 C#。
要么完全使用该库,要么找到您需要的特定调用和定义(搜索
SHGetPropertyStoreForWindow
以及其他依赖项)。You can download the Windows API Code Pack which has the necessary p/invoke calls you need to translate the code in your post to C#.
Either use the library in whole or find the specific calls and definitions you require (search it for
SHGetPropertyStoreForWindow
and then its other dependencies).在这个问题中,旧的新事物帖子还讨论了如何在每个应用程序的基础上设置一些注册表设置,以防止将应用程序固定到任务栏。
您所要做的就是将“NoStartPage”的值添加到 Root\Applications 下的应用程序的键中。该值可以为空并且可以是任何类型,如果 Windows 只是看到它在那里,当用户在任务栏中右键单击该应用程序时,它不会显示固定该应用程序的功能。
以下是 Microsoft 关于此功能的文档:使用注册表来防止固定 需要注意的是,
在 Windows 7 中,由于 UAC,您必须以管理员身份运行才能更新注册表。我通过 app.manifest 做到了这一点。
找到正确并更新正确注册表项的代码如下(希望它不会太冗长):
In the question, the Old New Thing post also talks about how you can set some registry settings on per application basis that will so prevent the pinning an application to the taskbar.
All you have to do is add the value of "NoStartPage" to a key for your application under the Root\Applications. The value can be blank and of any type, if Windows just sees it is there it will not show the ability to pin the app, when the user right clicks on it in the taskbar.
Here is the documentation from Microsoft on this feature: Use Registry to prevent pinning of an application
The one caveat to this is that in Windows 7, due to UAC, you have to run as administrator to update the registry. I did this via the app.manifest.
The code to find the right and update the correct registry keys is below (hopefully it is not too verbose):
使用 WindowsAPICodePack(通过 NuGet)您需要类似的代码:
Using the WindowsAPICodePack (via NuGet) you need code resembling: