Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Ideas For C# Programs


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Ntvu   Members   -  Reputation: 100

Like
0Likes
Like

Posted 17 January 2010 - 12:32 PM

I just began learning C# a few days ago. During the last three days I've made two small-to-medium sized applications. The first one is a mouse recorder that can record your mouse movement and clicks and play back your activity, and can also save and load script files for future use. The second program I made with C# is a program that keeps track of my usernames and passwords for all the websites that I have accounts on. It uses files to store the information (obviously) and utilizes a light encryption algorithm (it basically just reverses the string and then something else) that I made. I still have ideas for two more applications - a personal notepad/journal and a forum post checker. However, I think that creating the notepad would be too much of reinventing the wheel so I decided not to do that. As for the forum checker, I'm not exactly sure how I would implement such a thing so I decided to pass on that for now. Can you think of any ideas for windowed C# applications that I can create? I would like this to be a good learning experience for me; I want to learn new concepts in C# for every program that I make, however I don't want the programs to be too easy or so difficult that it will take me extremely long to finish them. Thanks.

Sponsor:

#2 Ntvu   Members   -  Reputation: 100

Like
0Likes
Like

Posted 17 January 2010 - 12:35 PM

Below is some of my code from the mouse recorder program:


using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MouseRecorderTool
{
public partial class FormMain : Form
{
[DllImport("user32.dll")]
static extern bool GetAsyncKeyState(Keys vKey);

public const int OPTION_ENABLE_HOTKEYS = 0;
public const int OPTION_MINIMIZE_RECORD = 1;
public const int OPTION_MINIMIZE_PLAYBACK = 2;
public const int OPTION_LOOP_PLAYBACK = 3;
public const int MAX_OPTIONS = 4;

private int[] options;
private MouseControl mouse;

public FormMain()
{
InitializeComponent();

this.options = new int[MAX_OPTIONS];
this.mouse = new MouseControl();

for (int c = 0; c < MAX_OPTIONS; c++)
{
this.options[c] = 0;
}
}

public void SetOption(int id, int value)
{
if (id < 0 || id >= MAX_OPTIONS || (value != 0 && value != 1))
{
return;
}

this.options[id] = value;
}

private bool StartNewScript()
{
// Check for an existing script
if (this.mouse.RecordSize > 0)
{
if (MessageBox.Show("You have already recorded something. If you choose to click Record and start a new" +
"script, all your previously recorded data will be lost if you do not save it. Continue?", "Confirm Action", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.mouse.Clear();
return true;
}
else
{
// If the user chooses No, then go back to prevent him from adding on to the existing recorded script
return false;
}
}

return true;
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void TimerGUIUpdate_Tick(object sender, EventArgs e)
{
LabelXCoord.Text = Cursor.Position.X.ToString();
LabelYCoord.Text = Cursor.Position.Y.ToString();

switch (this.mouse.MouseState)
{
case MouseControl.State.Idle:
LabelState.Text = "Idle";
ButtonRecord.Enabled = true;
ButtonPlay.Enabled = (this.mouse.RecordSize > 0);
ButtonStop.Enabled = false;
break;

case MouseControl.State.Play:
LabelState.Text = "Playing";
ButtonRecord.Enabled = false;
ButtonPlay.Enabled = false;
ButtonStop.Enabled = true;
break;

case MouseControl.State.Record:
LabelState.Text = "Recording";
ButtonRecord.Enabled = false;
ButtonPlay.Enabled = false;
ButtonStop.Enabled = true;
break;
}
}

private void ButtonRecord_Click(object sender, EventArgs e)
{
this.StartNewScript();

if (AppConfig.GetOption("MinimizeOnRecord") == 1)
{
this.WindowState = FormWindowState.Minimized;
}

if (this.mouse.Record() == false)
{
MessageBox.Show("You cannot record at this time.", "Error: Record");
}
}

private void TimerMouse_Tick(object sender, EventArgs e)
{
MouseControl.State mouseState = this.mouse.MouseState;

switch (mouseState)
{
case MouseControl.State.Play:
this.mouse.Play();
break;

case MouseControl.State.Record:
this.mouse.Record();
break;
}
}

private void ButtonPlay_Click(object sender, EventArgs e)
{
if (AppConfig.GetOption("MinimizeOnPlayback") == 1)
{
this.WindowState = FormWindowState.Minimized;
}

this.mouse.Play();
}

private void ButtonStop_Click(object sender, EventArgs e)
{
this.mouse.Stop();
}

private void aboutMeToolStripMenuItem_Click(object sender, EventArgs e)
{
new FormAbout().ShowDialog(this);
}

private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
(new FormHelp()).ShowDialog(this);
}

private void aboutMeToolStripMenuItem_Click_1(object sender, EventArgs e)
{
(new FormAbout()).ShowDialog(this);
}

private void TimerHotkey_Tick(object sender, EventArgs e)
{
// You can only use hotkeys if they haven't been disabled
if (AppConfig.GetOption("EnableHotkeys") == 0)
{
return;
}

if (GetAsyncKeyState(Keys.End))
{
if (this.mouse.MouseState == MouseControl.State.Play || this.mouse.MouseState == MouseControl.State.Record)
{
this.mouse.Stop();

// Restore the window back to its original state if necessary
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Normal;
}
}
}
else if (GetAsyncKeyState(Keys.Home))
{
if (this.mouse.MouseState == MouseControl.State.Play)
{
this.mouse.Stop();
this.mouse.Play();
}
else
{
MessageBox.Show("You cannot restart from the beginning because you are not in playback mode.", "Error: Rewind");
}
}
else if (GetAsyncKeyState(Keys.Delete))
{
if (this.mouse.MouseState == MouseControl.State.Play || this.mouse.MouseState == MouseControl.State.Record)
{
MessageBox.Show("You cannot clear recorded data when playing or recording.", "Error: Clear");
return;
}

if (MessageBox.Show("Are you sure you want to clear everything?", "Confirm Action", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.mouse.Clear();
}
}
}

private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
this.StartNewScript();
}

private void generalOptionsToolStripMenuItem_Click(object sender, EventArgs e)
{
(new FormOptions()).ShowDialog(this);
}

private void openSToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.StartNewScript())
{
FileManager.ShowDialog();
}
}

