熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用C#做ScreenSaver

2013-11-13 10:27:50  來源: .NET編程 

  /*    Program      :    A Simple Screen Saver
*    File Name    :    ScreenSavercs
*    Author        :    Tran Khanh Hien
*    Date        :    //
*    email        :   
*/

  namespace Screen_Saver
{
    using System;
    using SystemDrawing;
    using SystemCollections;
    using SystemComponentModel;
    using SystemWinForms;
    using SystemData;

  /// <summary>
    ///    Summary description for Form
    /// </summary>
    public class ScreenSaver : SystemWinFormsForm
    {
        /// <summary>
        ///    Required designer variable
        /// </summary>
        private SystemComponentModelContainer components;
        private SystemWinFormsTimer timerSaver;
        private SystemWinFormsLabel lblMarquee;

  private int  iSpeed = ;
        private string strMarqueeText=C Sharp Screen Saver;

  private SystemDrawingFont fontMarquee = new SystemDrawingFont (Arial
SystemDrawingFontStyleBold);
        private Color colorMarquee = SystemDrawingColorFromARGB();

  private int iDistance;
        private int ixStart= ;
        private int iyStart= ;

  public ScreenSaver()
        {
            InitializeComponent();

  lblMarqueeFont=fontMarquee;
            lblMarqueeForeColor=colorMarquee;
            SystemDrawingCursorHide();
        }

  /// <summary>
        ///    Clean up any resources being used
        /// </summary>
        public override void Dispose()
        {
            baseDispose();
            componentsDispose();
        }

  /// <summary>
        ///    Required method for Designer support do not modify
        ///    the contents of this method with the code editor
        /// </summary>
        private void InitializeComponent()
        {
            SystemResourcesResourceManager resources = new SystemResourcesResourceManager (typeof
(ScreenSaver));
            ponents = new SystemComponentModelContainer ();
            thistimerSaver = new SystemWinFormsTimer (ponents);
            thislblMarquee = new SystemWinFormsLabel ();
            //@thisTrayHeight = ;
            //@thisTrayLargeIcon = false;
            //@thisTrayAutoArrange = true;
            //@timerSaverSetLocation (new SystemDrawingPoint ( ));
            timerSaverInterval = ;
            timerSaverEnabled = true;
            timerSaverTick += new SystemEventHandler (thistimerSaver_Tick);
            lblMarqueeLocation = new SystemDrawingPoint ( );
            lblMarqueeSize = new SystemDrawingSize ( );
            lblMarqueeForeColor = SystemDrawingColorWhite;
            lblMarqueeTabIndex = ;
            lblMarqueeVisible = false;
            thisMaximizeBox = false;
            thisStartPosition = SystemWinFormsFormStartPositionManual;
            thisAutoScaleBaseSize = new SystemDrawingSize ( );
            thisBorderStyle = SystemWinFormsFormBorderStyleNone;
            thisKeyPreview = true;
            thisWindowState = SystemWinFormsFormWindowStateMaximized;
            thisShowInTaskbar = false;
            thisIcon = (SystemDrawingIcon) resourcesGetObject ($thisIcon);
            thisControlBox = false;
            thisMinimizeBox = false;
            thisBackColor = SystemDrawingColorBlack;
            thisClientSize = new SystemDrawingSize ( );
            thisKeyDown += new SystemWinFormsKeyEventHandler (thisForm_KeyDown);
            thisMouseDown += new SystemWinFormsMouseEventHandler (thisForm_MouseDown);
            thisMouseMove += new SystemWinFormsMouseEventHandler (thisForm_MouseMove);
            thisControlsAdd (thislblMarquee);
        }

  protected void timerSaver_Tick (object sender SystemEventArgs e)
        {
            lblMarqueeText=strMarqueeText;
            lblMarqueeHeight=lblMarqueeFontHeight;
            lblMarqueeWidth=lblMarqueeTextLength*(int)lblMarqueeFontSize;

  PlayScreenSaver();
        }

  private void PlayScreenSaver()
        {
            //Get the working area of the the computer screen
            SystemDrawingRectangle ssWorkArea = SystemWinFormsScreenGetWorkingArea(this);

  lblMarqueeLocation=new SystemDrawingPoint(ssWorkAreaWidth iDistance
                                        lblMarqueeLocationY);

  //Make the label visible if it is not currently visible
            lblMarqueeVisible=true;

  // Increment the label distance based on the speed set by the user
            iDistance += iSpeed;
            // If the label is offscreen then we want to reposition it to the right
            if (lblMarqueeLocationX <= (lblMarqueeWidth))
            {
                //Reset the distance to
                iDistance = ;

  //If the label is at the top move it to the middle
                if (lblMarqueeLocationY == )
                    lblMarqueeLocation=new SystemDrawingPoint(lblMarqueeLocationX
(ssWorkAreaHeight / ));

  // If label is in the middle of the screen move it to the bottom
                else if(lblMarqueeLocationY== ssWorkAreaHeight /)
                    lblMarqueeLocation=new SystemDrawingPoint(lblMarqueeLocationXssWorkAreaHeight
lblMarqueeHeight);
                //Move the label back to the top
                else
                    lblMarqueeLocation=new SystemDrawingPoint(lblMarqueeLocationX);
            }
        }

  protected void Form_MouseDown (object sender SystemWinFormsMouseEventArgs e)
        {
            StopScreenSaver();
        }

  protected void Form_MouseMove (object sender SystemWinFormsMouseEventArgs e)
        {
            // Determine if the mouse cursor position has been stored previously
            if (ixStart == && iyStart == )
            {
                //Store the mouse cursor coordinates
                ixStart = eX;
                iyStart = eY;
                return;
            }
            // Has the mouse cursor moved since the screen saver was started?
            else if (eX != ixStart || eY != iyStart)
                StopScreenSaver();

  }

  private void StopScreenSaver()
        {
            SystemDrawingCursorShow();
            timerSaverEnabled=false;
            ApplicationExit();
        }

  protected void Form_KeyDown (object sender SystemWinFormsKeyEventArgs e)
        {
            StopScreenSaver();
        }

  /// <summary>
        /// The main entry point for the application
        /// </summary>
        public static void Main(string[] args)
        {
            if (argsLength==)
            {
                //Display the options dialog box
                if (args[]Substring()Equals(/c))
                {
                    MessageBoxShow(Options are not available for this screen saver
                                    C# Screen Saver
                                    MessageBoxIconInformation);
                    ApplicationExit();
                }
                //Start the screen saver normally
                else if (args[]==/s)
                    ApplicationRun(new ScreenSaver());

  //Diaplay the password dialog
                else if (args[]==/a)
                {
                    MessageBoxShow(Passwords are not available for this screen saver
                                    C# Screen Saver
                                    MessageBoxIconInformation);
                    ApplicationExit();
                }
            }
            //For any other args > start
            else
                ApplicationRun(new ScreenSaver());
        }

  }
}


From:http://tw.wingwit.com/Article/program/net/201311/13725.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.