c++ 涂鸦程序

1 新建MFC对话框程序,新建类 

#pragma once
#include <afxwin.h>
class CTransparentWnd :
    public CWnd
{
public:
    virtual BOOL PreTranslateMessage(MSG* pMsg);
    bool CreateTransparentWnd();
    CSize GetScreenSize();
    void GetScreenBitmap();
    CBitmap* m_pBitmap;
    bool m_bStartDraw;
    CPoint m_OldPoint;
    DECLARE_MESSAGE_MAP()
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
};

#include "pch.h"
#include "CTransparentWnd.h"


BOOL CTransparentWnd::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message==WM_KEYDOWN)
	{
		if (pMsg->wParam==VK_ESCAPE)
		{
			ShowWindow(SW_HIDE);
		}
	}

	return CWnd::PreTranslateMessage(pMsg);
}


bool CTransparentWnd::CreateTransparentWnd()
{
	// TODO: 在此处添加实现代码.
	CSize size = GetScreenSize();

	return CWnd::CreateEx(WS_EX_TOPMOST,
		AfxRegisterWndClass(CS_DBLCLKS,LoadCursor(NULL,IDC_CROSS),
			(HBRUSH)GetStockObject(WHITE_BRUSH)),
		_T(""),
		WS_POPUP,
		CRect(0,0,size.cx,size.cy),
		NULL,
		0,NULL);
}


CSize CTransparentWnd::GetScreenSize()
{
	// TODO: 在此处添加实现代码.
	CSize size;
	size.cx = GetSystemMetrics(SM_CXSCREEN);
	size.cy = GetSystemMetrics(SM_CYSCREEN);
	return size;
}


void CTransparentWnd::GetScreenBitmap()
{
	// TODO: 在此处添加实现代码.
	CSize size = GetScreenSize();
	HWND hWnd = ::GetDesktopWindow();
	HDC hDC = ::GetDC(hWnd);
	CDC* pDC = CDC::FromHandle(hDC);
	CDC imageDC;
	imageDC.CreateCompatibleDC(pDC);

	CBitmap bitmap;
	bitmap.CreateCompatibleBitmap(pDC, size.cx, size.cy);
	CBitmap* pOldBitmap = imageDC.SelectObject(&bitmap);
	imageDC.BitBlt(0, 0, size.cx/2, size.cy/2, pDC, 0, 0, SRCCOPY);
	m_pBitmap = imageDC.SelectObject(pOldBitmap);
	imageDC.DeleteDC();
}
BEGIN_MESSAGE_MAP(CTransparentWnd, CWnd)
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()


BOOL CTransparentWnd::OnEraseBkgnd(CDC* pDC)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_pBitmap==NULL)
	{
		return CWnd::OnEraseBkgnd(pDC);
	}
	CDC imageDC;
	imageDC.CreateCompatibleDC(pDC);
	CBitmap* pOldBitmap = imageDC.SelectObject(m_pBitmap);
	CSize size = GetScreenSize();
	pDC->BitBlt(0, 0, size.cx, size.cy, &imageDC, 0, 0,SRCCOPY);
	imageDC.SelectObject(pOldBitmap);
	imageDC.DeleteDC();


	return CWnd::OnEraseBkgnd(pDC);
}


void CTransparentWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (!m_bStartDraw)
	{
		m_bStartDraw = true;
		SetCapture();
		m_OldPoint.x= point.x;
		m_OldPoint.y = point.y;

	}

	CWnd::OnLButtonDown(nFlags, point);
}


void CTransparentWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_bStartDraw)
	{
		m_bStartDraw = false;
		ReleaseCapture();
	}

	CWnd::OnLButtonUp(nFlags, point);
}


void CTransparentWnd::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_bStartDraw)
	{
		if (nFlags&MK_LBUTTON)
		{
			CClientDC dc(this);
			CPen pen(PS_SOLID, 2, RGB(255, 0, 0));
			CPen* pOldPen = dc.SelectObject(&pen);
			dc.MoveTo(m_OldPoint.x, m_OldPoint.y);
			dc.LineTo(point.x, point.y);
			m_OldPoint.x = point.x;
			m_OldPoint.y = point.y;
			dc.SelectObject(pOldPen);
		}
	}

	CWnd::OnMouseMove(nFlags, point);
}

2 实现对话框上启动按钮事件

void CDrawScreenDlg::OnBnClickedBtnStart()
{
	// TODO: 在此添加控件通知处理程序代码
	if (NULL==m_pTransparentWnd->GetSafeHwnd())
	{
		m_pTransparentWnd->CreateTransparentWnd();
	}
	SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
	Sleep(300);
	m_pTransparentWnd->GetScreenBitmap();
	m_pTransparentWnd->ShowWindow(SW_SHOW);
}

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/113921321
今日推荐