C# Screen Saver.

I am using C# Express 2008

Page 1 of 1

6 Replies - 7057 Views - Last Post: 11 August 2009 - 09:26 AM Rate Topic: -----

#1 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

C# Screen Saver.

Posted 09 August 2009 - 06:55 AM

Hello, I have been following the C# Tutorial on DIC (Create a Screen Saver), and was having some issues using the "directives" I think they are called.

Here is what I have so far.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DreamInCode
{
	class Program
	{
		[STAThread]
		static void Main(string[] args)
		{
			if (args.Length > 0) //we have arguments
			{
				//"/s", show the screen saver.
				if (args[0].ToLower().Trim().Substring(0, 2) == "/s")
				{
					//show the screen saver.
					Application.EnableVisualSytles();
					Application.SetCompatibleTextRenderingDefault(false);
					ShowScreenSaver();
					Application.Run();
				}
				//"/p", preview screen saver.
				else if (args[0].ToLower().Trim().Substring(0, 2) == "/p")
				{
					//preview screen saver.
				}
				//"/c", config screeen saver.
				else if (args[0].ToLower().Trim().Substring(0, 2) == "/c")
				{
					//configure the screen saver.
				}
			}
			else  //no arguments passed
			{
				//show screen saver anyway.
			}
		}
		static void ShowScreenSaver()
		{
			foreach (Screen screen in Screen.AllScreens)
			{
				MainForm screensaver = new MainForm(screen.Bounds);
				screensaver.Show();
			}	
		}
	}
}




Error Messages:

Quote

Error 1 The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)
Error 1 The name 'EnableVisualSytles' does not exist in the current context C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\Program.cs 20 33 DreamInCode
Error 2 The name 'Application' does not exist in the current context C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\Program.cs 20 21 DreamInCode
Error 3 The name 'Application' does not exist in the current context C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\Program.cs 21 21 DreamInCode
Error 4 The name 'Application' does not exist in the current context C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\Program.cs 23 21 DreamInCode
Error 5 The name 'Screen' does not exist in the current context C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\Program.cs 43 39 DreamInCode
Error 6 The type or namespace name 'Screen' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\Program.cs 43 22 DreamInCode
Error 7 The type or namespace name 'MainForm' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\Program.cs 45 17 DreamInCode
Error 8 The type or namespace name 'MainForm' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\Program.cs 45 44 DreamInCode


Is This A Good Question/Topic? 0
  • +

Replies To: C# Screen Saver.

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: C# Screen Saver.

Posted 09 August 2009 - 07:28 AM

Looks like you are at least missing a reference to System.Windows.Forms, that's what the first error indicates.

Open solution explorer window, right click on your project and select Add Reference...

On the opened window search for System.Windows.Forms, select it and click OK.

Then build the application and at least some of the errors will be gone. If there are errors left, post them here and let's find a way to correct them too.
Was This Post Helpful? 0
  • +
  • -

#3 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: C# Screen Saver.

Posted 09 August 2009 - 07:50 AM

What you said has worked with that error, and I thank you for that.
My other errors are:
Error	1	'System.Windows.Forms.Application' does not contain a definition for 'EnableVisualSytles'	C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\Program.cs	20	33	DreamInCode
Error	2	The type 'System.Drawing.Rectangle' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.	C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\Program.cs	45	17	DreamInCode
Error	3	The type or namespace name 'MainForm' could not be found (are you missing a using directive or an assembly reference?)	C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\Program.cs	45	17	DreamInCode
Error	4	The type or namespace name 'MainForm' could not be found (are you missing a using directive or an assembly reference?)	C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\Program.cs	45	44	DreamInCode



I'm sorry for posting this, but I am a little lost(well, a lot).
Was This Post Helpful? 0
  • +
  • -

#4 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: C# Screen Saver.

Posted 09 August 2009 - 08:55 AM

For first error:
There is a typo in the method name. It should be Application.EnableVisualStyles(); instead of Application.EnableVisualSytles();

for second error:
Add a reference to System.Drawing just like I instructed earlier with System.Windows.Forms

