导航:  CWindow Class > Tutorial >

Using PNG icons in toolbars

上一页返回章节概述下一页

AfxGdiplus.inc提供了允许在工具栏中使用alphablended PNG图标的功能。

 

AfxGdipIconFromFile 从嵌入在应用程序中的资源文件加载来自磁盘和。 AfxGdipIconFromRes的映像

 

我们需要为相应大小的工具栏创建一个图像列表。 要计算大小,我使用以下公式:16 * pWindow.DPI \ 96.其中16是一个普通图标的大小(个人而言,对于工具栏,我更喜欢使用20来使它们更大),pWindow。 DPI由计算机使用的DPI和96 DPI不知道的应用程序使用的DPI。

 

' // Create an image list for the toolbar

DIM hImageList AS HIMAGELIST

DIM cx AS LONG = 16 * pWindow.DPI \ 96

hImageList = ImageList_Create(cx, cx, ILC_COLOR32 OR ILC_MASK, 4, 0)

IF hImageList THEN

 AfxGdipAddIconFromRes(hImageList, hInst, "IDI_ARROW_LEFT_32")

 AfxGdipAddIconFromRes(hImageList, hInst, "IDI_ARROW_RIGHT_48")

 AfxGdipAddIconFromRes(hImageList, hInst, "IDI_HOME_48")

 AfxGdipAddIconFromRes(hImageList, hInst, "IDI_SAVE_48")

END IF

SendMessageW hToolBar, TB_SETIMAGELIST, 0, CAST(LPARAM, hImageList)

 

我在这个例子中使用了48位图标,通常调整好以适应不同的DPI设置。 这样,我们只能使用一组图标而不是几组不同大小的图标。 但是,为了获得最佳质量,建议使用适当的图标尺寸。

 

AfxGdipIconFromFileAfxGdipIconFromRes 还提供两个可选参数dimPercentbGrayScale。 使用dimPercent可以指示调光的百分比,bGrayScale是一个布尔值(TRUE或FALSE),可以将这些函数转换为灰色阴影。 这允许创建一个具有相同图标集的禁用项目的图像列表。 以下代码使用相同颜色的PNG图标创建禁用的图像,但将其调整为60%,并将其转换为灰色:

 

' // Create a disabled image list for the toolbar

DIM hDisabledImageList AS HIMAGELIST

DIM cx AS LONG = 16 * pWindow.DPI \ 96

hDisabledImageList = ImageList_Create(cx, cx, ILC_COLOR32 OR ILC_MASK, 4, 0)

IF hDisabledImageList THEN

 AfxGdipAddIconFromRes(hDisabledImageList, hInst, "IDI_ARROW_LEFT_32", 60, TRUE))

 AfxGdipAddIconFromRes(hDisabledImageList, hInst, "IDI_ARROW_RIGHT_48", 60, TRUE))

 AfxGdipAddIconFromRes(hDisabledImageList, hInst, "IDI_HOME_48", 60, TRUE))

 AfxGdipAddIconFromRes(hDisabledImageList, hInst, "IDI_SAVE_48", 60, TRUE))

END IF

SendMessageW hToolBar, TB_SETDISABLEDIMAGELIST, 0, CAST(LPARAM, hDisabledImageList)

 

Resource file:

 

//=============================================================================

// Manifest

//=============================================================================

 

1 24 "WThemes.xml"

 

//=============================================================================

// Toolbar icons

//=============================================================================

 

// Toolbar, normal

IDI_ARROW_LEFT_48       RCDATA "arrow_left_48.png"

IDI_ARROW_RIGHT_48      RCDATA "arrow_right_48.png"

IDI_HOME_48             RCDATA "home_48.png"

IDI_SAVE_48             RCDATA "save_48.png"

 

Manifest:

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">

 

    <assemblyIdentity version="1.0.0.0"

       processorArchitecture="*"

       name="ApplicationName"

       type="win32"/>

    <description>Optional description of your application</description>

 

    <asmv3:application>

       <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">

          <dpiAware>true</dpiAware>

       </asmv3:windowsSettings>

    </asmv3:application>

 

    <!-- Compatibility section -->

    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">

       <application>

          <!--The ID below indicates application support for Windows Vista -->

          <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>

          <!--The ID below indicates application support for Windows 7 -->

          <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>

          <!--This Id value indicates the application supports Windows 8 functionality-->

          <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>

          <!--This Id value indicates the application supports Windows 8.1 functionality-->

          <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>

       </application>

     </compatibility>

 

    <!-- Trustinfo section -->

    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">

       <security>

          <requestedPrivileges>

             <requestedExecutionLevel

                level="asInvoker"

                uiAccess="false"/>

             </requestedPrivileges>

       </security>

    </trustInfo>

 

    <dependency>

       <dependentAssembly>

          <assemblyIdentity

             type="win32"

             name="Microsoft.Windows.Common-Controls"

             version="6.0.0.0"

             processorArchitecture="*"

             publicKeyToken="6595b64144ccf1df"

             language="*" />

       </dependentAssembly>

    </dependency>

 

 </assembly>

 

示例

 

' ########################################################################################

' Microsoft Windows

' Contents: CWindow with a toolbar

' Compiler: FreeBasic 32 & 64 bit

' Copyright (c) 2016 José Roca. Freeware. Use at your own risk.

' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER

' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF

' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

' ########################################################################################

 

#define unicode