private void aveToolStripMenuItem_Click(object sender, EventArgs e)
{
FileSaveManager.ShowDialog();
}

private void FileManager_FileOk(object sender, CancelEventArgs e)
{
if (FileManager.FileName.EndsWith(".dms") == false)
{
MessageBox.Show("That is not a valid file. DirectMouse script files have the file extension .dms", "Error : Read File");
return;
}

// Clear the original data before loading the new data
this.mouse.Clear();

BinaryReader reader = new BinaryReader(new FileStream(FileManager.FileName, FileMode.Open));
int[] data;
int count = reader.ReadInt32();

for (int a = 0; a < count; a++)
{
data = new int[MouseControl.TOTAL_MOUSE_DESCRIPTORS];

for (int c = 0; c < MouseControl.TOTAL_MOUSE_DESCRIPTORS; c++)
{
data[c] = reader.ReadInt32();
}

// If there's a problem in adding the entry, then stop reading the file
if (this.mouse.AddEntry(data) == false)
{
MessageBox.Show("There was an error reading from the file.", "Error: Read File");
reader.Close();
return;
}
}

reader.Close();

// Update the GUI
this.mouse.Stop();
}

private void FileSaveManager_FileOk(object sender, CancelEventArgs e)
{
BinaryWriter writer = new BinaryWriter(new FileStream(FileSaveManager.FileName, FileMode.Create));
int[] data = new int[MouseControl.TOTAL_MOUSE_DESCRIPTORS];

writer.Write(this.mouse.RecordSize);

for (int c = 0; c < this.mouse.RecordSize; c++)
{
data = this.mouse.getMouseDataAt©;

foreach (int d in data)
{
writer.Write(d);
}
}

writer.Close();
}

private void FormMain_Load(object sender, EventArgs e)
{

}
}
}




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MouseRecorderTool
{
class MouseControl
{
[DllImport("user32.dll")] static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, int dwExtraInfo);
[DllImport("user32.dll")] static extern bool GetAsyncKeyState(Keys vKey);

[Flags]
public enum MouseEventFlags
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010
}

