您的位置:首页 > 其它

屏幕保护程序

2011-09-25 20:56 141 查看
CWnd::OnActivate

The framework calls this member function when a CWnd object is being activated or deactivated.

afx_msg void OnActivate(

UINT nState,

CWnd* pWndOther,

BOOL bMinimized

);

Parameters

nState

Specifies whether the CWnd is being activated or deactivated. It can be one of the following values:

WA_INACTIVE The window is being deactivated.

WA_ACTIVE The window is being activated through some method other than a mouse click (for example, by use of the keyboard interface to select the window).

WA_CLICKACTIVE The window is being activated by a mouse click.

pWndOther

Pointer to the CWnd being activated or deactivated. The pointer can be NULL, and it may be temporary.

bMinimized

Specifies the minimized state of the CWnd being activated or deactivated. A value of TRUE indicates the window is minimized.

If TRUE, the CWnd is being activated; otherwise deactivated.

Remarks

If the CWnd object is activated with a mouse click, it will also receive an OnMouseActivate member function call.

Note

This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation
of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function.

CWnd::OnActivateApp

The framework calls this member function to all top-level windows of the task being activated and for all top-level windows of the task being deactivated.

afx_msg void OnActivateApp(

BOOL bActive,

DWORD dwThreadID

);

Parameters

bActive

Specifies whether the CWnd is being activated or deactivated. TRUE means the CWnd is being activated. FALSE means the CWnd is being deactivated.

dwThreadID

Specifies the value of the thread ID. If bActive is TRUE, dwThreadID identifies the thread that owns the CWnd being deactivated. If bActive is FALSE,dwThreadID identifies the thread that owns the CWnd being activated.

Remarks

Note

This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation
of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function.

构造自己的窗体CMyWnd

class CMyWnd : public CWnd

{

public:

CMyWnd();

static LPCSTR lpszClassName; //注册类名

public:

BOOL Create();

public:

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CMyWnd)

protected:

virtual void PostNcDestroy();

//}}AFX_VIRTUAL

public:

virtual ~CMyWnd();

protected:

CPoint m_prePoint; //检测鼠标移动

void DrawBitmap(CDC& dc, int nIndexBit);

//{{AFX_MSG(CMyWnd)

afx_msg void OnPaint();

afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);

afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

afx_msg void OnMButtonDown(UINT nFlags, CPoint point);

afx_msg void OnMouseMove(UINT nFlags, CPoint point);

afx_msg void OnRButtonDown(UINT nFlags, CPoint point);

afx_msg void OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);

afx_msg void OnDestroy();

afx_msg void OnTimer(UINT nIDEvent);

afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);

afx_msg void OnActivateApp(BOOL bActive, DWORD dwThreadID);

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

#define ID_TIMER WM_USER+1000

CMyWnd::CMyWnd()

{

m_prePoint=CPoint(-1,-1);

}

CMyWnd::~CMyWnd()

{

}

BEGIN_MESSAGE_MAP(CMyWnd, CWnd)

//{{AFX_MSG_MAP(CMyWnd)

ON_WM_PAINT()

ON_WM_KEYDOWN()

ON_WM_LBUTTONDOWN()

ON_WM_MBUTTONDOWN()

ON_WM_MOUSEMOVE()

ON_WM_RBUTTONDOWN()

ON_WM_SYSKEYDOWN()

ON_WM_DESTROY()

ON_WM_TIMER()

ON_WM_ACTIVATE()

ON_WM_ACTIVATEAPP()

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

LPCSTR CMyWnd::lpszClassName=NULL;

BOOL CMyWnd::Create()

{

if(lpszClassName==NULL)

{

lpszClassName=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,

::LoadCursor(AfxGetResourceHandle(),MAKEINTRESOURCE(IDC_NOCURSOR)));

//注册类;IDC_NOCURSOR为新建光标的ID,这个光标没有任何图案

}

CRect rect(0, 0, ::GetSystemMetrics(SM_CXSCREEN),

::GetSystemMetrics(SM_CYSCREEN));

CreateEx(WS_EX_TOPMOST, lpszClassName, _T(""), WS_VISIBLE|WS_POPUP,

rect.left,rect.top,rect.right - rect.left, rect.bottom - rect.top,

GetSafeHwnd(), NULL, NULL); //创建一个全屏窗口

SetTimer(ID_TIMER, 500, NULL);//计时器,ID_TIMER别忘了定义

return TRUE;

}

//为了防止同时运行两个相同的程序,下面两个函数是必需的

void CMyWnd::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)

{

CWnd::OnActivate(nState,pWndOther,bMinimized);

if(nState==WA_INACTIVE)

PostMessage(WM_CLOSE);

}

void CMyWnd::OnActivateApp(BOOL bActive, DWORD dwThreadID)

{

CWnd::OnActivateApp(bActive, dwThreadID);

if(!bActive) //is being deactivated

PostMessage(WM_CLOSE);

}

//OnPaint()函数将全屏窗口置为黑色

void CMyWnd::OnPaint()

{

CPaintDC dc(this);

CBrush brush(RGB(0,0,0));

CRect rect;

GetClientRect(rect);

dc.FillRect(&rect,&brush);

}

//由计数器调用DrawBitmap()函数,切换图片;

//注意,下面两个函数中的IDB_BITMAP1, dc.BitBlt(0,0,800,600……以及if(nIndexBit>=5)中的

//有关数据依据你的bmp图片个数、尺寸、位置不同而不同,我是选择了5张

//800x600的bmp图片。注意,ID值是连续的,IDB_BITMAP1最小

void CMyWnd::DrawBitmap(CDC& dc, int nIndexBit)

{

CDC dcmem;

dcmem.CreateCompatibleDC(&dc);

CBitmap m_Bitmap;

m_Bitmap.LoadBitmap(IDB_BITMAP1+nIndexBit);

dcmem.SelectObject(m_Bitmap);

dc.BitBlt(0,0,800,600,&dcmem,0,0,SRCCOPY);

}

void CMyWnd::OnTimer(UINT nIDEvent)

{

CClientDC dc(this);

static int nIndexBit=0;

if(nIndexBit>=5)

nIndexBit=0;

DrawBitmap(dc, nIndexBit++);

CWnd::OnTimer(nIDEvent);

}

//响应键盘、鼠标是屏幕保护程序不可缺少的

void CMyWnd::OnKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags)

{

PostMessage(WM_CLOSE);

}

void CMyWnd::OnLButtonDown(UINT nFlags,CPoint point)

{

PostMessage(WM_CLOSE);

}

void CMyWnd::OnMButtonDown(UINT nFlags,CPoint point)

{

PostMessage(WM_CLOSE);

}

void CMyWnd::OnRButtonDown(UINT nFlags,CPoint point)

{

PostMessage(WM_CLOSE);

}

void CMyWnd::OnSysKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags)

{

PostMessage(WM_CLOSE);

}

void CMyWnd::OnMouseMove(UINT nFlags,CPoint point)

{

if(m_prePoint == CPoint(-1,-1))

m_prePoint = point;

else if(m_prePoint!=point)

PostMessage(WM_CLOSE);

}

void CMyWnd::OnDestroy()

{

//删掉计时器

KillTimer(ID_TIMER);

}

void CMyWnd::PostNcDestroy()

{

delete this;

}

在App::InitInstance()中

//显示屏保窗口

CMyWnd* pWnd = new CMyWnd;

pWnd->Create();

m_pMainWnd = pWnd;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: