透明窗口的实现并在透明窗口上能画鼠标轨迹、文字输出和截图放大,小窗口浮动等功能,

本文介绍如何创建一个透明窗口,并实现鼠标轨迹跟踪、文字显示和截图放大的功能。通过C++代码详细展示了从Simple.h到Temp.rc的各个部分实现细节,包括CMainFrame类的处理和资源文件配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:

1、Simple.h

#pragma once
class SimpleApp :
	public CWinApp
{
public:
	SimpleApp();
	~SimpleApp();
	virtual BOOL InitInstance();
	COLORREF m_clrWndTransparencyColorKey;
	COLORREF m_clrWndNoTransparencyColorKey;
};



 

2、Simple.cpp

#include <afxwin.h>
#include "Simple.h"
#include"CMainFrame.h"
#include"resource.h"




SimpleApp::SimpleApp()
{
	//设置窗口透明色
	m_clrWndTransparencyColorKey = RGB(147, 147, 148);
	//缺省的不透明色
	m_clrWndNoTransparencyColorKey = RGB(255, 0, 19);
}


SimpleApp::~SimpleApp()
{
}


BOOL SimpleApp::InitInstance()
{
	// TODO: 在此添加专用代码和/或调用基类
	
	//创建窗口并显示、更新窗口
	m_pMainWnd = new CMainFrame();
	////////////////////////////////////////////////////////////////////////////
	//设置窗口启动时最大化,且成为活动窗口。

	m_nCmdShow = SW_SHOWMAXIMIZED;

	////////////////////////////////////////////////////////////////////////////

	m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();

	////////////////////////////////////////////////////////////////////////////
	//设置窗口的扩展样式:WS_EX_LAYERED,以形成透明窗口风格。

	CMainFrame *pWnd = (CMainFrame *)m_pMainWnd;
	LONG nExStyle = ::GetWindowLong(pWnd->m_hWnd, GWL_EXSTYLE);
	nExStyle |= WS_EX_LAYERED;
	::SetWindowLong(pWnd->m_hWnd, GWL_EXSTYLE, nExStyle);
	//设置窗口:用透明色来实现透明窗口。(还可以通过透明度来设置透明窗口:LWA_ALPHA)
	::SetLayeredWindowAttributes(pWnd->m_hWnd,
		m_clrWndTransparencyColorKey,
		255,
		LWA_COLORKEY);

	////////////////////////////////////////////////////////////////////////////



	return CWinApp::InitInstance();
}

3、CMainFrame.h

#pragma once

class CMainFrame :
	public CFrameWnd
{
public:
	CMainFrame();
	~CMainFrame();

public:
	DECLARE_MESSAGE_MAP()

	afx_msg void OnContextMenu(CWnd* /*pWnd*/, CPoint /*point*/);
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnPaint();
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnOperationClose();
	afx_msg void OnOperationClear();
	afx_msg void OnColorRed();
	afx_msg void OnColorYellow();
	afx_msg void OnColorGreen();
	afx_msg void OnColorBlack();
	afx_msg void OnColorWhite();
	afx_msg void OnLinewidth1();
	afx_msg void OnLinewidth2();
	afx_msg void OnLinewidth5();
	afx_msg void OnLinewidth10();
	afx_msg void OnUpdateColorRed(CCmdUI *pCmdUI);
	afx_msg void OnUpdateColorGreen(CCmdUI *pCmdUI);
	afx_msg void OnUpdateColorYellow(CCmdUI *pCmdUI);
	afx_msg void OnUpdateColorBlack(CCmdUI *pCmdUI);
	afx_msg void OnUpdateColorWhite(CCmdUI *pCmdUI);
	afx_msg void OnUpdateLinewidth1(CCmdUI *pCmdUI);
	afx_msg void OnUpdateLinewidth10(CCmdUI *pCmdUI);
	afx_msg void OnUpdateLinewidth2(CCmdUI *pCmdUI);
	afx_msg void OnUpdateLinewidth5(CCmdUI *pCmdUI);
	afx_msg void OnMouseTrack();
	afx_msg void OnUpdateMouseTrack(CCmdUI *pCmdUI);
	afx_msg void OnTextout();
	afx_msg void OnUpdateTextout(CCmdUI *pCmdUI);
	afx_msg void OnTextcolorBlack();
	afx_msg void OnUpdateTextcolorBlack(CCmdUI *pCmdUI);
	afx_msg void OnTextcolorBlue();
	afx_msg void OnUpdateTextcolorBlue(CCmdUI *pCmdUI);
	afx_msg void OnTextcolorGreen();
	afx_msg void OnUpdateTextcolorGreen(CCmdUI *pCmdUI);
	afx_msg void OnTextcolorRed();
	afx_msg void OnUpdateTextcolorRed(CCmdUI *pCmdUI);
	afx_msg void OnTextcolorWhite();
	afx_msg void OnUpdateTextcolorWhite(CCmdUI *pCmdUI);
	afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
	afx_msg void OnTextpoint125();
	afx_msg void OnUpdateTextpoint125(CCmdUI *pCmdUI);
	afx_msg void OnTextpoint350();
	afx_msg void OnUpdateTextpoint350(CCmdUI *pCmdUI);
	afx_msg void OnTextpoint150();
	afx_msg void OnUpdateTextpoint150(CCmdUI *pCmdUI);
	afx_msg void OnTextpoint250();
	afx_msg void OnUpdateTextpoint250(CCmdUI *pCmdUI);
	afx_msg void OnOperationMinimized();
	afx_msg void OnScreenshots();
	afx_msg void OnUpdateScreenshots(CCmdUI *pCmdUI);
	afx_msg void OnGetMinMaxInfo(MINMAXINFO* lpMMI);
	afx_msg void OnOperationMaxmized();
	afx_msg void OnOperationNormal();


public:
	void DrawInputText(CDC *pDC);
	void SetCaretPosition(CDC *pDC);
	void SetCharCXCY(int nFontSize, LPCTSTR strFont = _T("宋体"));
	void ReSetTextAndCaretPos();
	void ClearScreen(CDC *pDC);
	void KeepNoTransparent(CDC *pDC);
	void CreateMyCaret(CPoint point);
	void DestroyMyCaret();

public:
	CPoint m_ptPrev;//鼠标移动时点(之前的点)
	CPoint m_ptCurrent;//鼠标移动时点(当前的点)

	BOOL m_blMouseDown;//鼠标左键处于按下的状态标志

	COLORREF m_clrLineColor;//线颜色
	COLORREF m_clrFontColor;//字体颜色

	int m_nLineWidth;//线宽

	BOOL m_blTextOut;//是否处于文字输入状态
	BOOL m_blMouseTrack;//是否处于鼠标轨迹状态
	BOOL m_blScreenShots;//是否处于截图放大局面的状态

	CFont m_fontMain;//字体
	int m_nFontSize;//字体大小(字号)
	int m_cxChar;//当前字体字号下的字体宽度
	int m_cyChar;//当前字体字号下的字体高度

	CPoint m_ptCaretLineBeginning;//输入光标的位置点
	CString m_strInputText;//输入的文本
	//int m_nTextPos;//文本字符串的索引
	CString m_strFontName;//字体名称

	CRect m_rtScreenShot;
	BOOL m_blWndFloating;



	
	
};

 

4、CMainFrame.cpp

//#include <afxwin.h>
#include"StdAfx.h"
#include "CMainFrame.h"
#include"Simple.h"
#include"resource.h"

SimpleApp theApp;

BEGIN_MESSAGE_MAP(CMa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值