[Flags]
public enum State
{
Idle,
Record,
Play
};

private const int MOUSE_X = 0;
private const int MOUSE_Y = 1;
private const int MOUSE_LEFTDOWN = 2;
private const int MOUSE_LEFTUP = 3;
private const int MOUSE_RIGHTDOWN = 4;
private const int MOUSE_RIGHTUP = 5;
private const int MOUSE_MIDDLEDOWN = 6;
private const int MOUSE_MIDDLEUP = 7;

public const int TOTAL_MOUSE_DESCRIPTORS = 8;
public const int TOTAL_MOUSE_BUTTONS = 3;

private List<int[]> recordData;
private int buttonDown;
private int buttonUp;
private State mouseState;
private int recordCount;
private int playCount;

public MouseControl()
{
this.recordData = new List<int[]>();
this.buttonDown = 0;
this.buttonUp = 0;
this.mouseState = State.Idle;
this.recordCount = 0;
this.playCount = 0;
}

private bool Authorize(State requestedState)
{
// The purpose of this function is to ensure that one state does not switch to
// another one invalidly. For example, it cannot switch from recording
// to playing immediately, or vice versa. It must stop first and then
// proceed to that action. This function authorizes all the actions
// and makes sure that no invalid commands are given.

/*
switch (requestedState)
{
case State.Idle:
{
if (this.mouseState != State.Idle)
return true;
break;
}

case State.Record:
{
if (this.mouseState == State.Idle)
return true;
break;
}

case State.Play:
{
if (this.mouseState == State.Idle)
return true;
break;
}
}
* */


return this.mouseState == State.Idle;
}

public bool AddEntry(int[] data)
{
if (data.Length == TOTAL_MOUSE_DESCRIPTORS)
{
this.recordData.Add(data);
return true;
}
return false;
}

public void RegisterButtons()
{
if (GetAsyncKeyState(Keys.LButton))
{
this.buttonDown = MOUSE_LEFTDOWN;
}
else // If it was previously down but is no longer down, then it's a mouse up action
{
if (this.recordCount > 0)
{
if (this.recordData[this.recordCount - 1][MOUSE_LEFTDOWN] == 1)
{
this.buttonUp = MOUSE_LEFTUP;
}
}
}

if (GetAsyncKeyState(Keys.RButton))
{
this.buttonDown = MOUSE_RIGHTDOWN;
}
else // If it was previously down but is no longer down, then it's a mouse up action
{
if (this.recordCount > 0)
{
if (this.recordData[this.recordCount - 1][MOUSE_RIGHTDOWN] == 1)
{
this.buttonUp = MOUSE_RIGHTUP;
}
}
}

if (GetAsyncKeyState(Keys.MButton))
{
this.buttonDown = MOUSE_MIDDLEDOWN;
}
else // If it was previously down but is no longer down, then it's a mouse up action
{
if (this.recordCount > 0)
{
if (this.recordData[this.recordCount - 1][MOUSE_MIDDLEDOWN] == 1)
{
this.buttonUp = MOUSE_MIDDLEUP;
}
}
}
}

public bool Record()
{
if (this.mouseState != State.Record)
{
if (this.Authorize(State.Record))
{
this.mouseState = State.Record;
this.recordCount = 0;
}
else
{
this.Stop();
return false;
}
}

// Register mouse button presses
this.RegisterButtons();


// Build the new entry
int[] mouseData = new int[TOTAL_MOUSE_DESCRIPTORS];

mouseData[MOUSE_X] = Cursor.Position.X;
mouseData[MOUSE_Y] = Cursor.Position.Y;
mouseData[MOUSE_LEFTDOWN] = 0;
mouseData[MOUSE_RIGHTDOWN] = 0;
mouseData[MOUSE_MIDDLEDOWN] = 0;
mouseData[MOUSE_LEFTUP] = 0;
mouseData[MOUSE_RIGHTUP] = 0;
mouseData[MOUSE_MIDDLEUP] = 0;

if (this.buttonDown != 0)
{
mouseData[this.buttonDown] = 1;
}
else if (this.buttonUp != 0)
{
mouseData[this.buttonUp] = 1;
}

// Add the entry and move to the next one
this.recordData.Add(mouseData);
this.recordCount++;

// Clear the mouse button flags
this.buttonDown = 0;
this.buttonUp = 0;

return true;
}