#INCLUDE ONCE "windows.bi"

#INCLUDE ONCE "Afx/CWindow.inc"

#INCLUDE ONCE "Afx/AfxCtl.inc"

#INCLUDE ONCE "Afx/AfxGdiplus.inc"

' $FB_RESPATH = "FBTBRES.rc"

 

USING Afx

 

DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _

                        BYVAL hPrevInstance AS HINSTANCE, _

                        BYVAL szCmdLine AS ZSTRING PTR, _

                        BYVAL nCmdShow AS LONG) AS LONG

 

 END WinMain(GetModuleHandleW(NULL), NULL, COMMAND(), SW_NORMAL)

 

CONST IDC_TOOLBAR = 1001

enum

 IDM_LEFTARROW = 28000

 IDM_RIGHTARROW

 IDM_HOME

 IDM_SAVEFILE

end enum

 

' ========================================================================================

' 窗口处理程序

' ========================================================================================

FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

 

 DIM pWindow AS CWindow PTR

 

 SELECT CASE uMsg

 

    CASE WM_CREATE

       EXIT FUNCTION

 

    CASE WM_COMMAND

       SELECT CASE LOWORD(wParam)

          CASE IDCANCEL

             ' // If ESC key pressed, close the application sending an WM_CLOSE message

             IF HIWORD(wParam) = BN_CLICKED THEN

                SendMessageW hwnd, WM_CLOSE, 0, 0

                EXIT FUNCTION

             END IF

'            CASE IDM_CUT   ' etc.

'               MessageBoxW hwnd, "You have clicked the Cut button", "Toolbar", MB_OK

'               EXIT FUNCTION

       END SELECT

 

    CASE WM_SIZE

       IF wParam <> SIZE_MINIMIZED THEN

          ' // Update the size and position of the Toolbar control

          SendMessageW GetDlgItem(hWnd, IDC_TOOLBAR), TB_AUTOSIZE, 0, 0

          ' // Resize the buttons

          pWindow = CAST(CWindow PTR, GetWindowLongPtr(hwnd, 0))

          pWindow->MoveWindow GetDlgItem(hwnd, IDCANCEL), pWindow->ClientWidth - 95, pWindow->ClientHeight - 35, 75, 23, CTRUE

       END IF

 

         CASE WM_DESTROY

       ' // Destroy the image list

       ImageList_Destroy CAST(HIMAGELIST, SendMessageW(GetDlgItem(hwnd, IDC_TOOLBAR), TB_SETIMAGELIST, 0, 0))

       PostQuitMessage(0)

       EXIT FUNCTION

 

 END SELECT

 

 FUNCTION = DefWindowProcW(hWnd, uMsg, wParam, lParam)

 

END FUNCTION

' ========================================================================================

 

' ========================================================================================

' Main

' ========================================================================================

FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _

                BYVAL hPrevInstance AS HINSTANCE, _

                BYVAL szCmdLine AS ZSTRING PTR, _

                BYVAL nCmdShow AS LONG) AS LONG

 

 ' // 设置处理DPI感知

'   AfxSetProcessDPIAware

 

 DIM pWindow AS CWindow

 pWindow.Create(NULL, "CWindow with a toolbar", @WndProc)

 ' // Disable background erasing

 pWindow.ClassStyle = CS_DBLCLKS

 ' // Set the client size

 pWindow.SetClientSize(600, 300)

 ' // Center the window

 pWindow.Center

 

 ' // Add a button

 pWindow.AddControl("Button", pWindow.hWindow, IDCANCEL, "&Close")

 

 ' // Add a tooolbar

 DIM hToolBar AS HWND = pWindow.AddControl("Toolbar", pWindow.hWindow, IDC_TOOLBAR)

 ' // Module instance handle

 DIM hInst AS HINSTANCE = GetModuleHandle(NULL)

 ' // Create an image list for the toolbar

 DIM hImageList AS HIMAGELIST

 DIM cx AS LONG = 16 * pWindow.DPI \ 96

 hImageList = ImageList_Create(cx, cx, ILC_COLOR32 OR ILC_MASK, 4, 0)

 IF hImageList THEN

    ImageList_ReplaceIcon(hImageList, -1, AfxGdipIconFromRes(hInst, "IDI_ARROW_LEFT_48"))

    ImageList_ReplaceIcon(hImageList, -1, AfxGdipIconFromRes(hInst, "IDI_ARROW_RIGHT_48"))

    ImageList_ReplaceIcon(hImageList, -1, AfxGdipIconFromRes(hInst, "IDI_HOME_48"))

    ImageList_ReplaceIcon(hImageList, -1, AfxGdipIconFromRes(hInst, "IDI_SAVE_48"))

 END IF

 SendMessageW hToolBar, TB_SETIMAGELIST, 0, CAST(LPARAM, hImageList)

 

 ' // Add buttons to the toolbar

 Toolbar_AddButton hToolBar, 0, IDM_LEFTARROW

 Toolbar_AddButton hToolBar, 1, IDM_RIGHTARROW

 Toolbar_AddButton hToolBar, 2, IDM_HOME

 Toolbar_AddButton hToolBar, 3, IDM_SAVEFILE

 

 ' // Size the toolbar

 SendMessageW hToolBar, TB_AUTOSIZE, 0, 0

 

 ' // Process event messages

 FUNCTION = pWindow.DoEvents(nCmdShow)

 

END FUNCTION

' ========================================================================================