user-0 提问: 背景画刷为方格线条时，如何设置方格中为透明？

回复:
user: user-1
content: 用GDI不用GDIplus就这么麻烦。 用GDI的好处是速度快。

user: user-1
content: BLENDFUNCTION  The BLENDFUNCTION structure controls blending by specifying the blending functions for source and destination bitmaps.   typedef struct _BLENDFUNCTION {     BYTE     BlendOp;     BYTE     BlendFlags;     BYTE     SourceConstantAlpha;     BYTE     AlphaFormat; }BLENDFUNCTION, *PBLENDFUNCTION, *LPBLENDFUNCTION;   Members  BlendOp  Specifies the source blend operation. Currently, the only source and destination blend operation that has been defined is AC_SRC_OVER. For details, see the following Remarks section.  BlendFlags  Must be zero.  SourceConstantAlpha  Specifies an alpha transparency value to be used on the entire source bitmap. The SourceConstantAlpha value is combined with any per-pixel alpha values in the source bitmap. If you set SourceConstantAlpha to 0, it is assumed that your image is transparent. Set the SourceConstantAlpha value to 255 (opaque) when you only want to use per-pixel alpha values.  AlphaFormat  This member controls the way the source and destination bitmaps are interpreted. AlphaFormat has the following value: Value Meaning  AC_SRC_ALPHA  This flag is set when the bitmap has an Alpha channel.     Remarks When the AC_SRC_OVER operation is used, the source bitmap is placed over the destination bitmap based on the alpha values of the source pixels.   If the source bitmap has no per-pixel alpha value, the blend is based on a constant source alpha value as shown in the following table.  Dst.Red    = Src.Red  * ConstantAlpha + (1 - ConstantAlpha) * Dst.Red   Dst.Green    = Src.Green    * ConstantAlpha + (1 - ConstantAlpha) * Dst.Green   Dst.Blue    = Src.Blue  * ConstantAlpha + (1 - ConstantAlpha) * Dst.Blue     If the source bitmap has per-pixel alpha, the constant alpha is not used (0xff) as shown in the following table.  Dst.Red  = Src.Red  + (1 - Src.Alpha) * Dst.Red   Dst.Green    = Src.Green    + (1 - Src.Alpha) * Dst.Green   Dst.Blue  = Src.Blue  + (1 - Src.Alpha) * Dst.Blue     If the destination bitmap has an alpha channel, then:   Dest.alpha    = Src.Alpha    + (1 - SrcAlpha) * Dst.Alpha     If the source has pixel per alpha, the constant alpha is not 0xff.  Dst.Red    = Src.Red   - Src.Red = Src.Red * ConstantAlpha;    Dst.Red  = Src.Red   - Src.Green = Src.Green * ConstantAlpha ;   Dst.Red    = Src.Red   - Src.Blue = Src.Blue * ConstantAlpha ;   Dst.Red   =  Src.Red  + (1 - Src.Alpha) * Dst.Red   Dst.Red   =  Src.Green     + (1 - Src.Alpha) * Dst.Green   Dst.Red   =  Src.Blue  + (1 - Src.Alpha) * Dst.Blue     QuickInfo   Windows NT: Requires version 5.0 or later.   Windows: Requires Windows 98 or later.   Windows CE: Unsupported.   Header: Declared in wingdi.h.  See Also Bitmaps Overview, Bitmap Structures

