可移动窗口

Posted on 2005-12-29 09:26 藤条焖猪肉 阅读(143) 评论(0)  编辑 收藏

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


namespace WeatherAlert
{
 /// <summary>
 /// The main form.
 /// </summary>
 /// <remarks>
 /// Run the application, and handle the UI requests.
 /// </remarks>
 public class FormMain : System.Windows.Forms.Form
 {
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;
    private System.Windows.Forms.LinkLabel linkLabelUpdate;
    private System.Windows.Forms.Label labelLocation;
    private System.Windows.Forms.Label labelTemperature;
    private System.Windows.Forms.Label labelSkyConditions;
    private System.Windows.Forms.Label labelVisibility;
    private System.Windows.Forms.Label labelAirPressure;
    private System.Windows.Forms.Label labelWind;
    private System.Windows.Forms.Label labelHumidity;
    private System.Windows.Forms.Label labelDewPoint;
    private System.Windows.Forms.LinkLabel linkLabelConfigure;
  private System.Windows.Forms.Button buttonClose;


  /// <summary>
  /// Is the user moving the form?
  /// </summary>
  private bool _isMoving;

  /// <summary>
  /// Where did the X location start?
  /// </summary>
  private int XOffset;

  /// <summary>
  /// Where did the Y location start?
  /// </summary>
  private int YOffset;

  public FormMain()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();
  }