For third and fourth errors:
Right click on your project in the solution explorer. Then go to Add -> Windows Form...
Name the form as "MainForm" and click ok

A new form will be and you'll see it in the designer. Right click the form and select View code. Now the code screen should be shown.
It should look like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DreamInCode
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}
	}
}



Replace the constructor with this (from the tutorial):
		public MainForm(Rectangle Bounds)
		{
			InitializeComponent();
			this.Bounds = Bounds;
			//hide the cursor
			Cursor.Hide();
		}



Let's hope the errors disappear with these.
Was This Post Helpful? 0
  • +
  • -

#5 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: C# Screen Saver.

Posted 09 August 2009 - 07:17 PM

Well, you have been a great help.
Those errors have been working, and I have an idea of how this is supposed to go together ( I think). I have one last error, that I am drawing a blank on.

Error	1	Member 'System.Windows.Forms.Cursor.Hide()' cannot be accessed with an instance reference; qualify it with a type name instead	C:\Documents and Settings\Cam\My Documents\Visual Studio 2008\Projects\DreamInCode\DreamInCode\MainForm.Designer.cs	19	13	DreamInCode



The source code.
using System.Drawing.Design;
using System.Drawing;

namespace DreamInCode
{
	public partial class MainForm
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		//constructor
		public MainForm(Rectangle Bounds)
		{
			InitializeComponent();
			this.Bounds = Bounds;
			//hide the cursor
			Cursor.Hide();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Text = "MainForm";
		}

		#endregion
	}
}



Well, nvm about the error I posted. Thanks to you helping me earlier I have fixed that problem on my own :). I figured it was something similar, just took 4 google searches lol.
Was This Post Helpful? 0
  • +
  • -

#6 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: C# Screen Saver.

Posted 10 August 2009 - 05:59 PM

Hello guys.
I have another question. Lets say while the screen saver is currently running (has an instance) and the user pressed a key (any key). How would I detect that key, and then close the application?

Currently I have it in my in my Form (which is active and fullscreen)
and the code I have compiles, but doesn't actually do anything.
I was thinking I am putting it in the wrong place, but not sure what to do.
private void KeyPressDetect(object sender, KeyPressEventArgs e)
{
if(e != null)  //we have a keypress
{
Application.Exit();
}
//else do nothing.
}




So, my thought was that if a key was pressed, this would be called by the form and then the app could exit (and obviously stop drawing the graphics).
Was This Post Helpful? 0
  • +
  • -

#7 indrora  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 25-July 08

Re: C# Screen Saver.

Posted 11 August 2009 - 09:26 AM

Make sure to wire together your events. Classical VB programmers are all too used to doing
sub qq ( ... ) handles ...

but C# doesnt do that, instead you can have as many event handlers as can subscribe to your event. Example:
// Handle the KeyPress event...
MainForm.KeyPress += delegate { MessageBox.Show("Hi! I'm in ur event handlers, stealin ur keystrokes :)/>"); MessageBox.Show("Oh, and the next event kills the message loop"); }
// Handle the keypress event here also...
MainForm.KeyPress += delegate { Application.Exit(); }



now, i dont know exactly which event is called when a key is pushed (there's a good 3-4 of them) but its "KeyDown" "KeyUp" "KeyPress" or "KeyRelease" -- though I dont know if the last 2 are valid. Intellisense will guide you to events because a little lightning bolt will be its icon, or you can use the designer If You Dare and you can add/wire up event handlers from the properties window.

edit: you can use more than just no-argument delegates -- you can use things like this:
BackWorker.onprogressChanged += delegate(object s, ProgressEventArgs e) { this.text = "progress: " + e.Progress; };

and you dont even have to use Delegates, you can use EventHanders, as such:
this.FormClosing += new EventHandler(this.FormClosingHandler); 

Assuming that there's a void method called "FormClosingHandler" that takes an object, ClosingEventArgs pair (if i recall correctly)

This post has been edited by indrora: 11 August 2009 - 09:31 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1