Make a picture screen saver with VC ++ 6.0

zhaozj2021-02-11  212

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 thisime by the sale mfc in a shared dll

#ELSE

Enable3dControlsStatic (); // call thisime1 linking to mfc staticly

#ENDIF

CMYWND * PWND = New CMYWND;

PWND-> CREATE ();

m_pmainwnd = pWnd;

Return True;

}

Of course, before it is previously #include "mywnd.h". Behind it is done in MyWnd.h and MyWnd.cpp.

The description of CMYWND is given below:

Class CMYWND: PUBLIC CWND

{

PUBLIC:

CMYWND ();

Static LPCSTR LPSZCLASSNAME; // Registration class name

PUBLIC:

Bool crete ();

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 points);

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 ()

}

MyWnd.cpp file:

......

CMYWND :: CMYWND ()

{

m_prepoint = cpoint (-1, -1);

}

LPCSTR CMYWND :: lpszclassname = NULL;

Bool cmywnd :: crete ()

{

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, this cursor does not have any patterns

}

CRECT RECT (0, 0, :: GetSystemmetrics),

:: GetSystemMetrics (SM_CYSCREEN));

Createex (WS_EX_TOPMOST, LPSZCLASSNAME, _T ("), WS_Visible | WS_POPUP,

Rect.Lep, Rect.top, Rect.right - Rect.Top, 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;

}

In order to prevent two identical programs at the same time, 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 (WM_CLOSE);

}

The onpaint () function sets the full screen window to black:

Void cmywnd :: onpaint ()

{

CPAINTDC DC (this);

CBRUSH Brush (RGB (0, 0, 0));

CRECT RECT;

GetClientRect (Rect);

DC.FillRect (& Rect, & Brush);

}

Drawbitmap () function is called by the counter, toggle the image; note that the IDB_bitmap1, DC.bitblt (0, 0, 800, 600 ... and if (Nindexbit> = 5) in the following two functions are based on your BMP image. Size, the location is different, I have selected 5 800x600 BMP pictures. Note that the ID value is continuous, IDB_bitmap1 is the smallest.

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, srcopy);

}

Void CMYWND :: ONTIMER (uint nidevent)

{

CClientDC DC (this);

Static nindexbit = 0;

IF (Nindexbit> = 5)

NINDEXBIT = 0;

DrawBitmap (DC, Nindexbit );

CWnd :: ONTIMER (Nidevent);

}

In response to the keyboard, the mouse is an indispensable screen saver, onkeydown () () () () () (), OnMButtondown (), OnSyskeyDown (), the onsyskeydown () function is joined in:

Postmessage (WM_CLOSE);

The onmouseMove () function is special, it should be added to:

IF (m_prepoint == cpoint (-1, -1)))

M_Prepoint = Point;

Else IF (m_prepoint! = POINT)

Postmessage (WM_CLOSE);

It's going to be completed. Delete the timer in the onDestroy () function: killtimer; ID_TIMER;

Also, add: delete this;

Oops, back pain back, eyeball, hands back (no, it will be)! However, I believe that you will not wait to press CTRL F5, watching a picture in front of you, ah, your own screen saver! Hurry up quickly, replace the homemade screensaver, feeling different: the picture is taking you, time time you change, mouse? keyboard? I want to respond to who will respond to anyone ... Oops, who threw the paper group: (. In fact, there are still many places that can improve, such as the picture always display; BMP file is too large, resulting in the generated screen protection The program is also very large, there is no JPG, there is no password, there is no interface that can be directly controlled. Due to the simple handling of the initInstance () function (directly call the CMYWND class), you will find that when you right click on the desktop, select "Properties" , "Screen Sword" page, "Screen Sword" drop-down menu, when Myscreensaver is selected, Myscreensaver previewed (or running directly); assume you determine Myscreensaver as your screen saver, wait for your second time When the "Screen Saver Program" page, you will preview. Go back to see the initInstance () function. To make it more work more, modify the initInstance () function:

LPTSTR LPSZARGV = __ARGV [1];

IF (LPSZARGV [0] == '/')

LPSZARGV ;

IF (LSTRCMPI (LPSZARGV, _T ("s")) == 0)

{

CMYWND * PWND = New CMYWND;

PWND-> CREATE ();

m_pmainwnd = pWnd;

Return True;

}

Return False;

But now you run this program in VC, "The program executes illegal operations, which is about to be closed. If there is still a problem, please contact me (??)" will be accompanied by an overweight bass for your appreciation. (A?) The reason is that we added Return False; and, don't forget that there is a CMYSCREENSAVERDLG class that is useless, with it to talk to your screensaver direct dialogue. For example, in order to easily determine the interval, select the picture, plus a edit box and a few buttons. Reaffirming that due to the large memory of generating files, there are many memory, if you can't run, it is probably too much window. At this point you can change your smaller pictures. If you have any questions, please let it be: Toxyz@163.net. Later I will place the entire project of the above example at http://yxz.163.net at home, and some new gates are discussed together. (Full text)

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

New Post(0)