SetLayeredWindowAttributes 设置窗口透明

#ifndef _ROPWINDOW_H_
#define _ROPWINDOW_H_
#include "stdafx.h"
class RopWindow : public CWindowImpl<RopWindow>
{
public:
	DECLARE_WND_CLASS("rop widnow")

	BEGIN_MSG_MAP(theClass)
		MESSAGE_HANDLER(WM_PAINT,OnPaint)
		MESSAGE_HANDLER(WM_CREATE,OnCreate)
		MESSAGE_HANDLER(WM_DESTROY,OnDes)
	END_MSG_MAP()

	LRESULT OnPaint(UINT umsg,WPARAM wParam, LPARAM lParam,BOOL&bHandled)
	{
#if 1
		RECT rc,rcTxt = {0,0,100,100};
		CPaintDC dc = m_hWnd;
		dc.SetBkMode(TRANSPARENT);
		//dc.SetROP2(R2_NOT);
		dc.DrawText("Hello World!",strlen("Hello World!"),&rcTxt,0);

#if 1
		
		COLORREF backgndcolor = RGB(127,127,127);
		CBrush brush;
		brush.CreateSolidBrush(backgndcolor);
		GetClientRect(&rc);
		dc.FillRect(&rc,brush);

		RECT rc2 = {100,100,400,400};
		dc.Rectangle(&rc2);
#endif
#endif

		
		return false;
	}

	LRESULT OnCreate(UINT umsg,WPARAM wParam, LPARAM lParam,BOOL&bHandled)
	{
		CenterWindow();
		ModifyStyle(WS_VISIBLE | WS_BORDER | WS_CAPTION | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_LAYERED | WS_EX_LAYERED);
		LONG lWindowLong = ::GetWindowLong(m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED ;//| WS_EX_NOACTIVATE;
		::SetWindowLong(m_hWnd, GWL_EXSTYLE, lWindowLong); 
		 /*Set alpha layer*/
		const BYTE AlphaPercent = 60;
		SetLayeredWindowAttributes(m_hWnd, RGB(255,255,255), AlphaPercent, LWA_COLORKEY | LWA_ALPHA);
		RedrawWindow( NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN); 
		SetWindowPos(HWND_TOPMOST,0,0,0,0,SWP_NOSIZE);


		return false;
	}

	LRESULT OnDes(UINT umsg,WPARAM wParam, LPARAM lParam,BOOL&bHandled)
	{
		PostQuitMessage(0);
		return false;
	}

	LRESULT OnHit(UINT umsg,WPARAM wParam, LPARAM lParam,BOOL&bHandled)
	{
		//PostQuitMessage(0);
		return HTCAPTION ;
	}


	LRESULT OnMouseMove(UINT umsg,WPARAM wParam, LPARAM lParam,BOOL&bHandled)
	{
		//PostQuitMessage(0);
		return false ;
	}

	LRESULT OnActive(UINT umsg,WPARAM wParam, LPARAM lParam,BOOL&bHandled)
	{
		//PostQuitMessage(0);
		return false ;
	}

private:
	CBitmap mClientGndBmp;
	
};
#endif

你可能感兴趣的:(SetLayeredWindowAttributes 设置窗口透明)