使用圖示 - Win32 apps

🏛️ 365老玩家入口 ⏳ 2025-11-03 11:59:28 👤 admin 👁️ 3267 💎 206
使用圖示 - Win32 apps

下列主題描述如何執行與圖示相關的特定工作:

建立圖示

取得圖示大小

顯示圖示

共用圖示資源

建立圖示

若要使用圖示,您的應用程式必須取得圖示的控制代碼。 下列範例示範如何建立兩個不同的圖示句柄:一個用於標準問題圖示,另一個用於應用程式資源定義檔案中作為資源所包含的自定義圖示。

HICON hIcon1; // icon handle

HICON hIcon2; // icon handle

// Create a standard question icon.

hIcon1 = LoadIcon(NULL, IDI_QUESTION);

// Create a custom icon based on a resource.

hIcon2 = LoadIcon(hinst, MAKEINTRESOURCE(460));

// Create a custom icon at run time.

應用程式應該實作自定義圖示作為資源,而且應該使用 LoadIcon 或 LoadImage 函式,而不是在運行時間建立圖示。 此方法可避免裝置相依性、簡化當地語系化,並讓應用程式共享圖示位圖。 不過,下列範例會使用 CreateIcon,根據位圖掩碼在運行時間建立自定義單色圖示;其中包含它來說明系統如何解譯圖示位掩碼。

HICON hIcon3; // icon handle

// Yang icon AND bitmask

BYTE ANDmaskIcon[] = {0xFF, 0xFF, 0xFF, 0xFF, // line 1

0xFF, 0xFF, 0xC3, 0xFF, // line 2

0xFF, 0xFF, 0x00, 0xFF, // line 3

0xFF, 0xFE, 0x00, 0x7F, // line 4

0xFF, 0xFC, 0x00, 0x1F, // line 5

0xFF, 0xF8, 0x00, 0x0F, // line 6

0xFF, 0xF8, 0x00, 0x0F, // line 7

0xFF, 0xF0, 0x00, 0x07, // line 8

0xFF, 0xF0, 0x00, 0x03, // line 9

0xFF, 0xE0, 0x00, 0x03, // line 10

0xFF, 0xE0, 0x00, 0x01, // line 11

0xFF, 0xE0, 0x00, 0x01, // line 12

0xFF, 0xF0, 0x00, 0x01, // line 13

0xFF, 0xF0, 0x00, 0x00, // line 14

0xFF, 0xF8, 0x00, 0x00, // line 15

0xFF, 0xFC, 0x00, 0x00, // line 16

0xFF, 0xFF, 0x00, 0x00, // line 17

0xFF, 0xFF, 0x80, 0x00, // line 18

0xFF, 0xFF, 0xE0, 0x00, // line 19

0xFF, 0xFF, 0xE0, 0x01, // line 20

0xFF, 0xFF, 0xF0, 0x01, // line 21

0xFF, 0xFF, 0xF0, 0x01, // line 22

0xFF, 0xFF, 0xF0, 0x03, // line 23

0xFF, 0xFF, 0xE0, 0x03, // line 24

0xFF, 0xFF, 0xE0, 0x07, // line 25

0xFF, 0xFF, 0xC0, 0x0F, // line 26

0xFF, 0xFF, 0xC0, 0x0F, // line 27

0xFF, 0xFF, 0x80, 0x1F, // line 28

0xFF, 0xFF, 0x00, 0x7F, // line 29

0xFF, 0xFC, 0x00, 0xFF, // line 30

0xFF, 0xF8, 0x03, 0xFF, // line 31

0xFF, 0xFC, 0x3F, 0xFF}; // line 32

// Yang icon XOR bitmask

BYTE XORmaskIcon[] = {0x00, 0x00, 0x00, 0x00, // line 1

0x00, 0x00, 0x00, 0x00, // line 2

0x00, 0x00, 0x00, 0x00, // line 3

0x00, 0x00, 0x00, 0x00, // line 4

0x00, 0x00, 0x00, 0x00, // line 5

0x00, 0x00, 0x00, 0x00, // line 6

0x00, 0x00, 0x00, 0x00, // line 7

0x00, 0x00, 0x38, 0x00, // line 8

0x00, 0x00, 0x7C, 0x00, // line 9

0x00, 0x00, 0x7C, 0x00, // line 10

0x00, 0x00, 0x7C, 0x00, // line 11

0x00, 0x00, 0x38, 0x00, // line 12

0x00, 0x00, 0x00, 0x00, // line 13

0x00, 0x00, 0x00, 0x00, // line 14

0x00, 0x00, 0x00, 0x00, // line 15

0x00, 0x00, 0x00, 0x00, // line 16

0x00, 0x00, 0x00, 0x00, // line 17

0x00, 0x00, 0x00, 0x00, // line 18

0x00, 0x00, 0x00, 0x00, // line 19

0x00, 0x00, 0x00, 0x00, // line 20

0x00, 0x00, 0x00, 0x00, // line 21

0x00, 0x00, 0x00, 0x00, // line 22

0x00, 0x00, 0x00, 0x00, // line 23

0x00, 0x00, 0x00, 0x00, // line 24

0x00, 0x00, 0x00, 0x00, // line 25

0x00, 0x00, 0x00, 0x00, // line 26

0x00, 0x00, 0x00, 0x00, // line 27

0x00, 0x00, 0x00, 0x00, // line 28

0x00, 0x00, 0x00, 0x00, // line 29

0x00, 0x00, 0x00, 0x00, // line 30

0x00, 0x00, 0x00, 0x00, // line 31

0x00, 0x00, 0x00, 0x00}; // line 32

