Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

screensaver in Managed c++

0.00/5 (No votes)
28 Jun 2005 1  
another screensaver code, but this one is managed c++ only, no MFC, winipi stuff

Introduction

This code based on The .NET Screen Saver http://www.codeproject.com/useritems/dotnetscr.asp C#.

Actually it is a conversion from C# to C++ managed with some minor changes.

It works with .NET Framework version 1.1., not sure with prior releases.

compile and run: execute run.bat

For begginers

windows2000: copy scr file to folder \winnt\system32 and change screen saver properties. This is a jayssaver.cpp

//    Author: howdyeveryone



#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

#using <System.Windows.Forms.dll>
using namespace System::Windows::Forms;

#using <System.dll>
using namespace System::ComponentModel;

#using <System.Drawing.dll>
using namespace System::Drawing;
using namespace System::Drawing::Imaging;



__gc class ScreenSaverSetting:public Form {
private:
    Button        *pbtnOk;
    void         eventKeyOkClick(Object* sender,  EventArgs* e);
public:
    ScreenSaverSetting();
    ~ScreenSaverSetting();
};


ScreenSaverSetting::ScreenSaverSetting() {

    this->Text = "Screen Saver Setting Here";
    this->Location = Point( 100, 100 );
    this->Width = 600;
    this->Height = 500;
    this->FormBorderStyle = FormBorderStyle::Fixed3D;        

    pbtnOk = new Button();
    pbtnOk->Size = Point( 70, 30 );
    pbtnOk->Location = Point( 400, 400 );
    pbtnOk->Text = "Ok";
    pbtnOk->add_Click(new EventHandler(this, eventKeyOkClick));

    this->Controls->Add(pbtnOk);

}

ScreenSaverSetting::~ScreenSaverSetting() {
}



void ScreenSaverSetting::eventKeyOkClick(Object* sender,  EventArgs* e)   {
    this->Close();
}


__gc class ScreenSaver:public Form {
private:

    PictureBox    *ppbxPhoto;
    IContainer    *pcont;
    Timer        *ptmrPhoto;

    Point        pntMouse;
    Rectangle    recVisible;
    void eventTimerPhotoTick(Object* sender,  EventArgs* e);
    void eventKeyDown(Object *sender, KeyEventArgs *e );
    void eventLoadForm(Object *sender, EventArgs *e );
    void eventMouse(Object *sender, MouseEventArgs *e);
public:
    ScreenSaver();
    ~ScreenSaver();

};



ScreenSaver::ScreenSaver() {

//        this

    this->SuspendLayout();

    this->AutoScaleBaseSize = System::Drawing::Size( 5, 13 );
    this->BackColor = Color::Black;
    this->ClientSize = System::Drawing::Size(292, 273);
    this->FormBorderStyle = FormBorderStyle::None;
    this->Name = "ScreenSaverForm";
    this->Text = "ScreenSaver";        
    this->KeyDown += new KeyEventHandler(this, eventKeyDown);
    this->MouseDown += new MouseEventHandler(this, eventMouse );
    this->MouseMove += new MouseEventHandler(this, eventMouse );
    this->Load += new EventHandler( this, eventLoadForm);    
    this->Bounds = Screen::PrimaryScreen->Bounds;
    this->Cursor->Hide();
    this->TopMost = true;

//        ppbxPhoto

    ppbxPhoto = new PictureBox();
    ppbxPhoto->Name = "pictureBox";
    ppbxPhoto->Location = Point( 46, 86 );
    ppbxPhoto->SizeMode = PictureBoxSizeMode::StretchImage;
    try {
           ppbxPhoto->Image = Image::FromFile( "setup.bmp" );  // put your image file here

      } catch (Exception* e)    {
              Console::WriteLine(S"Image file not found or invalid format");
            Console::WriteLine(e->Message);
        return;
    }

    ppbxPhoto->Location = Point( 46, 86 );
    ppbxPhoto->Size = System::Drawing::Size( 200, 133);
    recVisible = Rectangle( 0, 0, this->Bounds.Width -   ppbxPhoto->Width,
                        this->Bounds.Height - ppbxPhoto->Height );

    ppbxPhoto->TabIndex = 0;
    ppbxPhoto->TabStop = false;

//        timers

    pcont = new System::ComponentModel::Container();
    ptmrPhoto = new Timer( pcont );
    ptmrPhoto->Tick += new EventHandler( this, eventTimerPhotoTick);    
    ptmrPhoto->Interval =  10000;
    ptmrPhoto->Enabled = true;

    this->Controls->Add(ppbxPhoto);
    this->ResumeLayout(false);
}

ScreenSaver::~ScreenSaver() {
    if ( pcont != 0 )
       pcont->Dispose();

    if ( ppbxPhoto->Image != 0 )
       ppbxPhoto->Image->Dispose();

}



void ScreenSaver::eventTimerPhotoTick(Object* sender,  EventArgs* e) {

    Random *prnd = new Random();
       ppbxPhoto->Location = Point( prnd->Next(recVisible.Width), 
                    prnd->Next(recVisible.Height));

    ppbxPhoto->Visible = false;
    ppbxPhoto->Update();
    ppbxPhoto->Visible = true;
    ppbxPhoto->Update();

}


void ScreenSaver::eventKeyDown(Object *sender, KeyEventArgs *e ) {
          this->Close();
}

void ScreenSaver::eventMouse(Object *sender, MouseEventArgs *e)    {
    
            if (!pntMouse.IsEmpty)
            {
                if (pntMouse != Point(e->X, e->Y))
                    this->Close();
                if (e->Clicks > 0)
                    this->Close();
            }
            pntMouse = Point(e->X, e->Y);

}



void ScreenSaver::eventLoadForm( Object *sender, EventArgs* e ) {

    this->Cursor->Hide();
    this->TopMost = true;
    this->Activate();

}



int main( int argc, char* argv[]) {
    if ( argc > 2 ) {
      Console::WriteLine( "Wrong parameter number" );
      return 1;
      }

    if ( argc == 1 ) {
       Application::Run( new ScreenSaver() );
           return 0;
    }

    String *pstr = argv[1];
    String *pstr1 = pstr->ToLower()->Trim()->Substring(0,2);

    if ( String::Compare( pstr1, "/c") == 0 )    {
       Application::Run( new ScreenSaverSetting() );
       return 0;
       }

    if ( String::Compare( pstr1, "/s") == 0 )    {
       Application::Run( new ScreenSaver() );
    }
    return 0;
}    // end main






License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here