Win32属性页中的图标是丑陋的4位图标

Icons in Win32 Property Pages are ugly - 4 bit icons

本文关键字:图标 4位 属性 Win32      更新时间:2023-10-16

我在应用程序中使用Win32 API C++属性表,与例如主标题或应用程序中的其他图标相比,页头中使用的图标质量较低。

https://i.stack.imgur.com/SxxWc.png

在附加的图片中,两个房屋图标都来自同一资源。

有没有办法把它改成32位的彩色图标?

const int Sheets = 2;
PROPSHEETPAGE psp[Sheets];
for (int i=0; i<Sheets; ++i)
{
    psp[i].dwSize = sizeof(PROPSHEETPAGE);
    psp[i].dwFlags = PSP_USEICONID | PSP_USETITLE;
    psp[i].lParam = 0;
    psp[i].pfnCallback = NULL;
    psp[i].hInstance = m_hInst;
}
psp[0].pszTemplate = MAKEINTRESOURCE(IDDNEW_IS0);
psp[0].pszIcon     = MAKEINTRESOURCE(IDI_GENERAL_TAB);
psp[0].pfnDlgProc  = IntegrationServer::tabGeneral;
psp[0].pszTitle    = "General";
psp[1].pszTemplate = MAKEINTRESOURCE(IDDNEW_IS1);
psp[1].pszIcon     = MAKEINTRESOURCE(IDI_GENERAL_REQUESTS);
psp[1].pfnDlgProc  = IntegrationServer::tabRequests;
psp[1].pszTitle    = "Requests";
PROPSHEETHEADER psh;
psh.dwSize      = sizeof(PROPSHEETHEADER);
psh.dwFlags     = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOCONTEXTHELP | PSH_NOAPPLYNOW;
psh.hInstance   = m_hInst;
psh.pszIcon     = MAKEINTRESOURCE(IDI_GENERAL_TAB);
psh.pszCaption  = (LPSTR) "Integration Server configuration";
psh.nPages      = sizeof(psp) / sizeof(PROPSHEETPAGE);
psh.nStartPage  = 0;
psh.ppsp        = (LPCPROPSHEETPAGE) &psp;
psh.hwndParent  = m_hWnd;
PropertySheet(&psh);

最后,我找到了解决上述问题的方法。我使用Tab控件创建了类似的窗口,而不是属性表。

使用Tab控件的好处:

  • 更容易维护每个选项卡的内容(只显示/隐藏HWND)
  • 图标是32位
  • 它是一个标准控件,类似于按钮、静态文本等,所以它以相同的方式工作

缺陷:

  • 需要编写更多的代码

以下是一个示例窗口:https://i.stack.imgur.com/0dxEv.png

源代码:

/* 
    Need this:
    #include <commctrl.h >
    #pragma comment(lib, "Comctl32.lib")
    #pragma comment(linker,"/manifestdependency:"type='win32' "
        "name='Microsoft.Windows.Common-Controls' version='6.0.0.0' "
        "processorArchitecture='*' publicKeyToken='6595b64144ccf1df' "
        "language='*'"")
*/
// get HWND of Tab Control
HWND tab = GetDlgItem(hDlg, IDC_TAB1);
// get current instance
HINSTANCE hInst = (HINSTANCE) GetModuleHandle(NULL);
// insert 7 tabs in our Tab Control
TCITEM tie;
tie.mask = TCIF_TEXT | TCIF_IMAGE; 
LPSTR item[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
for (int i = 0; i < 7; i++) 
{
    tie.pszText = item[i];
    tie.iImage = i;
    if (TabCtrl_InsertItem(tab, i, &tie) == -1)
        break; 
}
// insert 7 icons for each Tab
HIMAGELIST hi = ImageList_Create(16, 16, ILC_COLOR32, 0, 7);
if (hi != NULL)
{
    int icons[] = {IDI_ACTIONADD, IDI_ACTIONDELETE, IDI_ACTIONEDIT,
        IDI_ACTIONIMPORT, IDI_ACTIONVIEW, IDI_CONFIGURATION,
        IDI_CONF_CLEANUP};
    for (int i =0; i<7; ++i)
    {
        HICON icon = (HICON) LoadImage(hInst, MAKEINTRESOURCE(icons[i]), IMAGE_ICON, 16, 16, 0);
        ICONINFO iconinfo;
        GetIconInfo(icon, &iconinfo);
        HBITMAP bitmap = iconinfo.hbmColor;
        ImageList_Add(hi, bitmap, NULL);
        DestroyIcon(icon);
    }
}
TabCtrl_SetImageList(tab, hi);
// Set position and size of child window to
// put it on the entire surface of tab display window
RECT rect;
GetClientRect(tab, &rect);
// This will collect entire Tab window and will return rectangle, which will
// fulfill display space
TabCtrl_AdjustRect(tab, FALSE, &rect);
// Create child window, which will be inserted into Tab display space
HWND child = CreateDialog(hInst, MAKEINTRESOURCE(IDD_IS_COMMON_CLEANUP),
    tab, IntegrationServer::empty);
// Set child window position and size to fulfill Tab Control
// those "-2", "-1" etc. are just for me - I don't like white space :)
SetWindowPos(child, NULL, 
    rect.left-2, rect.top-1, rect.right-rect.left+2, rect.bottom-rect.top+2, 
    SWP_NOZORDER);
// Show child window
ShowWindow(child, SW_SHOW);

之后,当用户要更改当前显示的选项卡时,您必须查找选项卡控件通知,并将新的HWND加载到选项卡控件的显示空间中。

选项卡控件的通知可在此处找到:http://msdn.microsoft.com/en-us/library/windows/desktop/bb760548(v=vs.85).aspx