Windows 8.1下InitCommonControlsEx()使用ICC_LINK_CLASS时失败

InitCommonControlsEx() fails with ICC_LINK_CLASS in Windows 8.1

本文关键字:LINK CLASS 失败 ICC 使用 1下 InitCommonControlsEx Windows      更新时间:2023-10-16

我想在我的窗口放一个简单的超链接。

INITCOMMONCONTROLSEX iccx;
iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccx.dwICC  = ICC_LINK_CLASS;   // CommCtrl.h: #define ICC_LINK_CLASS 0x00008000
bool bResult = InitCommonControlsEx(&iccx); // bResult is false.
DWORD dwError = GetLastError(); // dwError is 0.
hWnd = CreateWindowExW( /*_In_      DWORD*/     0,
                        /*_In_opt_  LPCTSTR*/   WC_LINK, // CommCtrl.h: #define WC_LINK L"SysLink"
                        /*_In_opt_  LPCTSTR*/   L"Hello World",
                        /*_In_      DWORD*/     WS_VISIBLE | WS_CHILD | WS_TABSTOP,
                        /*_In_      int*/       50,
                        /*_In_      int*/       200,
                        /*_In_      int*/       100,
                        /*_In_      int*/       20,
                        /*_In_opt_  HWND*/      hWndParent,
                        /*_In_opt_  HMENU*/     NULL,
                        /*_In_opt_  HINSTANCE*/ hInstance,
                        /*_In_opt_  LPVOID*/    NULL);
DWORD dwError = GetLastError(); // hWnd is NULL and dwError is 1407.

错误码1407在这里解释如下。

ERROR_CANNOT_FIND_WND_CLASS
    1407 (0x57F)
    Cannot find window class.

我用的是Windows 8.1 Pro x64,我从来没有在任何其他版本的Windows上尝试过这段代码。

这里有什么问题?

正如你所发现的,添加

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

是解决问题的一种方法。

SysLink控件仅在通用控件版本6中添加。出于向后兼容性的原因,默认情况下不启用Common Controls 6。你必须通过创建一个清单来选择加入它。

清单可以作为单独的文件(命名为program.exe.manifest)存在,也可以作为具有特定资源ID的资源存在。#pragma行告诉Microsoft的链接器为您生成第二个。你也可以自己制作任何一种形式。这是如何。