• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            S.l.e!ep.¢%

            像打了激速一樣,以四倍的速度運轉,開心的工作
            簡單、開放、平等的公司文化;尊重個性、自由與個人價值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            AlphaBlend

            Posted on 2010-09-07 14:40 S.l.e!ep.¢% 閱讀(1593) 評論(0)  編輯 收藏 引用 所屬分類: DirectUI

            AlphaBlend

              函數功能:該函數用來顯示具有指定透明度的圖像。
              函數原型:AlphaBlend(HDC hdcDest,int nXOriginDest,int nYOriginDest,int nWidthDest,int hHeightDest,HDC hdcSrc,int nXOriginSrc,int nYOriginSrc,int nWidthSrc,int nHeightSrc,BLENDFUNCTION blendFunction);
              參數:
              hdcDest:指向目標設備環境的句柄。
              nXoriginDest:指定目標矩形區域左上角的X軸坐標,按邏輯單位。
              nYOriginDest:指定目標矩形區域左上角的Y軸坐標,按邏輯單位。
              nWidthDest:指定目標矩形區域的寬度,按邏輯單位。
              hHeghtdest:指向目標矩形區域高度的句柄,按邏輯單位。
              hdcSrc:指向源設備環境的句柄。
              nXOriginSrc:指定源矩形區域左上角的X軸坐標,按邏輯單位。
              nYOriginSrc:指定源矩形區域左上角的Y軸坐標,按邏輯單位。
              nWidthSrc:指定源矩形粗體區域的寬度,按邏輯單位。
              nHeightSrc:指定源矩形區域的高度,按邏輯單位。
              blendFunction:指定用于源位圖和目標位圖使用的alpha混合功能,用于整個源位圖的全局alpha值和格式信息。源和目標混合功能當前只限為AC_SRC_OVER。
              最后一個參數blendFunction是一個BLENDFUNCTION結構。BLENDFUNCTION結構控制源和目標位圖的混合方式,它的BlendOp字段指明了源混合操作,但只支持AC_SRC_OVER,即根據源alpha值把源圖像疊加到目標圖像上。OpenGL的alpha混合還支持其他的方式,如常量顏色源。下一個字段BlendFalgs必須是0,也是為以后的應用保留的。最后一個字段AlphaFormat有兩個選擇:0表示常量alpha值,AC_SRC_ALPHA表示每個像素有各自的alpha通道。
              如果AlphaFormat字段為0,源位圖中的所有像素使用同樣的常量alpha值,即SourceConstantAlpha字段中的值,該值實際上是0和255,而不是0和1。這里0表示完全透明,255表示完全不透明。目標像素以255-SourceConstantAlpha值作為alpha值。
              如果AlphaFormat字段的值是AC_SRC_ALPHA,源設備表面的每個像素必須有各自的alpha通道。即,必須是32-bpp的物理設備上下文,或是選中了32-bpp DDB和DIB段的內存設備上下文。這些情況下,每個源像素有4個8位通道:紅、綠、藍和alpha。每個像素的alpha通道和SourceConstantAlpha字段一起用于把源和目標混合起來。
            ?
            BLENDFUNCTION
            ?? 這個結構體不太明白。
            ?
            至于顏色的繪制,就需要一個重要的函數了,這就是AlphaBlend。在Windows2000之后,微軟的GDI+逐漸興起,其中最重要的就是支持了 AlphaBlend。這是一個可以讓鼠標透明有陰影,讓圖標支持消除鋸齒的增強型Icon格式,讓窗口半透明的函數。微軟在GDI+的計劃中添加了這一 API,硬件廠商也跟著支持,所有的廠家都加入到這個新的游戲中來。其實,Photoshop早在1.0版本的時候,就有這個功能了,畢竟軟件和系統的考慮和出發點不一樣,微軟支持的要晚些,因為他要考慮到整個系統的性能和內存占用情況。

            ??? 使用AlphaBlend,就必須將RGB格式的Canvas升級為ARGB的透明格式。比如,我們將黑色看成是RGB都為0的顏色,計算機的表達式為 0x000000,每兩個0表示16進制的1個字節,而ARGB格式則為0xFF000000,其中最高字節的0xFF就是16進制的255,也就是說這個黑色是全部顯示的。注意,計算機的術語和我們平時理解的百分比不一樣,一般都以256為100%的概念,這也和RGB的取值范圍一樣。比如,一個半透明的黑色,就是0x80000000,0x80正好是16進制的128。AlphaBlend除了要考慮畫布上原來的顏色,還要考慮到疊加顏色的RGB值和透明度,這一公式較復雜,而且也是技術核心,這里就不再羅列了,有興趣的可以參考很多關于Photoshop實現原理的軟件。

            ??? 使用AlphaBlend,我們可以將一個單位的黑色,分配到鄰近的4個點上,將0x25000000這樣的顏色,也就是半透明的黑色,疊加到原來白色的區域上。不過,這一區域也可能有其他的顏色,所以,一個區域如果不停的疊加黑色,也就會越來越黑。

            郊果圖:

            查看更多精彩圖片

            代碼:

            void DrawAlphaBlend (HWND hWnd, HDC hdcwnd)
            {
            ??? HDC hdc;?????????????? // handle of the DC we will create
            ??? BLENDFUNCTION bf;????? // structure for alpha blending
            ??? HBITMAP hbitmap;?????? // bitmap handle
            ??? BITMAPINFO bmi;??????? // bitmap header
            ??? VOID *pvBits;????????? // pointer to DIB section
            ??? ULONG?? ulWindowWidth, ulWindowHeight;????? // window width/height
            ??? ULONG?? ulBitmapWidth, ulBitmapHeight;????? // bitmap width/height
            ??? RECT??? rt;??????????? // used for getting window dimensions
            ??? UINT32?? x,y;????????? // stepping variables
            ??? UCHAR ubAlpha;???????? // used for doing transparent gradient
            ??? UCHAR ubRed;???????
            ??? UCHAR ubGreen;
            ??? UCHAR ubBlue;
            ??? float fAlphaFactor;??? // used to do premultiply
            ???????????
            ??? // get window dimensions
            ??? GetClientRect(hWnd, &rt);
            ???
            ??? // calculate window width/height
            ??? ulWindowWidth = rt.right - rt.left;?
            ??? ulWindowHeight = rt.bottom - rt.top;?

            ??? // make sure we have at least some window size
            ??? if ((!ulWindowWidth) || (!ulWindowHeight))
            ??????? return;

            ??? // divide the window into 3 horizontal areas
            ??? ulWindowHeight = ulWindowHeight / 3;

            ??? // create a DC for our bitmap -- the source DC for AlphaBlend
            ??? hdc = CreateCompatibleDC(hdcwnd);
            ???
            ??? // zero the memory for the bitmap info
            ??? ZeroMemory(&bmi, sizeof(BITMAPINFO));

            ??? // setup bitmap info
            ??? // set the bitmap width and height to 60% of the width and height of each of the three horizontal areas. Later on, the blending will occur in the center of each of the three areas.
            ??? bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
            ??? bmi.bmiHeader.biWidth = ulBitmapWidth = ulWindowWidth - (ulWindowWidth/5)*2;
            ??? bmi.bmiHeader.biHeight = ulBitmapHeight = ulWindowHeight - (ulWindowHeight/5)*2;
            ??? bmi.bmiHeader.biPlanes = 1;
            ??? bmi.bmiHeader.biBitCount = 32;???????? // four 8-bit components
            ??? bmi.bmiHeader.biCompression = BI_RGB;
            ??? bmi.bmiHeader.biSizeImage = ulBitmapWidth * ulBitmapHeight * 4;

            ??? // create our DIB section and select the bitmap into the dc
            ??? hbitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0x0);
            ??? SelectObject(hdc, hbitmap);

            ??? // in top window area, constant alpha = 50%, but no source alpha
            ??? // the color format for each pixel is 0xaarrggbb
            ??? // set all pixels to blue and set source alpha to zero
            ??? for (y = 0; y < ulBitmapHeight; y++)
            ??????? for (x = 0; x < ulBitmapWidth; x++)
            ??????????? ((UINT32 *)pvBits)[x + y * ulBitmapWidth] = 0x000000ff;

            ??? bf.BlendOp = AC_SRC_OVER;
            ??? bf.BlendFlags = 0;
            ??? bf.SourceConstantAlpha = 0x7f;? // half of 0xff = 50% transparency
            ??? bf.AlphaFormat = 0;???????????? // ignore source alpha channel

            ??? if (!AlphaBlend(hdcwnd, ulWindowWidth/5, ulWindowHeight/5,
            ??????????????????? ulBitmapWidth, ulBitmapHeight,
            ??????????????????? hdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf))
            ??????? return;???????????????????? // alpha blend failed
            ???
            ??? // in middle window area, constant alpha = 100% (disabled), source
            ??? // alpha is 0 in middle of bitmap and opaque in rest of bitmap
            ??? for (y = 0; y < ulBitmapHeight; y++)
            ??? {???
            ??for (x = 0; x < ulBitmapWidth; x++)
            ??{
            ???if ((x > (int)(ulBitmapWidth/5)) && (x < (ulBitmapWidth-ulBitmapWidth/5)) &&
            ??????????????? (y > (int)(ulBitmapHeight/5)) && (y < (ulBitmapHeight-ulBitmapHeight/5)))
            ??????????????? //in middle of bitmap: source alpha = 0 (transparent).
            ??????????????? // This means multiply each color component by 0x00.
            ??????????????? // Thus, after AlphaBlend, we have a, 0x00 * r,
            ??????????????? // 0x00 * g,and 0x00 * b (which is 0x00000000)
            ??????????????? // for now, set all pixels to red
            ??????????????? ((UINT32 *)pvBits)[x + y * ulBitmapWidth] = 0x00ff0000;
            ??????????? else
            ??????????????? // in the rest of bitmap, source alpha = 0xff (opaque)
            ??????????????? // and set all pixels to blue
            ??????????????? ((UINT32 *)pvBits)[x + y * ulBitmapWidth] = 0xff0000ff;
            ??}
            ?}
            ???
            ??? bf.BlendOp = AC_SRC_OVER;
            ??? bf.BlendFlags = 0;
            ??? bf.AlphaFormat = AC_SRC_ALPHA;? // use source alpha
            ??? bf.SourceConstantAlpha = 0xff;? // opaque (disable constant alpha)
            ??
            ??? if (!AlphaBlend(hdcwnd, ulWindowWidth/5, ulWindowHeight/5+ulWindowHeight, ulBitmapWidth, ulBitmapHeight, hdc, 0, 0, ulBitmapWidth, ulBitmapHeight, bf))
            ??????? return;

            ??? // bottom window area, use constant alpha = 75% and a changing
            ??? // source alpha. Create a gradient effect using source alpha, and
            ??? // then fade it even more with constant alpha
            ??? ubRed = 0x00;
            ??? ubGreen = 0x00;
            ??? ubBlue = 0xff;
            ???
            ??? for (y = 0; y < ulBitmapHeight; y++)
            ??????? for (x = 0; x < ulBitmapWidth; x++)
            ??????? {
            ??????????? // for a simple gradient, base the alpha value on the x
            ??????????? // value of the pixel
            ??????????? ubAlpha = (UCHAR)((float)x / (float)ulBitmapWidth * 255);
            ??????????? //calculate the factor by which we multiply each component
            ??????????? fAlphaFactor = (float)ubAlpha / (float)0xff;
            ??????????? // multiply each pixel by fAlphaFactor, so each component
            ??????????? // is less than or equal to the alpha value.
            ??????????? ((UINT32 *)pvBits)[x + y * ulBitmapWidth]
            ??????????????? = (ubAlpha << 24) |?????????????????????? //0xaa000000
            ???????????????? ((UCHAR)(ubRed * fAlphaFactor) << 16) |? //0x00rr0000
            ???????????????? ((UCHAR)(ubGreen * fAlphaFactor) << 8) | //0x0000gg00
            ???????????????? ((UCHAR)(ubBlue?? * fAlphaFactor));????? //0x000000bb
            ??????? }

            ??? bf.BlendOp = AC_SRC_OVER;
            ??? bf.BlendFlags = 0;
            ??? bf.AlphaFormat = AC_SRC_ALPHA;?? // use source alpha
            ??? bf.SourceConstantAlpha = 0xbf;?? // use constant alpha, with
            ???????????????????????????????????? // 75% opaqueness

            ??? AlphaBlend(hdcwnd, ulWindowWidth/5,
            ?????????????? ulWindowHeight/5+2*ulWindowHeight, ulBitmapWidth,
            ?????????????? ulBitmapHeight, hdc, 0, 0, ulBitmapWidth,
            ?????????????? ulBitmapHeight, bf);

            ??? // do cleanup
            ??? DeleteObject(hbitmap);
            ??? DeleteDC(hdc);
            ?
            }

            一本大道加勒比久久综合| 99久久久国产精品免费无卡顿| 波多野结衣中文字幕久久| 久久久久久久久久久精品尤物| 亚洲欧美久久久久9999| 亚洲精品高清一二区久久| 性做久久久久久免费观看| 香蕉久久AⅤ一区二区三区| 香蕉久久夜色精品国产尤物| 亚洲国产高清精品线久久| 亚洲人成无码久久电影网站| 香蕉久久夜色精品国产尤物| 亚洲欧美成人综合久久久| 久久精品国产亚洲精品2020| 久久66热人妻偷产精品9| 9191精品国产免费久久| 久久久久亚洲精品中文字幕| 久久人妻无码中文字幕| 无码超乳爆乳中文字幕久久| 丁香狠狠色婷婷久久综合| 国产精品综合久久第一页| 国产成人综合久久精品红| 精品无码久久久久久尤物| 久久精品国产一区二区| 久久久亚洲欧洲日产国码是AV| 国产婷婷成人久久Av免费高清| 久久国产成人午夜aⅴ影院| 久久久精品人妻一区二区三区蜜桃| 精品国际久久久久999波多野| 狠狠精品久久久无码中文字幕 | 久久99国产精一区二区三区| 精品久久久久久国产牛牛app| 久久久久se色偷偷亚洲精品av | 久久久久久久久无码精品亚洲日韩| 777久久精品一区二区三区无码 | 中文国产成人精品久久不卡| 欧美久久精品一级c片片| 久久久久久精品免费免费自慰| 亚洲一区中文字幕久久| 亚洲AV日韩精品久久久久久| 精品久久人人做人人爽综合|