public bool Play()
{
if (this.mouseState != State.Play)
{
if (this.Authorize(State.Play))
{
this.mouseState = State.Play;
this.playCount = 0;
}
else
{
this.Stop();
return false;
}
}

// Stop when it reaches the end
if (this.playCount == this.recordData.Count)
{
// Reset all mouse buttons back to their original states by "lifting" the mouse on all mouse buttons
mouse_event((uint)MouseEventFlags.LEFTUP, 0, 0, 0, 0);
mouse_event((uint)MouseEventFlags.RIGHTUP, 0, 0, 0, 0);
mouse_event((uint)MouseEventFlags.MIDDLEUP, 0, 0, 0, 0);

if (AppConfig.GetOption("LoopPlayback") == 1)
{
this.playCount = 0;
}
else
{
this.Stop();
}
return true;
}


// Retrieve the new mouse data
int[] mouseData = this.recordData[this.playCount];

// Move the mouse
Point point = new Point(mouseData[MOUSE_X], mouseData[MOUSE_Y]);
Cursor.Position = point;

// Play back the buttons (note: if the mouse button is already down, then don't press it down again)
if (this.playCount > 0)
{
if (mouseData[MOUSE_LEFTDOWN] == 1 && this.recordData[this.playCount - 1][MOUSE_LEFTDOWN] == 0)
{
mouse_event((uint)MouseEventFlags.LEFTDOWN, 0, 0, 0, 0);
}
else if (mouseData[MOUSE_RIGHTDOWN] == 1 && this.recordData[this.playCount - 1][MOUSE_RIGHTDOWN] == 0)
{
mouse_event((uint)MouseEventFlags.RIGHTDOWN, 0, 0, 0, 0);
}
else if (mouseData[MOUSE_MIDDLEDOWN] == 1 && this.recordData[this.playCount - 1][MOUSE_MIDDLEDOWN] == 0)
{
mouse_event((uint)MouseEventFlags.MIDDLEDOWN, 0, 0, 0, 0);
}
else if (mouseData[MOUSE_LEFTUP] == 1)
{
mouse_event((uint)MouseEventFlags.LEFTUP, 0, 0, 0, 0);
}
else if (mouseData[MOUSE_RIGHTUP] == 1)
{
mouse_event((uint)MouseEventFlags.RIGHTUP, 0, 0, 0, 0);
}
else if (mouseData[MOUSE_MIDDLEUP] == 1)
{
mouse_event((uint)MouseEventFlags.MIDDLEUP, 0, 0, 0, 0);
}
}

// Move to the next frame
this.playCount++;

return true;
}

public void Stop()
{
this.mouseState = State.Idle;
this.recordCount = 0;
this.playCount = 0;
}

public void Clear()
{
this.recordData.Clear();
this.Stop();
}

public int[] getMouseDataAt(int index)
{
if (index < 0 || index >= this.recordData.Count)
{
return new int[0];
}
return this.recordData[index];
}

public State MouseState
{
get
{
return this.mouseState;
}
}

public int RecordSize
{
get
{
return this.recordData.Count;
}
}
}
}



What can you say about my current knowledge and experience in C# from reading the code above? Also how can I improve on it? What are the good/bad parts of it?

#3 pulpfist   Members   -  Reputation: 528

Like
0Likes
Like

Posted 17 January 2010 - 09:09 PM

Quote:

What can you say about my current knowledge and experience in C# from reading the code above?

You seem to be thorough

Quote:

Can you think of any ideas for windowed C# applications that I can create?
I would like this to be a good learning experience for me; I want to learn new concepts in C# for every program that I make

You can extend the username/password program to use XML formatted files, if you haven't done so already. There is probably no good reason to do so except for learning the .NET XML interfaces.
If you get that to work you can use the DTD format to apply rules/requirements to the content of your XML files.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS