Home > Software engineering >  C using MFC to do a screensaver is not wrong but flash back
C using MFC to do a screensaver is not wrong but flash back

Time:10-05

To the Po once program

 ScreenWnd. HThe 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, dwords 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 onm ouseMove (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)
//registered window class
LpszClassName=AfxRegisterWndClass (CS_HREDRAW | CS_VREDRAW,
AfxGetApp () - & gt; LoadCursor (IDC_CURSOR),
//the cursor for custom cursor
CreateSolidBrush (RGB (0, 0, 0)));
//form the background color to black
CRect rc;
The GetDesktopWindow () - & gt; GetClientRect (rc);
//screen position and size information
if (! CreateEx (WS_EX_TOPMOST lpszClassName, _T (" "), WS_POPUP | WS_VISIBLE | WS_MAXIMIZE, rc, AfxGetMainWnd (), 0))
//create forms that covers the entire screen
PostMessage (WM_CLOSE);
//create a failure is to end the application
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 the default
CClientDC dc (this);
The SetTimer (1, 1000, NULL);
The CWnd: : OnTimer (nIDEvent);
}


Void CScreenWnd: : OnPaint ()
{
CPaintDC dc (this);//device context for painting
CRect the rect.
GetClientRect (the 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 (& amp; MyFont);
Dc. SetTextColor (RGB (abs (rand ()) % 256, abs (rand ()) % 256, abs (rand ()) % 256));
Dc. SetBkColor (RGB (0, 0, 0));
Cstrings STR (" My ScreenSaves!" );
Dc. ExtTextOut (the rect. Width ()/2 - STR. GetLength () * 10, the rect. Height ()/8 * 3, ETO_OPAQUE, NULL, STR, NULL);
Dc. SelectObject (pOldFont);//TODO: Add your message handler code here
//the Do not call CWnd: : OnPaint () for painting the messages
}


Void CScreenWnd: : OnClose ()
{
//TODO: Add your message handler code here and/or call the default
KillTimer (1);
The 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, dwords dwThreadID)
{

if(! BActive)
PostMessage (WM_CLOSE);
//TODO: Add your message handler code here
The CWnd: : OnActivateApp (bActive dwThreadID);
}


Void CScreenWnd: : OnLButtonDown (UINT nFlags, CPoint point)
{
//TODO: Add your message handler code here and/or call the default
PostMessage (WM_CLOSE);
The CWnd: : OnLButtonDown (nFlags, point);
}


Void CScreenWnd: : OnRButtonDown (UINT nFlags, CPoint point)
{
//TODO: Add your message handler code here and/or call the default
PostMessage (WM_CLOSE);
The CWnd: : OnRButtonDown (nFlags, point);
}


Void CScreenWnd: : OnMButtonDown (UINT nFlags, CPoint point)
{
//TODO: Add your message handler code here and/or call the default
PostMessage (WM_CLOSE);
The CWnd: : OnMButtonDown (nFlags, point);
}


Void CScreenWnd: : onm ouseMove (UINT nFlags, CPoint point)
{
//TODO: Add your message handler code here and/or call the default
The static CPoint staticPoint (1, 1);
CPoint temp (1, 1);
If (temp!=staticPoint)
PostMessage (WM_CLOSE);
The else staticPoint=point;
The CWnd: : onm ouseMove (nFlags, point);

}


Void CScreenWnd: : OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags)
{
//TODO: Add your message handler code here and/or call the default
PostMessage (WM_CLOSE);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related