16,473
社区成员
发帖
与我相关
我的任务
分享
ScreenWnd.h
class CScreenWnd :
public CWnd
{
public:
CScreenWnd();
~CScreenWnd();
private:
LPCTSTR lpszClassName;
public:
BOOL CreateScreenWnd();
DECLARE_MESSAGE_MAP()
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg void OnPaint();
afx_msg void OnClose();
afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
afx_msg void OnActivateApp(BOOL bActive, DWORD dwThreadID);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
};
ScreenWnd.cpp
#include "stdafx.h"
#include "ScreenWnd.h"
CScreenWnd::CScreenWnd()
{
lpszClassName = NULL;
}
CScreenWnd::~CScreenWnd()
{
}
BOOL CScreenWnd::CreateScreenWnd()
{
//TODO:Add yourspecialized code here and/or call the base class
if (!lpszClassName)
//注册窗口类
lpszClassName = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,
AfxGetApp()->LoadCursor(IDC_CURSOR),
//光标为自定义光标
CreateSolidBrush(RGB(0, 0, 0)));
//窗体背景颜色为黑色
CRect rc;
GetDesktopWindow()->GetClientRect(rc);
//获取屏幕位置大小信息
if (!CreateEx(WS_EX_TOPMOST, lpszClassName, _T(""), WS_POPUP | WS_VISIBLE | WS_MAXIMIZE, rc, AfxGetMainWnd(), 0))
//创建能够覆盖整个屏幕的窗体
PostMessage(WM_CLOSE);
//创建失败则结束应用程序
return TRUE;
}
BEGIN_MESSAGE_MAP(CScreenWnd, CWnd)
ON_WM_TIMER()
ON_WM_PAINT()
ON_WM_CLOSE()
ON_WM_ACTIVATE()
ON_WM_ACTIVATEAPP()
ON_WM_LBUTTONDOWN()
ON_WM_MBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_KEYDOWN()
ON_WM_SYSKEYDOWN()
END_MESSAGE_MAP()
void CScreenWnd::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
SetTimer(1, 1000, NULL);
CWnd::OnTimer(nIDEvent);
}
void CScreenWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect;
GetClientRect(rect);
CFont myFont;
myFont. CreateFont(56, 20, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH,L"Verdana" );
CFont *pOldFont = dc.SelectObject(&myFont);
dc.SetTextColor(RGB(abs(rand()) % 256, abs(rand()) % 256, abs(rand()) % 256));
dc.SetBkColor(RGB(0, 0, 0));
CString str("My ScreenSaves!");
dc.ExtTextOut(rect.Width()/2-str.GetLength()*10, rect.Height()/8*3, ETO_OPAQUE, NULL, str, NULL);
dc.SelectObject(pOldFont); // TODO: Add your message handler code here
// Do not call CWnd::OnPaint() for painting messages
}
void CScreenWnd::OnClose()
{
// TODO: Add your message handler code here and/or call default
KillTimer(1);
CWnd::OnClose();
}
void CScreenWnd::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
if (nState == WA_INACTIVE)
PostMessage(WM_CLOSE);
// TODO: Add your message handler code here
CWnd::OnActivate(nState, pWndOther, bMinimized);
}
void CScreenWnd::OnActivateApp(BOOL bActive, DWORD dwThreadID)
{
if(!bActive)
PostMessage(WM_CLOSE);
// TODO: Add your message handler code here
CWnd::OnActivateApp(bActive, dwThreadID);
}
void CScreenWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
PostMessage(WM_CLOSE);
CWnd::OnLButtonDown(nFlags, point);
}
void CScreenWnd::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
PostMessage(WM_CLOSE);
CWnd::OnRButtonDown(nFlags, point);
}
void CScreenWnd::OnMButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
PostMessage(WM_CLOSE);
CWnd::OnMButtonDown(nFlags, point);
}
void CScreenWnd::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
static CPoint staticPoint(-1, -1);
CPoint temp(-1, -1);
if (temp != staticPoint)
PostMessage(WM_CLOSE);
else staticPoint = point;
CWnd::OnMouseMove(nFlags, point);
}
void CScreenWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
PostMessage(WM_CLOSE);
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CScreenWnd::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
PostMessage(WM_CLOSE);
CWnd::OnSysKeyDown(nChar, nRepCnt, nFlags);
}
MyScreenSave.h
#pragma once
#include "stdafx.h"
#ifndef __AFXWIN_H__
#endif
#include "resource.h" // 主符号
#include "ScreenWnd.h"
// CMyScreenSaveApp:
// 有关此类的实现,请参阅 MyScreenSave.cpp
//
class CMyScreenSaveApp : public CWinApp
{
public:
CMyScreenSaveApp();
// Overrides
public:
virtual BOOL InitInstance();
// Implementation
DECLARE_MESSAGE_MAP()
private:
CScreenWnd* m_pScrWnd;
public:
virtual int ExitInstance();
};
extern CMyScreenSaveApp theApp;
MyScreenSave.cpp
#include "stdafx.h"
#include "MyScreenSave.h"
#include "MyScreenSaveDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CMyScreenSaveApp
BEGIN_MESSAGE_MAP(CMyScreenSaveApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
// CMyScreenSaveApp 构造
CMyScreenSaveApp::CMyScreenSaveApp()
{
// 支持重新启动管理器
//m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
}
// 唯一的一个 CMyScreenSaveApp 对象
CMyScreenSaveApp theApp;
// CMyScreenSaveApp 初始化
BOOL CMyScreenSaveApp::InitInstance()
{
AfxEnableControlContainer();
//Standard initialization
//If you are not using these features and wish to reduce the size
//of your final executable, you should remove from the following
//the specific initialization routines you do not need.
//创建全屏窗体实例
CScreenWnd *pWnd = new CScreenWnd();
m_pScrWnd = pWnd;
m_pMainWnd = pWnd;
pWnd->CreateScreenWnd();
return TRUE;
}
int CMyScreenSaveApp::ExitInstance()
{
if (m_pScrWnd)
delete m_pScrWnd;
return CWinApp::ExitInstance();
}