Mouse action: left, right, middle button and cursor position


   

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  public class MainForm : System.Windows.Forms.Form
  {
    private System.ComponentModel.Container components;

    public MainForm()
    {
      Top = 100;
      Left = 75;
      Height = 100;
      Width = 500;

      MessageBox.Show(Bounds.ToString(), "Current rect");

      this.MouseUp += new MouseEventHandler(OnMouseUp);
      this.MouseMove += new MouseEventHandler(OnMouseMove);
      this.KeyUp += new KeyEventHandler(OnKeyUp);
      InitializeComponent();
      CenterToScreen();
    }

    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
      MessageBox.Show("Disposing this Form");
    }

    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      this.Size = new System.Drawing.Size(300,300);
      this.Text = "Form1";
    }

    [STAThread]
    static void Main() 
    {
      Application.Run(new MainForm());
    }
    protected void OnMouseUp(object sender, MouseEventArgs e)
    {
      if(e.Button == MouseButtons.Left)
        MessageBox.Show("Left click!");
      else if(e.Button == MouseButtons.Right)
        MessageBox.Show("Right click!");
      else if(e.Button == MouseButtons.Middle)
        MessageBox.Show("Middle click!");
    }

    protected void OnMouseMove(object sender, MouseEventArgs e)
    {
      this.Text = "Current Pos: (" + e.X + ", " + e.Y + ")";
    }

    public void OnKeyUp(object sender, KeyEventArgs e)
    {
      MessageBox.Show(e.KeyCode.ToString(), "Key Pressed!");
    }
  }



           
          


Subclass NativeWindow

   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class CaptureLoss: Form
{
     public static void Main()
     {
          Application.Run(new CaptureLoss());
     }
     public CaptureLoss()
     {
          Text = "Capture Loss";
   
          CaptureLossWindow win = new CaptureLossWindow();
          win.form = this;
          win.AssignHandle(Handle);
     }
     protected override void OnMouseDown(MouseEventArgs mea)
     {
          Invalidate();
     }
     public void OnLostCapture()
     {
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
   
          if (Capture)
               grfx.FillRectangle(Brushes.Red, ClientRectangle);
          else
               grfx.FillRectangle(Brushes.Gray, ClientRectangle);
     }
}
   
class CaptureLossWindow : NativeWindow 
{
     public CaptureLoss form;
   
     protected override void WndProc(ref Message message) 
     {
          if (message.Msg == 533)                 // WM_CAPTURECHANGED
               form.OnLostCapture();
   
          base.WndProc(ref message);
     }
}

    


Set InitialDirectory

   
 

using System;
using System.Windows.Forms;

class MainClass {
    static void Main(string[] args) {

        OpenFileDialog dlg = new OpenFileDialog();
        dlg.InitialDirectory = Application.StartupPath;

        if (dlg.ShowDialog() == DialogResult.OK) {
            Console.WriteLine(dlg.FileName);

        }
    }
}

    


Set Filter

   
 
using System;
using System.Windows.Forms;

class MainClass {
    static void Main(string[] args) {

        OpenFileDialog dlg = new OpenFileDialog();
        dlg.Filter = "Rich Text Files (*.rtf)|*.RTF|" +
          "All files (*.*)|*.*";
        if (dlg.ShowDialog() == DialogResult.OK) {
            Console.WriteLine(dlg.FileName);

        }
    }
}

    


FileOk Action

   
 
using System;
using System.IO;
using System.Windows.Forms;

class FileDialogApp {
    private static OpenFileDialog ofd;

    static void Main(string[] args) {
        ofd = new OpenFileDialog();
        string s = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()));

        ofd.InitialDirectory = s;
        ofd.FileOk += new System.ComponentModel.CancelEventHandler(ofd_OK);
        ofd.ShowDialog();
    }
    public static void ofd_OK(object sender,
        System.ComponentModel.CancelEventArgs e) {
        StreamReader sr = new StreamReader(ofd.FileName);
        string s;
        while ((s = sr.ReadLine()) != null) {
            Console.WriteLine(s);
        }
        sr.Close();
    }
}