/* Professional Windows GUI Programming Using C# by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, Zach Greenvoss, Shripad Kulkarni, Neil Whitlow Publisher: Peer Information ISBN: 1861007663 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ControlModifier { /// <summary> /// Summary description for ControlModifier. /// </summary> public class ControlModifier : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public ControlModifier() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // System.Timers.Timer t = new System.Timers.Timer(10000); t.Elapsed += new System.Timers.ElapsedEventHandler(time); t.Start(); } void time(object sender, System.Timers.ElapsedEventArgs e) { MessageBox.Show(ControlModifier.ModifierKeys.ToString()); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (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.Size = new System.Drawing.Size(300,300); this.Text = "ControlModifier"; } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new ControlModifier()); } } }
Author: coder
Demonstrates using the System.Timers.Timer class 2
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // Timer.cs -- demonstrates using the System.Timers.Timer class. // // Compile this program with the following command line: // C:>csc Timer.cs using System; using System.Windows.Forms; using System.Timers; namespace nsDelegates { public class TimerAndDialog { static int countdown = 10; static System.Timers.Timer timer; static public void Main () { // Create the timer object. timer = new System.Timers.Timer (1000); // Make it repeat. Setting this to false will cause just one event. timer.AutoReset = true; // Assign the delegate method. timer.Elapsed += new ElapsedEventHandler(ProcessTimerEvent); // Start the timer. timer.Start (); // Just wait. MessageBox.Show ("Waiting for countdown", "Text"); } // Method assigned to the timer delegate. private static void ProcessTimerEvent (Object obj, ElapsedEventArgs e) { --countdown; // If countdown has reached 0, it's time to exit. if (countdown == 0) { timer.Close(); Environment.Exit (0); } // Make a string for a new message box. string sigtime = e.SignalTime.ToString (); string str = "Signal time is " + sigtime.Substring (sigtime.IndexOf(" ") + 1); str += " Countdown = " + countdown; // Show a message box. MessageBox.Show (str, "Timer Thread"); } } }
Demonstrates using the System.Threading.Timer object
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // ThrdTime.cs -- Demonstrates using the System.Threading.Timer object. // // Compile this program with the following command line: // C:>csc ThrdTime.cs using System; using System.Windows.Forms; using System.Threading; namespace nsDelegates { public class ThrdTime { static int countdown = 10; static System.Threading.Timer timer; static public void Main () { // Create the timer callback delegate. System.Threading.TimerCallback cb = new System.Threading.TimerCallback (ProcessTimerEvent); // Create the object for the timer. clsTime time = new clsTime (); // Create the timer. It is autostart, so creating the timer will start it. timer = new System.Threading.Timer (cb, time, 4000, 1000); // Blessed are those who wait. MessageBox.Show ("Waiting for countdown", "Text"); } // Callback method for the timer. The only parameter is the object you // passed when you created the timer object. private static void ProcessTimerEvent (object obj) { --countdown; // If countdown is complete, exit the program. if (countdown == 0) { timer.Dispose (); Environment.Exit (0); } string str = ""; // Cast the obj argument to clsTime. if (obj is clsTime) { clsTime time = (clsTime) obj; str = time.GetTimeString (); } str += " Countdown = " + countdown; MessageBox.Show (str, "Timer Thread"); } } // Define a class to use as the object argument for the timer. class clsTime { public string GetTimeString () { string str = DateTime.Now.ToString (); int index = str.IndexOf(" "); return (str.Substring (index + 1)); } } }
Using TimerCallback
using System; using System.Threading; class TimePrinter { static void PrintTime(object state) { Console.WriteLine("Time is: {0}, Param is: {1}", DateTime.Now.ToLongTimeString(), state.ToString()); } static void Main(string[] args) { TimerCallback timeCB = new TimerCallback(PrintTime); Timer t = new Timer( timeCB, // The TimerCallback delegate type. "Hi", // Any info to pass into the called method. 0, // Amount of time to wait before starting. 1000); // Interval of time between calls. } }
use Asterisk sound from SystemSounds
using System; using System.Media; class MainClass { public static void Main() { // Play the sound associated with the Asterisk event SystemSounds.Asterisk.Play(); } }
Print all System Information
using System; using System.Drawing; using System.Windows.Forms; class SysInfoStrings { public static string[] Values { get { return new string[] { SystemInformation.ArrangeDirection.ToString(), SystemInformation.ArrangeStartingPosition.ToString(), SystemInformation.BootMode.ToString(), SystemInformation.Border3DSize.ToString(), SystemInformation.BorderSize.ToString(), SystemInformation.CaptionButtonSize.ToString(), SystemInformation.CaptionHeight.ToString(), SystemInformation.ComputerName, SystemInformation.CursorSize.ToString(), SystemInformation.DbcsEnabled.ToString(), SystemInformation.DebugOS.ToString(), SystemInformation.DoubleClickSize.ToString(), SystemInformation.DoubleClickTime.ToString(), SystemInformation.DragFullWindows.ToString(), SystemInformation.DragSize.ToString(), SystemInformation.FixedFrameBorderSize.ToString(), SystemInformation.FrameBorderSize.ToString(), SystemInformation.HighContrast.ToString(), SystemInformation.HorizontalScrollBarArrowWidth.ToString(), SystemInformation.HorizontalScrollBarHeight.ToString(), SystemInformation.HorizontalScrollBarThumbWidth.ToString(), SystemInformation.IconSize.ToString(), SystemInformation.IconSpacingSize.ToString(), SystemInformation.KanjiWindowHeight.ToString(), SystemInformation.MaxWindowTrackSize.ToString(), SystemInformation.MenuButtonSize.ToString(), SystemInformation.MenuCheckSize.ToString(), SystemInformation.MenuFont.ToString(), SystemInformation.MenuHeight.ToString(), SystemInformation.MidEastEnabled.ToString(), SystemInformation.MinimizedWindowSize.ToString(), SystemInformation.MinimizedWindowSpacingSize.ToString(), SystemInformation.MinimumWindowSize.ToString(), SystemInformation.MinWindowTrackSize.ToString(), SystemInformation.MonitorCount.ToString(), SystemInformation.MonitorsSameDisplayFormat.ToString(), SystemInformation.MouseButtons.ToString(), SystemInformation.MouseButtonsSwapped.ToString(), SystemInformation.MousePresent.ToString(), SystemInformation.MouseWheelPresent.ToString(), SystemInformation.MouseWheelScrollLines.ToString(), SystemInformation.NativeMouseWheelSupport.ToString(), SystemInformation.Network.ToString(), SystemInformation.PenWindows.ToString(), SystemInformation.PrimaryMonitorMaximizedWindowSize.ToString(), SystemInformation.PrimaryMonitorSize.ToString(), SystemInformation.RightAlignedMenus.ToString(), SystemInformation.Secure.ToString(), SystemInformation.ShowSounds.ToString(), SystemInformation.SmallIconSize.ToString(), SystemInformation.ToolWindowCaptionButtonSize.ToString(), SystemInformation.ToolWindowCaptionHeight.ToString(), SystemInformation.UserDomainName, SystemInformation.UserInteractive.ToString(), SystemInformation.UserName, SystemInformation.VerticalScrollBarArrowHeight.ToString(), SystemInformation.VerticalScrollBarThumbHeight.ToString(), SystemInformation.VerticalScrollBarWidth.ToString(), SystemInformation.VirtualScreen.ToString(), SystemInformation.WorkingArea.ToString(), }; } } }
SystemInformation.PrimaryMonitor
using System; using System.Drawing; using System.Windows.Forms; class ScribbleWithBitmap: Form { bool bTracking; Point ptLast; Bitmap bitmap; Graphics grfxBm; public static void Main() { Application.Run(new ScribbleWithBitmap()); } public ScribbleWithBitmap() { Text = "Scribble with Bitmap"; Size size = SystemInformation.PrimaryMonitorMaximizedWindowSize; bitmap = new Bitmap(size.Width, size.Height); grfxBm = Graphics.FromImage(bitmap); grfxBm.Clear(BackColor); } protected override void OnMouseDown(MouseEventArgs mea) { if (mea.Button != MouseButtons.Left) return; ptLast = new Point(mea.X, mea.Y); bTracking = true; } protected override void OnMouseMove(MouseEventArgs mea) { if (!bTracking) return; Point ptNew = new Point(mea.X, mea.Y); Pen pen = new Pen(ForeColor); Graphics grfx = CreateGraphics(); grfx.DrawLine(pen, ptLast, ptNew); grfx.Dispose(); grfxBm.DrawLine(pen, ptLast, ptNew); ptLast = ptNew; } protected override void OnMouseUp(MouseEventArgs mea) { bTracking = false; } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; grfx.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height); } }