  /// <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()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormMain));
   this.buttonClose = new System.Windows.Forms.Button();
   this.linkLabelUpdate = new System.Windows.Forms.LinkLabel();
   this.labelLocation = new System.Windows.Forms.Label();
   this.labelTemperature = new System.Windows.Forms.Label();
   this.labelSkyConditions = new System.Windows.Forms.Label();
   this.labelVisibility = new System.Windows.Forms.Label();
   this.labelAirPressure = new System.Windows.Forms.Label();
   this.labelWind = new System.Windows.Forms.Label();
   this.labelHumidity = new System.Windows.Forms.Label();
   this.labelDewPoint = new System.Windows.Forms.Label();
   this.linkLabelConfigure = new System.Windows.Forms.LinkLabel();
   this.SuspendLayout();
   //
   // buttonClose
   //
   this.buttonClose.Image = ((System.Drawing.Image)(resources.GetObject("buttonClose.Image")));
   this.buttonClose.Location = new System.Drawing.Point(368, 16);
   this.buttonClose.Name = "buttonClose";
   this.buttonClose.Size = new System.Drawing.Size(24, 23);
   this.buttonClose.TabIndex = 49;
   this.buttonClose.Text = "X";
   this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);
   //
   // linkLabelUpdate
   //
   this.linkLabelUpdate.BackColor = System.Drawing.Color.Transparent;
   this.linkLabelUpdate.Font = new System.Drawing.Font("MS Reference Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
   this.linkLabelUpdate.LinkColor = System.Drawing.Color.Yellow;
   this.linkLabelUpdate.Location = new System.Drawing.Point(248, 16);
   this.linkLabelUpdate.Name = "linkLabelUpdate";
   this.linkLabelUpdate.Size = new System.Drawing.Size(100, 24);
   this.linkLabelUpdate.TabIndex = 50;
   this.linkLabelUpdate.TabStop = true;
   this.linkLabelUpdate.Text = "Update";
   this.linkLabelUpdate.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   this.linkLabelUpdate.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabelUpdate_LinkClicked);
   //
   // labelLocation
   //
   this.labelLocation.BackColor = System.Drawing.Color.Transparent;
   this.labelLocation.ForeColor = System.Drawing.Color.Yellow;
   this.labelLocation.Location = new System.Drawing.Point(105, 40);
   this.labelLocation.Name = "labelLocation";
   this.labelLocation.Size = new System.Drawing.Size(400, 72);
   this.labelLocation.TabIndex = 51;
   this.labelLocation.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   this.labelLocation.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseUp);
   this.labelLocation.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseMove);
   this.labelLocation.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseDown);
   //
   // labelTemperature
   //
   this.labelTemperature.BackColor = System.Drawing.Color.Transparent;
   this.labelTemperature.ForeColor = System.Drawing.Color.Yellow;
   this.labelTemperature.Location = new System.Drawing.Point(15, 120);
   this.labelTemperature.Name = "labelTemperature";
   this.labelTemperature.Size = new System.Drawing.Size(195, 24);
   this.labelTemperature.TabIndex = 52;
   this.labelTemperature.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   this.labelTemperature.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseUp);
   this.labelTemperature.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseMove);
   this.labelTemperature.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseDown);
   //
   // labelSkyConditions
   //
   this.labelSkyConditions.BackColor = System.Drawing.Color.Transparent;
   this.labelSkyConditions.ForeColor = System.Drawing.Color.Yellow;
   this.labelSkyConditions.Location = new System.Drawing.Point(215, 120);
   this.labelSkyConditions.Name = "labelSkyConditions";
   this.labelSkyConditions.Size = new System.Drawing.Size(160, 24);
   this.labelSkyConditions.TabIndex = 53;
   this.labelSkyConditions.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   this.labelSkyConditions.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseUp);
   this.labelSkyConditions.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseMove);
   this.labelSkyConditions.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseDown);
   //
   // labelVisibility
   //
   this.labelVisibility.BackColor = System.Drawing.Color.Transparent;
   this.labelVisibility.ForeColor = System.Drawing.Color.Yellow;
   this.labelVisibility.Location = new System.Drawing.Point(380, 120);
   this.labelVisibility.Name = "labelVisibility";
   this.labelVisibility.Size = new System.Drawing.Size(205, 24);
   this.labelVisibility.TabIndex = 54;
   this.labelVisibility.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   this.labelVisibility.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseUp);
   this.labelVisibility.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseMove);
   this.labelVisibility.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseDown);
   //
   // labelAirPressure
   //
   this.labelAirPressure.BackColor = System.Drawing.Color.Transparent;
   this.labelAirPressure.ForeColor = System.Drawing.Color.Yellow;
   this.labelAirPressure.Location = new System.Drawing.Point(16, 152);
   this.labelAirPressure.Name = "labelAirPressure";
   this.labelAirPressure.Size = new System.Drawing.Size(568, 24);
   this.labelAirPressure.TabIndex = 55;
   this.labelAirPressure.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   this.labelAirPressure.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseUp);
   this.labelAirPressure.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseMove);
   this.labelAirPressure.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseDown);
   //
   // labelWind
   //
   this.labelWind.BackColor = System.Drawing.Color.Transparent;
   this.labelWind.ForeColor = System.Drawing.Color.Yellow;
   this.labelWind.Location = new System.Drawing.Point(32, 184);
   this.labelWind.Name = "labelWind";
   this.labelWind.Size = new System.Drawing.Size(536, 36);
   this.labelWind.TabIndex = 56;
   this.labelWind.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   this.labelWind.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseUp);
   this.labelWind.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseMove);
   this.labelWind.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseDown);
   //
   // labelHumidity
   //
   this.labelHumidity.BackColor = System.Drawing.Color.Transparent;
   this.labelHumidity.ForeColor = System.Drawing.Color.Yellow;
   this.labelHumidity.Location = new System.Drawing.Point(88, 232);
   this.labelHumidity.Name = "labelHumidity";
   this.labelHumidity.Size = new System.Drawing.Size(192, 24);
   this.labelHumidity.TabIndex = 57;
   this.labelHumidity.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   this.labelHumidity.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseUp);
   this.labelHumidity.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseMove);
   this.labelHumidity.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseDown);
   //
   // labelDewPoint
   //
   this.labelDewPoint.BackColor = System.Drawing.Color.Transparent;
   this.labelDewPoint.ForeColor = System.Drawing.Color.Yellow;
   this.labelDewPoint.Location = new System.Drawing.Point(288, 232);
   this.labelDewPoint.Name = "labelDewPoint";
   this.labelDewPoint.Size = new System.Drawing.Size(208, 24);
   this.labelDewPoint.TabIndex = 58;
   this.labelDewPoint.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   this.labelDewPoint.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseUp);
   this.labelDewPoint.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseMove);
   this.labelDewPoint.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnChildMouseDown);
   //
   // linkLabelConfigure
   //
   this.linkLabelConfigure.BackColor = System.Drawing.Color.Transparent;
   this.linkLabelConfigure.Font = new System.Drawing.Font("MS Reference Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
   this.linkLabelConfigure.LinkColor = System.Drawing.Color.Yellow;
   this.linkLabelConfigure.Location = new System.Drawing.Point(184, 264);
   this.linkLabelConfigure.Name = "linkLabelConfigure";
   this.linkLabelConfigure.Size = new System.Drawing.Size(240, 24);
   this.linkLabelConfigure.TabIndex = 59;
   this.linkLabelConfigure.TabStop = true;
   this.linkLabelConfigure.Text = "Configure";
   this.linkLabelConfigure.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   this.linkLabelConfigure.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabelConfigure_LinkClicked);
   //
   // FormMain
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 17);
   this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
   this.ClientSize = new System.Drawing.Size(597, 312);
   this.Controls.Add(this.linkLabelConfigure);
   this.Controls.Add(this.labelDewPoint);
   this.Controls.Add(this.labelHumidity);
   this.Controls.Add(this.labelWind);
   this.Controls.Add(this.labelAirPressure);
   this.Controls.Add(this.labelVisibility);
   this.Controls.Add(this.labelSkyConditions);
   this.Controls.Add(this.labelTemperature);
   this.Controls.Add(this.labelLocation);
   this.Controls.Add(this.linkLabelUpdate);
   this.Controls.Add(this.buttonClose);
   this.Font = new System.Drawing.Font("Comic Sans MS", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
   this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
   this.Name = "FormMain";
   this.Text = "Weather Information";
   this.TransparencyKey = System.Drawing.Color.Lime;
   this.ResumeLayout(false);

  }
  #endregion

    /// <summary>
    /// Update the weather report locations.
    /// </summary>
    /// <remarks>
    /// Go to the server and get new
    /// weather information. Once retrieved,
    /// put it in the text boxes.
    /// </remarks>
    public void UpdateWeatherReport( WeatherData results )
    {
      labelLocation.Text = string.Format("Weather data updated{1}{0}{1}at {2}.",
        results.TimeRecorded ,
        System.Environment.NewLine,
        results.Location );
      labelTemperature.Text = string.Format( "Temperature is {0}", results.Temperature );
      labelSkyConditions.Text = string.Format( "Skies are {0}", results.SkyConditions );
      labelVisibility.Text = string.Format( "Current Visibility is {0}", results.Visibility );
      labelAirPressure.Text = string.Format( "Barometer is {0}, which is {1}", results.AirPressure,
        results.PressureTrend );
      labelWind.Text = string.Format( "Wind is {0}, producing a  {1}",
        results.WindSpeed, results.WindChill );
      labelHumidity.Text = string.Format( "Relative Humidity is {0}", results.RelativeHumidity );
      labelDewPoint.Text = string.Format( "The dewpoint is {0}", results.DewPoint );

      this.Invalidate();
    }

  /// <summary>
  /// Don't close.  Just hide.
  /// </summary>
  /// <param name="e">Cancel event args</param>
    protected override void OnClosing(CancelEventArgs e)
    {
      e.Cancel = true;
      this.Visible = false;
      base.OnClosing (e);
    }

  /// <summary>
  /// Load handler.
  /// </summary>
  /// <param name="e">unused</param>
  /// <remarks>
  /// Fix for KB article http://www.kbalertz.com/Feedback_822495.aspx
  /// If the monitor is at greater than 24 bit resolution, you need to fix the Bitmap.
  /// As you'll see in the KB article, you must reset the
  /// trnasparency key on some monitors.
  /// After handling that, update the weather information.
  /// </remarks>
  protected override void OnLoad(EventArgs e)
    {
   Bitmap bm = this.BackgroundImage as Bitmap;
   bm.MakeTransparent( bm.GetPixel( 0,0 ) );
   this.TransparencyKey = bm.GetPixel( 0, 0 );
   
   UpdateWeatherReport( ApplicationMain.CurrentResults );
      base.OnLoad (e);
    }

  /// <summary>
  /// Handler for the close button
  /// </summary>
  /// <param name="sender">unused</param>
  /// <param name="e">unused</param>
  /// <remarks>
  /// Just hide instead.
  /// </remarks>
  private void buttonClose_Click(object sender, System.EventArgs e)
  {
   this.Visible = false;
  }

  /// <summary>
  /// Mouse down handler.
  /// </summary>
  /// <param name="e">Mouse event args</param>
  /// <remarks>
  /// If the user pressed the mouse on the visible parts of the
  /// window, start a move.
  /// </remarks>
    protected override void OnMouseDown(MouseEventArgs e)
    {
      if ( e.Button == MouseButtons.Left )
      {
        System.Drawing.Bitmap bm = this.BackgroundImage as System.Drawing.Bitmap;
        if ( bm != null )
        {
          System.Drawing.Color c = bm.GetPixel( e.X, e.Y );
          if ( c.ToArgb() == TransparencyKey.ToArgb() )
            return;
        }
        _isMoving = true;
        XOffset = e.X;
        YOffset = e.Y;
      }
      base.OnMouseDown (e);
    }

  /// <summary>
  /// Mouse Move handler.
  /// </summary>
  /// <param name="e">Mouse event args.</param>
  /// <remarks>
  /// If the user is moving the form, move it.
  /// </remarks>
    protected override void OnMouseMove(MouseEventArgs e)
    {
      if ( _isMoving )
      {
        this.Location = new Point(this.Location.X + e.X - XOffset,
          this.Location.Y + e.Y - YOffset );
      }
      base.OnMouseMove (e);
    }
   
  /// <summary>
  /// Mouse up handler.
  /// </summary>
  /// <param name="e">event args.</param>
  /// <remarks>
  /// Stop moving the mouse.
  /// </remarks>
    protected override void OnMouseUp(MouseEventArgs e)
    {
      if ( e.Button == MouseButtons.Left )
      {
        _isMoving = false;
      }
      base.OnMouseUp (e);
    }

  /// <summary>
  /// Update button handler.
  /// </summary>
  /// <param name="sender">unused</param>
  /// <param name="e">unused</param>
  /// <remarks>
  /// Get new results and update the form.
  /// </remarks>
    private void linkLabelUpdate_LinkClicked(object sender,
   System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
    {
      UpdateWeatherReport( ApplicationMain.CurrentResults );
    }

  /// <summary>
  /// Configure button handler.
  /// </summary>
  /// <param name="sender">unused</param>
  /// <param name="e">unused</param>
  /// <remarks>
  /// Let the main application configure itself.
  /// </remarks>
    private void linkLabelConfigure_LinkClicked(object sender,
   System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
    {
      ApplicationMain.ConfigureApp();
    }

  private void OnChildMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   if ( e.Button == MouseButtons.Left )
   {
    // No need to check for transparency here, because the
    // child controls are not on transparent areas.
    _isMoving = true;
    XOffset = e.X;
    YOffset = e.Y;
   }
  }

  private void OnChildMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   if ( _isMoving )
   {
    Control c = sender as Control;
    this.Location = new Point(this.Location.X + e.X - XOffset,
     this.Location.Y + e.Y - YOffset );
   }
  }

  private void OnChildMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   if ( e.Button == MouseButtons.Left )
   {
    _isMoving = false;
   }
  }
 }
}

posts - 0, comments - 5, trackbacks - 0, articles - 6

Copyright © 藤条焖猪肉