Make a picture screen saver with VC ++ 6.0

zhaozj2021-02-08  261

VC can be said that it is very popular. If you learn home, or you will master the MFC, you will feel it convenient and fast, of course, most important is powerful. No, from the most basic application .EXE to the dynamic connection library DLL, then the Internet ActiveX control to the Internet Server API, of course, there are database applications ... Hey, I use it to do screen saver. . The general screen saver is used as an SCR as an extension, and in the C: / Windows directory or C: / windows / system directory, the Windows 98 internal program is called (Windows NT is in the C: / Windows / System32 directory under). How to call? Don't say it, this doesn't know. Ok, let's make a simple. Select MFC AppWizard (exe), project name is MyScreensaver, [Next], dialog, and then follow you. Open Menu Project, Settings, in the debug page, the Executable for Debug session item, and the output file name in the link page, this Output file name is changed to C: /Windows/myscreensaver.scr, so that you can run directly in the VC after debugging (Ctrl F5) ), You can see the result. Of course, the sole disadvantage that this is that you must manually clear the junk file in the Windows directory (of course, after seeing satisfactory results;), you can help you with Safeclean this small Dongdong, unless your hard drive is big Let you feel that it doesn't matter ... Come back soon, see if I ran there). Next, use Class Wizard to generate a CMYWND class, the base class is CWND (generic CWnd in the base class). This class is what we have to focus on. Create a full screen window, timer, hidden mouse, display picture, response to keyboard, mouse, etc., this guy is all inclusive. As for myscreensaverdlg.h and myScreensaverdlg.cpp files. We temporarily do it. Open MyScreensaver.cpp, modify the InitInstance () function: BOOL CMyScreensaverApp :: InitInstance () {AfxEnableControlContainer (); #ifdef _AFXDLL Enable3dControls (); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic (); // Call THIS WHENDIF CMYWND * PWND = New CMYWND; PWND-> CREATE (); m_pmainwnd = pwnd; return true;} Of course, before it is before #include "myWnd.h". Behind it is done in MyWnd.h and MyWnd.cpp.

CMyWnd description given below: class CMyWnd: public CWnd {public: CMyWnd (); static LPCSTR lpszClassName; // class name registered 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; // detect mouse movement 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, Htask Htas K); //}} AFX_MSG DECLARE_MESSAGE_MAP ()}; myWnd.cpp file: ... cmywnd :: cmyWnd () {m_prepoint = cpoint () {m_prepoint = cpoint (-1, -1);} lpcstr cmywnd :: lpszclassname = null; bool cmywnd: : Create () {if (lpszClassName == NULL) {lpszClassName = AfxRegisterWndClass (CS_HREDRAW | CS_VREDRAW, :: LoadCursor (AfxGetResourceHandle (), MAKEINTRESOURCE (IDC_NOCURSOR))); // register class; IDC_NOCURSOR for the new cursor ID, the cursor no pattern} 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, Rect.Bottom - Rect.top, GetsafehWnd (), NULL, NULL);

// Create a full screen window setTimer (ID_TIMER, 500, NULL); // Timer, ID_timer Don't forget to define Return True;} To prevent two identical programs while running simultaneously, the following two functions are required: 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, hTASK hTask ) {CWnd :: onactivateApp (BACTIVE, HTASK); if (! BACTIVE) // is being deactivated postmessage;} onpaint () function Sets the full screen window to black: void cmywnd :: onpaint () {CPAINTDC DC ( THIS); CBRUSH Brush (RGB (0, 0)); CRECT RECT; GetClientRect (Rect); DC.FillRect (& Reed, & Brush);} Drawbitmap () function by the counter, switching pictures; note, below IDB_bitmap1, dc.bitblt (0, 0, 800, 600 ... and IF (Nindexbit> = 5) varies depending on your BMP picture, size, I have selected 5 800x600 BMP image Note that, the value of ID is continuous, the minimum 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 nindexbit = 0; if (Nindexbit> = 5) NINDEXBIT = 0; DrawBitmap (DC, NINDEXBIT ); CWnd :: ONTIMER (Nidevent);} Responding to the keyboard, the mouse is indispensable, in ONKEYDOWN () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () () (), " WM_Close); OnMouseMove () function is more special, it should be added to: if (m_prepoint == cpoint (-1, -1)) m_prepoint = Point; else if (m_prepoint! = Point) PostMessage (WM_CLOSE); fast .

转载请注明原文地址:https://www.9cbs.com/read-1420.html

New Post(0)