hIcon3 = CreateIcon(hinst, // application instance

32, // icon width

32, // icon height

1, // number of XOR planes

1, // number of bits per pixel

ANDmaskIcon, // AND bitmask

XORmaskIcon); // XOR bitmask

若要建立圖示,CreateIcon 會將下列事實數據表套用至 AND 和 XOR 位掩碼。

AND 位掩碼

XOR 位掩碼

顯示

0

0

0

1

1

0

螢幕

1

1

反轉畫面

若要在運行時間建立彩色圖示,您必須使用 CreateIconIndirect 函式,它會根據 ICONINFO 結構的內容來建立圖示。

在關閉之前,您的應用程式必須使用 DestroyIcon,來銷毀任何透過 CreateIcon 或 CreateIconIndirect所建立的圖示。 不需要銷毀其他函式所建立的圖示。

取得圖示大小

以下是範例程式碼,說明如何從 HICON 句柄 獲取圖示大小:

// Also works for cursors

BOOL GetIconDimensions(__in HICON hico, __out SIZE *psiz)

{

ICONINFO ii;

BOOL fResult = GetIconInfo(hico, &ii);

if (fResult) {

BITMAP bm;

fResult = GetObject(ii.hbmMask, sizeof(bm), &bm) == sizeof(bm);

if (fResult) {

psiz->cx = bm.bmWidth;

psiz->cy = ii.hbmColor ? bm.bmHeight : bm.bmHeight / 2;

}

if (ii.hbmMask) DeleteObject(ii.hbmMask);

if (ii.hbmColor) DeleteObject(ii.hbmColor);

}

return fResult;

}

顯示圖示

您的應用程式可以載入並建立圖示,以顯示在應用程式的工作區或子視窗中。 下列範例示範如何在視窗的工作區中繪製圖示,其裝置內容 (DC) 是由 hdc 參數所識別。

HICON hIcon1; // icon handle

HDC hdc; // handle to display context

DrawIcon(hdc, 10, 20, hIcon1);

系統會自動顯示視窗的類別圖示。 您的應用程式可以在註冊視窗類別時指派類別圖示。 您的應用程式可以使用 SetClassLong 函式來取代類別圖示。 此函式會變更指定類別之所有視窗的預設窗口設定。 下列範例會將類別圖示取代為資源標識碼為 480 的圖示。

HINSTANCE hinst; // handle to current instance

HWND hwnd; // main window handle

// Change the icon for hwnd's window class.

SetClassLongPtr(hwnd, // window handle

GCLP_HICON, // changes icon

(LONG_PTR) LoadIcon(hinst, MAKEINTRESOURCE(480))

);

若要獲得關於視窗類別的詳細資訊,請參閱 視窗類別。

共用圖示資源

下列程式代碼會使用 CreateIconFromResourceEx、DrawIcon和 LookupIconIdFromDirectoryEx和數個資源函式的函式,根據另一個可執行檔中的圖示數據建立圖示句柄。 然後,它會在視窗中顯示圖示。

安全性警告: 使用 LoadLibrary 錯誤,可能會藉由載入錯誤的 DLL 來危害應用程式的安全性。 如需如何使用不同 Windows 版本正確載入 DLL 的相關信息,請參閱 LoadLibrary 檔。

HICON hIcon1; // icon handle

HINSTANCE hExe; // handle to loaded .EXE file

HRSRC hResource; // handle to FindResource

HRSRC hMem; // handle to LoadResource

BYTE *lpResource; // pointer to resource data

int nID; // ID of resource that best fits current screen

HDC hdc; // handle to display context

// Load the file from which to copy the icon.

// Note: LoadLibrary should have a fully explicit path.

//

hExe = LoadLibrary("myapp.exe");

if (hExe == NULL)

{

//Error loading module -- fail as securely as possible

return;

}

// Find the icon directory whose identifier is 440.

hResource = FindResource(hExe,

MAKEINTRESOURCE(440),

RT_GROUP_ICON);

// Load and lock the icon directory.

hMem = LoadResource(hExe, hResource);

lpResource = LockResource(hMem);

// Get the identifier of the icon that is most appropriate

// for the video display.

nID = LookupIconIdFromDirectoryEx((PBYTE) lpResource, TRUE,

CXICON, CYICON, LR_DEFAULTCOLOR);

// Find the bits for the nID icon.

hResource = FindResource(hExe,

MAKEINTRESOURCE(nID),

MAKEINTRESOURCE(RT_ICON));

// Load and lock the icon.

hMem = LoadResource(hExe, hResource);

lpResource = LockResource(hMem);

// Create a handle to the icon.

hIcon1 = CreateIconFromResourceEx((PBYTE) lpResource,

SizeofResource(hExe, hResource), TRUE, 0x00030000,

CXICON, CYICON, LR_DEFAULTCOLOR);

// Draw the icon in the client area.

DrawIcon(hdc, 10, 20, hIcon1);

相关掠夺

365bet的官网是多少
营销方案网

营销方案网

🗓️ 09-25 👁️ 5352
365老玩家入口
中国药店

中国药店

🗓️ 10-01 👁️ 7115
365bet的官网是多少
可我偏要偏要

可我偏要偏要

🗓️ 08-12 👁️ 2162