Whether it is tired of Windows comes with the screen saver, I don't want to do a colorful, people who are rich. Let me tell you how you should do it. First introduce you to the principles of the screen saver. In fact, the screen saver is an ordinary executable program (Exe). Only Windows changed it to an extension (SCR), put it under the system directory, is called by the operating system. This directory for Windows NT and Windows 2000 is C: / WinNT / System32. For Win95, Win98 is C: / Windows and C: / Windows / System Below Starting to start building our own screen saver. 1. Select AppWizard to establish a dialog-based engineering, the project is called Myscreensaver (in the wizard's first step in the wizard: Dialog based, other default options) 2. Prepare 2 800 × 600 BMP images ( You can set the corresponding picture of your hobby, I use two pieces in the example, join the project, IDB_bitmap1 and idb_bitmap2, and pay attention to ensure that these two ID numbers are continuous, if you once These two documents have been added to the project, and these two ID numbers are usually continuous. 3. Create a new class CMYSCREENWND, and each setting is shown below
4. Click "OK" to pop up a warning dialog, as shown below prompts that we cannot find the header file defined by the base class CWnd, do not manage it, click "OK".
5. Add a sentence in MyScreenwnd.h: #include "windef.h", definition of base class CWnd in WINDEF.H 6. Add a public member function in the CMYScreenWnd class of MyScreenwnd.h file, as follows: BOOL CREATE ();
7. Add the MyScreenWnd.cpp the Create () function implementation code: BOOL CMyScreenWnd :: Create () {if (lpszClassName == NULL) {lpszClassName = AfxRegisterWndClass (CS_HREDRAW | CS_VREDRAW, :: LoadCursor (AfxGetResourceHandle (), MAKEINTRESOURCE ( IDC_nocursor)))); // Registration class, IDC_nocursor is the ID of the new cursor (need to draw itself) // This cursor does not have any pattern} CRECT RECT (0, 0, :: getSystemMetrics (sm_cxscreen), :: getSystemmetrics (SM_CYSCREEN) ); // Get the length and width of the display (WS_EX_TOPMOST, LPSZCLASSNAME, _T ("), WS_Visible | WS_POPUP, RECT.LEFT, RECT.TOP, Rect.right-Rect.Top, Rect.Bottom-Rect.top , GetSafehWnd (), null, null); // Create a full screen window setTimer (id_timer, 2000, null); // Setting the timer, 2 seconds to change a picture return true;} 8. CMYScreenWnd in MyScreenwnd.h file In the class, the public variable is added, the prototype Static LPCSTR LPSZCLASSNAME; // Register Class 9. Add the initialization statement for static variable lpszclassname in the myscreenwnd.cpp file: (Note that this initialization statement must be added to all functions) CMYSCreenwnd :: lpszclassname = NULL;
10. Add a private member variable in the CMYScreenWnd class, the prototype is as follows: uint id_timer; add: id_timer = 1 in the constructor of the CMYSCREENWND class; ID_TIMER is the ID 11 we use when we use the timer. In the CMYSCREENWND class Add private member variables m_point definition, the prototype is as follows: cpoint m_point; add: m_point.x = -1; m_point.x = -1; m_point.x = -1; 12. Add private member function definition in the CMYSCREENWND class. The prototype is as follows: Void Drawbitmap (CDC & DC, INT M_NINDEX); This function is responsible for the work of drawing and displaying BMP files 13. Join DRAWBITMAP (CDC & DC, INT M_NINDEX) in MyScreenwnd.cpp file: Void CMYScreenWnd :: DrawBitmap (CDC & dc, int nIndex) {CDC dcMem; dcMem.CreateCompatibleDC (& dc); CBitmap m_Bitmap; m_Bitmap.LoadBitmap (IDB_BITMAP1 nIndex); dcMem.SelectObject (m_Bitmap); // if your image size is 1024 × 768, please Replace the following 800, 600 // to 1024 * 768 DC.Bitblt (0, 0, 800, 600, & DCMEM, 0, 0, SRCCOPY);} 14. Declaration of the virtual function PostncDestroy () // {AFX_VIRTUAL (CMYWND) protected: virtual void postncdestroy (); //}} AFX_VIRTUAL
15. Next is to handle the keyboard, mouse message, and WM_Paint message, because ClassWizard does not have our new classes, we must manually join the map code, add the following code to the CMYScreenwnd class definition of the myscreensaver.h file / / {{{ {aFX_MSG (CMyScreenWnd) 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, Htask Htask; //}} AFX_MSG DECLARE_MESSAGE_MAP ()
16. Add a mapping code in the MyScreensaver.cpp file (which can be added anywhere in this file, just put the begin_Message_Map (CMYSCREENWND, CWND) // {AFX_MSG_MAP ON_WM_PAINT () ON_WM_KEYDOWN () ON_WM_LBUTTONDOWN () ON_WM_RBUTTONDOWN () ON_WM_MOUSEMOVE () ON_WM_DESTROY () ON_WM_TIMER () //}} AFX_MSG_MAP END_MESSAGE_MAP () 17. code that implements these functions was added in MyScreenWnd.cpp file // OnPaint () function is set to the full-screen window Black void cmyscreenwnd :: onpaint () {CPAINTDC DC (this); CBRUSH Brush (RGB (0,0,0)); CRECT Rect; getClientRect (Rect); DC.FillRect (& Rect, & Brush);} void cmyscreenwnd :: ONTIMER (uint nidevent) {cclientdc DC (this); int N = 2; // You have a few pictures to change n, because I // is only two pictures in the screensaver, thus n = 2 static int m_nindex = 0; m_nIndex% = n-1; DrawBitmap (dc, m_nIndex ); CWnd :: OnTimer (nIDEvent);} void CMyScreenWnd :: OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags) {PostMessage (WM_CLOSE);} void CMyScreenWnd :: ONLBUTTONDOWN (uint nflags, cpoint point) {P ostMessage (WM_CLOSE);} void CMyScreenWnd :: OnRButtonDown (UINT nFlags, CPoint point) {PostMessage (WM_CLOSE);} void CMyScreenWnd :: OnSysKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags) {PostMessage (WM_CLOSE);} void CMyScreenWnd: : OnMouseMove (uint nflags, cpoint point) {if (m_point == cpoint (-1, -1)) m_point = point; else if (m_point! = Point) postmessage (wm_close);} void cmyscreenwnd :: ONDESTROY () { KillTimer;} void cmyscreenwnd :: postncdestroy () {delete this;} // In order to prevent two identical programs at the same time, the following two functions are required // If you don't understand this code, don't take them ,
As long as they are copied to your // project on the line void CMyScreenWnd :: OnActivate (UINT nState, CWnd * pWndOther, BOOL bMinimized) {CWnd :: OnActivate (nState, pWndOther, bMinimized); if (nState == WA_INACTIVE) PostMessage (WM_CLOSE);} void CMyScreenWnd :: OnActivateApp (BOOL bActive, hTASK hTask) {CWnd :: OnActivateApp (bActive, hTask); if // is being deactivated PostMessage (WM_CLOSE) (bActive!);} 18. soon completed The initInstance () function modified CMYSCREENSAVERAPP is as follows: (Note that the statement must be added in MyScreensaver.h: #include "myscreenwnd.h") Bool cmyscreensaverapp :: 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.
#ifdef _afxdll enable3dControls (); // call thisime using mfc in a shared dll #else enable3dControlsStatic (); // call this when linking to mfc staticly #ENDIF
CMYSCREENWND * PWND = New CMYSCREENWND; PWND-> CREATE (); m_pmainwnd = pwnd; return true;
}
19. Master-made, rename the executable of the project's Debug (or Release) directory myscreensaver.exe to myscreensaver.scr, copy to C: / Winnt / System32 (if you use Windows NT and Windows 2000), or C: / windows (if you use the system is Win95, win98), so right-click on the desktop, select "Properties", select Screen Saver, so you can see the screen saver we have created. , Click "Preview", is it cool? Hey, um? Who throws watermelon skin? 20. The above screen saver function is relatively simple, in various mouse and keyboard message processing functions, we just simply call PostMessage (WM_Close); make the program exit, you can modify this part of the code. Of course, we can load the JPG file or GIF file to reduce the size of the screen. You can do this part of your work. Welcome to contact me, my email is: cpplover@163.com 21. The above program is in Windows 2000 Compilation by Professional, Visual C 6.0 Chinese Enterprise Edition. If you need the source code of the project, please contact me. Author: Xu Fu Address: Department of Computer, Beijing University of Aeronautics and Astronautics 98 5-51-mail home page: http: //cppapple.yeah.net (apple Paradise) Email: cpplover@163.com