⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 floatwnd.cpp

📁 透明的浮动子窗口 透明的浮动子窗口 透明的浮动子窗口 透明的浮动子窗口 透明的浮动子窗口 透明的浮动子窗口 透明的浮动子窗口 透明的浮动子窗口
💻 CPP
字号:
// CFloatWnd.cpp : implementation file
//

#include "stdafx.h"
#include "FloatWindow.h"
#include "FloatWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFloatWnd
CFloatWnd* pFloatWnd=NULL;

CFloatWnd::CFloatWnd()
{
}

CFloatWnd::~CFloatWnd()
{

	if(bitmap)
	{
	delete bitmap;
	}
}


BEGIN_MESSAGE_MAP(CFloatWnd, CWnd)
	//{{AFX_MSG_MAP(CFloatWnd)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_TIMER()
	ON_WM_NCHITTEST()
	ON_WM_NCLBUTTONDOWN()
	ON_WM_NCMOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CFloatWnd message handlers

CFloatWnd::CFloatWnd(double width_ratio, double height_ratio)
{

   m_bAdd=true;
	pFloatWnd=this;
	m_startPoint=NULL;
	bitmap=NULL;
	bitmap=new CBitmap;
	bitmap->LoadBitmap(MAKEINTRESOURCE(IDB_BITMAP));
	m_width_ratio=width_ratio;
	m_height_ratio=height_ratio;

}

BOOL CFloatWnd::Create(int left, int top)
{
	BITMAP bm;
	int    count=0;
	//	bm.bmType=0;
	
	count=(bitmap->GetObject(sizeof(BITMAP),&bm));
	if(count==0)
		MessageBoxEx(NULL,_T("位图资源选择错误"),_T("系统错误"),MB_OK|MB_ICONSTOP,0x0804);
	m_wndWidth=bm.bmWidth;
	m_wndHeight=bm.bmHeight;

	m_winWidth=(int)(m_wndWidth*m_width_ratio);
	m_winHeight=(int)(m_wndHeight*m_height_ratio);
	return CWnd::CreateEx(WS_EX_TOPMOST|WS_EX_WINDOWEDGE/*|WS_EX_TRANSPARENT*/,AfxRegisterWndClass(CS_NOCLOSE,0,0,0),_T(""),WS_POPUP|WS_VISIBLE,
		left,top,m_winWidth,m_winHeight,NULL,NULL);
//	return 1;

}



BOOL CFloatWnd::DrawTransparentBitmap(int x, int y, int nWidth, int nHeight, CDC *pDC, UINT IDImage)
{
	
  CDC ImageDC,MaskDC; 
   CBitmap Image,*pOldImage;
   BITMAP bmpInfo;
   CBitmap maskBitmap,*pOldMaskDCBitmap; 
   COLORREF rgbMask;

   Image.LoadBitmap(IDImage); 
   ImageDC.CreateCompatibleDC(pDC); 
   pOldImage = ImageDC.SelectObject(&Image); 
   MaskDC.CreateCompatibleDC(pDC); 

   //得到位图的信息
   Image.GetBitmap(&bmpInfo);
 
   maskBitmap.CreateBitmap(bmpInfo.bmWidth, bmpInfo.bmHeight, 1, 1, NULL ); 
   pOldMaskDCBitmap = MaskDC.SelectObject( &maskBitmap );
   //
  // MaskDC.SetBkColor(RGB(0, 0, 0)); 
  // pDC->StretchBlt(x,y,nWidth, nHeight, &MaskDC, 0, 0 ,bmpInfo.bmWidth ,bmpInfo.bmHeight, SRCAND); 
   
   rgbMask = ImageDC.GetPixel(0,0);
   //把位图拷到MaskDC
   ImageDC.SetBkColor(rgbMask); 
   MaskDC.BitBlt( 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &ImageDC, 0, 0, SRCCOPY ); 
   //ImageDC 只剩位图前景,其它为黑色  ??SRCAND ,desDC的 背景,srcDC的前景去和srcDC的前景做AND?
   ImageDC.SetBkColor(RGB(0, 0, 0)); 
   ImageDC.SetTextColor(RGB(255,255,255)); 
   ImageDC.BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &MaskDC, 0, 0 , SRCAND); 
   //pDC 作为背景,位图前景为黑色
    pDC->StretchBlt(x,y,nWidth, nHeight, &MaskDC, 0, 0 ,bmpInfo.bmWidth ,bmpInfo.bmHeight, SRCAND); 
   //pDC 作为背景,加入位图前景
   pDC->StretchBlt(x,y,nWidth, nHeight, &ImageDC, 0, 0 ,bmpInfo.bmWidth ,bmpInfo.bmHeight,SRCPAINT); 

   MaskDC.SelectObject(pOldMaskDCBitmap); 
   ImageDC.SelectObject(pOldImage);
   
   return TRUE;

}

int CFloatWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	//加入WS_EX_LAYERED扩展属性
	SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
					GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
	HINSTANCE hInst = LoadLibrary("User32.DLL"); 
	if(hInst) 
	{
		typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
		MYFUNC fun = NULL;
         //取得SetLayeredWindowAttributes函数指针 
          fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
          if(fun)
			  fun(this->GetSafeHwnd(),0,100,0x2); 
          FreeLibrary(hInst); 
	}
	m_iCount=0;
	this->SetTimer(1,10,0);
	//
	if (!ToolTipCtrl.Create(this,TTS_ALWAYSTIP))
	{
		TRACE(_T("Unable To create ToolTip\n"));           
		return -1;
	}	
	return 0;
}

void CFloatWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	CDC memDC;
	memDC.CreateCompatibleDC(&dc);
	CBitmap *pOld=memDC.SelectObject(bitmap);
	if(pOld==NULL)
		return;
	dc.StretchBlt(0,0,m_winWidth,m_winHeight,&memDC,0,0,m_wndWidth,
		m_wndHeight,/*SRCCOPY*/SRCPAINT);
	memDC.SelectObject(pOld);
	// Do not call CWnd::OnPaint() for painting messages
}


void CFloatWnd::OnTimer(UINT nIDEvent) 
{
	if(m_bAdd)
	{
		m_iCount++;
		if(m_iCount>=255)
		{
			m_bAdd=false;
		}
	}else
	{
		m_iCount--;
		if(m_iCount<=0)
		{
			m_bAdd=true;
		}

	}
	HINSTANCE hInst = LoadLibrary("User32.DLL"); 
	if(hInst) 
	{
		typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
		MYFUNC fun = NULL;
         //取得SetLayeredWindowAttributes函数指针 
          fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
          
		  if(fun)fun(this->GetSafeHwnd(),0,m_iCount,2); 
          FreeLibrary(hInst); 
	}
	CWnd::OnTimer(nIDEvent);
}

UINT CFloatWnd::OnNcHitTest(CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	 CWnd::OnNcHitTest(point);
	 return HTCAPTION ;
}


void CFloatWnd::OnNcLButtonDown(UINT nHitTest, CPoint point) 
{
//	调整窗口位置
	CRect rt,rt1;
	this->GetWindowRect(&rt);
	CWnd::OnNcLButtonDown(nHitTest, point);
	this->GetWindowRect(&rt1);
	CPoint pt;
	if(GetCursorPos(&pt))
	{
	if(!rt1.PtInRect(pt))
	{
	this->MoveWindow(&rt);
	}
	}
	
}

void CFloatWnd::OnNcMouseMove(UINT nHitTest, CPoint point) 
{
//	显示提示信息
	CRect rc;
    this->GetWindowRect(rc);
	if(rc.PtInRect(point))
	{

		MSG msg;
		msg.message=WM_MOUSEMOVE;
		msg.hwnd=this->m_hWnd;
		msg.lParam=0;
		msg.wParam=0;
		this->ToolTipCtrl.Activate(true);
		ToolTipCtrl.RelayEvent(&msg);
       ToolTipCtrl.AddTool(this,_T("一颗红心向着党"));
		
		
		
	}
	CWnd::OnNcMouseMove(HTCAPTION, point);

}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -