文章編號: 318876 - 上次校閱: 2007年2月12日 - 版次: 1.5

如何在 Windows XP 中建立 Alpha 混合的游標或圖示

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
全部展開 | 全部摺疊

結論

本文將告訴您需要以程式設計方式建立游標或圖示包含 Alpha 的位元的步驟。基於本文的目的,這些類型的游標或圖示被指 「 alpha 混合資料指標 」 或 Alpha 混合圖示。Alpha 混合資料指標,且在 Microsoft Windows XP 上只支援 alpha 混合的圖示。

其他相關資訊

若要建立 Alpha 混合的游標或圖示,建立包含 Alpha 值的 DIB 區段,然後呼叫 CreateIconIndirect 函式與該 DIB 區段]。雖然遮罩點陣圖 ICONINFO 結構中的不是必要的您必須建立一個空的單色點陣圖,傳送給 CreateIconIndirect 函式,為遮罩點陣圖。Alpha 的位元,每個像素 DIB 區段點陣圖中決定游標或已建立的圖示中的每一個像素的可視性。

請依照下列步驟執行來建立 Alpha 混合的游標或圖示:
  1. 完成 BITMAPV5HEADER 結構,請依照下列步驟執行,程式碼例如,來定義每像素 (BPP) 32 位元 Alpha 混合 DIB。
  2. 呼叫 CreateDIBSection 函式來建立 DIB 區段,根據您完成 [BITMAPV5HEADER 結構。
  3. 使用您要為您的 Alpha 混合的游標或圖示完成 DIB 區段點陣圖和 Alpha 資訊。
  4. 完成 ICONINFO 結構。
  5. 將放在 hbmMask] 欄位中空白的單色點陣圖,接著在 [hbmColor] 欄位中放入 Alpha 混合的 DIB 區段。
  6. 呼叫 CreateIconIndirect 函式來建立 Alpha 混合的游標或圖示。
下列的 Microsoft Visual C++ 程式碼會示範如何建立 Alpha 混合資料指標。相同的程式碼可用來藉由變更 ICONINFO 結構 fIcon 成員設為 TRUE,以建立 Alpha 混合的圖示:

HCURSOR CreateAlphaCursor(void)
{
    HDC hMemDC;
    DWORD dwWidth, dwHeight;
    BITMAPV5HEADER bi;
    HBITMAP hBitmap, hOldBitmap;
    void *lpBits;
    DWORD x,y;
    HCURSOR hAlphaCursor = NULL;

    dwWidth  = 32;  // width of cursor
    dwHeight = 32;  // height of cursor

    ZeroMemory(&bi,sizeof(BITMAPV5HEADER));
    bi.bV5Size           = sizeof(BITMAPV5HEADER);
    bi.bV5Width           = dwWidth;
    bi.bV5Height          = dwHeight;
    bi.bV5Planes = 1;
    bi.bV5BitCount = 32;
    bi.bV5Compression = BI_BITFIELDS;
    // The following mask specification specifies a supported 32 BPP
    // alpha format for Windows XP.
    bi.bV5RedMask   =  0x00FF0000;
    bi.bV5GreenMask =  0x0000FF00;
    bi.bV5BlueMask  =  0x000000FF;
    bi.bV5AlphaMask =  0xFF000000; 

    HDC hdc;
    hdc = GetDC(NULL);

    // Create the DIB section with an alpha channel.
    hBitmap = CreateDIBSection(hdc, (BITMAPINFO *)&bi, DIB_RGB_COLORS, 
        (void **)&lpBits, NULL, (DWORD)0);

    hMemDC = CreateCompatibleDC(hdc);
    ReleaseDC(NULL,hdc);

    // Draw something on the DIB section.
    hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
    PatBlt(hMemDC,0,0,dwWidth,dwHeight,WHITENESS);
    SetTextColor(hMemDC,RGB(0,0,0));
    SetBkMode(hMemDC,TRANSPARENT);
    TextOut(hMemDC,0,9,"rgba",4);
    SelectObject(hMemDC, hOldBitmap);
    DeleteDC(hMemDC);

    // Create an empty mask bitmap.
    HBITMAP hMonoBitmap = CreateBitmap(dwWidth,dwHeight,1,1,NULL);

    // Set the alpha values for each pixel in the cursor so that
    // the complete cursor is semi-transparent.
    DWORD *lpdwPixel;
    lpdwPixel = (DWORD *)lpBits;
    for (x=0;x<dwWidth;x++)
       for (y=0;y<dwHeight;y++)
       {
           // Clear the alpha bits
           *lpdwPixel &= 0x00FFFFFF;
           // Set the alpha bits to 0x9F (semi-transparent)
           *lpdwPixel |= 0x9F000000;
           lpdwPixel++;
       }

    ICONINFO ii;
    ii.fIcon = FALSE;  // Change fIcon to TRUE to create an alpha icon
    ii.xHotspot = 0;
    ii.yHotspot = 0;
    ii.hbmMask = hMonoBitmap;
    ii.hbmColor = hBitmap;

    // Create the alpha cursor with the alpha DIB section.
    hAlphaCursor = CreateIconIndirect(&ii);

    DeleteObject(hBitmap);          
    DeleteObject(hMonoBitmap); 

    return hAlphaCursor;
}
				

這篇文章中的資訊適用於:
  • Microsoft Platform Software Development Kit-January 2000 Edition?應用於:
    • the operating system: Microsoft Windows XP
  • Microsoft Windows XP Professional
  • the operating system: Microsoft Windows XP 64-Bit Edition
關鍵字:?
kbmt kbdswgdi2003swept kbcursor kbgdi kbhowto KB318876 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:318876? (http://support.microsoft.com/kb/318876/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。