user: user-1
content: AlphaBlend  The AlphaBlend function displays bitmaps that have transparent or semitransparent pixels.  AlphaBlend(   HDC hdcDest,   int nXOriginDest,   int nYOriginDest,   int nWidthDest,   int hHeightDest,   HDC hdcSrc,   int nXOriginSrc,   int nYOriginSrc,   int nWidthSrc,   int nHeightSrc,   BLENDFUNCTION blendFunction );   Parameter hdcDest  Handle to the destination device context.  nXOriginDest  Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle.  nYOriginDest  Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle.  nWidthDest  Specifies the width, in logical units, of the destination rectangle.  hHeightDset  Handle to the height, in logical units, of the destination rectangle.  hdcSrc  Handle to the source device context.  nXOriginSrc  Specifies the x-coordinate, in logical units, of the upper-left corner of the source rectangle.  nYOriginSrc  Specifies the y-coordinate, in logical units, of the upper-left corner of the source rectangle.  nWidthSrc  Specifies the width, in logical units, of the source rectangle.  nHeightSrc  Specifies the height, in logical units, of the source rectangle.  blendFunction  Specifies the alpha blending function for source and destination bitmaps, a global alpha value to be applied to the entire source bitmap, and format information for the source bitmap. The source and destination blend functions are currently limited to AC_SRC_OVER. See the BLENDFUNCTION and EMRALPHABLEND structures.  Return Values If the function succeeds, the return value is TRUE.  If the function fails, the return value is FALSE.   Windows NT: To get extended error information, callGetLastError.   Remarks If the source rectangle and destination rectangle are not the same size, the source bitmap is stretched to match the destination rectangle. If the SetStretchBltMode function is used, the iStretchMode value is BLACKONWHITE and WHITEONBLACK; the iStretchMode value is automatically converted to COLORONCOLOR for this function.   The destination coordinates are transformed by using the transformation currently specified for the destination device context. The source coordinates are transformed by using the transformation currently specified for the source device context.   An error occurs (and the function returns FALSE) if the source device context identifies an enhanced metafile device context.   If destination and source bitmaps do not have the same color format, AlphaBlend converts the source bitmap to match the destination bitmap.   AlphaBlend does not support mirroring. If either the width or height of the source or destination is negative, this call will fail.   Note: The SourceConstantaAlpha member of BLENDFUNCTION specifies an alpha transparancy value to be used on the entire source bitmap. The SourceConstantAlpha value is combined with any per-pixel alpha values. If you set SourceConstantAlpha to 0, it is assumed that your image is transparent. Set the SourceConstantAlpha value to 255 (indicates that the image is opaque) when you only want to use per-pixel alpha values.   QuickInfo   Windows NT: Requires version 5.0 or later.   Windows: Requires Windows 98 or later.   Windows CE: Unsupported.   Header: Declared in wingdi.h.   Import Library: Included as a resource in msimg32.dll.  See Also Bitmaps Overview, Bitmap Functions

user: user-1
content: 透明应该改用GDIplus，我觉得。

user: user-2
content: 引用 楼主 六道佩恩 的回复:背景画刷为HS_CROSS时，方格中的颜色是背景色，通过SetBkColor设置，但通过SetBkMode设置背景透明却无效？为什么？能否做到这种方格线条中间透明的背景效果吗？ 对透明的理解：在窗体或控件输出的字符串或图形是有背景色的。而窗体或控件也是有背景色的。这两种背景色一般是不同的， 这样输出图形或字符串时，两种背景（窗体或控件的背景色、符串或图形的背景色）重叠在一起了，影响美观，为了不让这两种背景重叠，可以设置背景模式为TRANSPARENT（透明的），即让输出的字符串或图形的背景色不可见。可以这样理解：把输出的字符串或图形的背景色看成是一物体，窗体或控件看成另一物体，字符串或图形在窗体或控件前面挡住窗体或控件了（因为输出 的字符串或图形是画在窗体或控件上的，所以可以这么类比），但是透过字符串或图形能看到窗体或控件。即字符串或图形的背 景色所代表的物体是透明的。  画刷为HS_CROSS时，系统用这个位图去刷背景，你说的透明和SetBkMode的透明是两回事 你的要求我觉得用SetBkMode是无法完成的 楼上说的TransparentBlt，关键在最后一个参数，如何将源像素和背景像素相互作用后显示出来

user: user-1
content: https://www.baidu.com/s?wd=TransparentBlt

user: user-1
content: TransparentBlt [This is preliminary documentation and subject to change.]   The TransparentBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context.  BOOL TransparentBlt(   HDC hdcDest,   int nXOriginDest,   int nYOriginDest,   int nWidthDest,   int hHeightDest,   HDC hdcSrc,   int nXOriginSrc,   int nYOriginSrc,   int nWidthSrc,   int nHeightSrc,   UINT crTransparent );   Parameters hdcDest  Handle to the destination device context.  nXOriginDest  Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle.  nYOriginDest  Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle.  nWidthDest  Specifies the width, in logical units, of the destination rectangle.  hHeightDest  Handle to the height, in logical units, of the destination rectangle.  hdcSrc  Handle to the source device context.  nXOriginSrc  Specifies the x-coordinate, in logical units, of the source rectangle.  nYOriginSrc  Specifies the y-coordinate, in logical units, of the source rectangle.  nWidthSrc  Specifies the width, in logical units, of the source rectangle.  nHeightSrc  Specifies the height, in logical units, of the source rectangle.  crTransparent  The RGB color in the source bitmap to treat as transparent.  Return Values If the function succeeds, the return value is TRUE.  If the function fails, the return value is FALSE.   Windows NT: To get extended error information, callGetLastError.   Remarks The TransparentBlt function is supported for source bitmaps of 4 bits per pixel and 8 bits per pixel. Use AlphaBlend to specify 32-bit per pixel bitmaps with transparency.   If the source and destination rectangles are not the same size, the source bitmap is stretched to match the destination rectangle. When the SetStretchBltMode function is used, the iStretchMode modes of BLACKONWHITE and WHITEONBLACK are converted to COLORONCOLOR for the TransparentBlt function.   The destination device context specifies the transformation type for the destination coordinates. The source device context specifies the transformation type for the source coordinates.   TransparentBlt does not mirror a bitmap if either the width or height, of either the source or destination, is negative.   QuickInfo   Windows NT: Requires version 5.0 or later.   Windows: Requires Windows 98 or later.   Windows CE: Unsupported.   Header: Declared in wingdi.h.   Import Library: Included as a resource in msimg32.dll.  See Also Bitmaps Overview, Bitmap Functions, AlphaBlend, SetStretchBltMode     ?

user: user-0
content: 引用 2 楼 赵4老师 的回复:透明应该改用GDIplus，我觉得。 gdiplus好像都是C++的函数呀？

user: user-0
content: #include "stdio.h" #include "windows.h" LRESULT MyProc( HWND h , UINT msg , WPARAM wParam , LPARAM lParam ) { 	switch( msg ) 	{		 		case WM_PAINT: 			{ 				HDC hdc = GetDC( h );			 				LineTo( hdc, 100 ,100 ); 				SetBkMode( hdc , 1 ); 				SendMessage( h , WM_ERASEBKGND , (WPARAM)hdc , 0 );	 				ReleaseDC( h , hdc ); 				ValidateRect( h , NULL );  			} 			return 0;  		 		case WM_DESTROY: 			PostQuitMessage(0); 			return 0; 	} 	return DefWindowProc( h , msg , wParam , lParam ); } void SetWNDCLASS( WNDCLASS* a, WNDPROC proc, LPCTSTR ClassName ) { 	a->style 			=CS_HREDRAW | CS_VREDRAW;	 	a->lpfnWndProc		=proc;							 	a->cbClsExtra		=0;		 	a->cbWndExtra		=0;	 	a->hInstance		=GetModuleHandle(NULL); 	a->hIcon			=NULL; 	a->hCursor			=LoadCursor(NULL, IDC_ARROW);		 	a->hbrBackground	= CreateHatchBrush ( HS_CROSS , RGB(255,0,0) ); 	a->lpszMenuName	=NULL;	 	a->lpszClassName	=ClassName;	 } ATOM  RegMyClass(  const char* name , WNDPROC  proc) { 	WNDCLASS MyClass; 	SetWNDCLASS ( &MyClass, proc, name );	//处理函数为MyProc,类名为"MyClass" 	return RegisterClass( &MyClass ); } int main( void ) { 	RegMyClass( "MyClass" , MyProc ); 	CreateWindow( "MyClass" , "画刷" ,WS_OVERLAPPEDWINDOW|WS_VISIBLE,  				300,400,500,500,NULL,NULL,NULL,NULL ); 	MSG msg; 	while( GetMessage( &msg , NULL, 0,0 ) ) 	{ 		DispatchMessage( &msg ); 	} 	return 0; }  如果设置透明成功的话，应该能显示红色方格覆盖在一条黑色的线条上，但没有，说明没有实现透明