Tải bản đầy đủ - 0 (trang)
7-15. Make a Borderless Form Movable

7-15. Make a Borderless Form Movable

Tải bản đầy đủ - 0trang

CHAPTER 7 ■ WINDOWS FORMS



Figure 7-11. A movable borderless form



The Code

The following example creates a borderless form that a user can move by clicking a form control and

dragging the form:

using System;

using System.Drawing;

using System.Windows.Forms;

namespace Apress.VisualCSharpRecipes.Chapter07

{

public partial class Recipe07_15 : Form

{

// Boolean member tracks whether the form is in drag mode. If it is,

// mouse movements over the label will be translated into form movements.

private bool dragging;

// Stores the offset where the label is clicked.

private Point pointClicked;

public Recipe07_15()

{

// Initialization code is designer generated and contained

// in a separate file named Recipe07-15.Designer.cs.

InitializeComponent();

}

// MouseDown event handler for the label initiates the dragging process.

private void lblDrag_MouseDown(object sender, MouseEventArgs e)

{



344



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



if (e.Button == MouseButtons.Left)

{

// Turn drag mode on and store the point clicked.

dragging = true;

pointClicked = new Point(e.X, e.Y);

}

else

{

dragging = false;

}

}

// MouseMove event handler for the label processes dragging movements if

// the form is in drag mode.

private void lblDrag_MouseMove(object sender, MouseEventArgs e)

{

if (dragging)

{

Point pointMoveTo;

// Find the current mouse position in screen coordinates.

pointMoveTo = this.PointToScreen(new Point(e.X, e.Y));

// Compensate for the position the control was clicked.

pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);

// Move the form.

this.Location = pointMoveTo;

}

}

// MouseUp event handler for the label switches off drag mode.

private void lblDrag_MouseUp(object sender, MouseEventArgs e)

{

dragging = false;

}

private void cmdClose_Click(object sender, EventArgs e)

{

this.Close();

}

[STAThread]

public static void Main(string[] args)

{

Application.Run(new Recipe07_15());

}

}

}



345



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



7-16. Create an Animated System Tray Icon

Problem

You need to create an animated system tray icon (perhaps to indicate the status of a long-running task).



Solution

Create and show a NotifyIcon control. Use a timer that fires periodically (every second or so) and

updates the NotifyIcon.Icon property.



How It Works

The .NET Framework makes it easy to show a system tray icon with the NotifyIcon component. You

simply need to add this component to a form and supply an icon by setting the Icon property. You can

animate a system tray icon by swapping the icon periodically. Optionally, you can add a linked context

menu to the NotifyIcon through the ContextMenuStrip property. The NotifyIcon component

automatically displays its context menu when it’s right-clicked.



The Code

The following example uses eight icons, each of which shows a moon graphic in a different stage of

fullness. By moving from one image to another, the illusion of animation is created.

using System;

using System.Drawing;

using System.Windows.Forms;

namespace Apress.VisualCSharpRecipes.Chapter07

{

public partial class Recipe07_16 : Form

{

// An array to hold the set of icons used to create the

// animation effect.

private Icon[] images = new Icon[8];

// An integer to identify the current icon to display.

int offset = 0;

public Recipe07_16()

{

// Initialization code is designer generated and contained

// in a separate file named Recipe07-16.Designer.cs.

InitializeComponent();



346



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



// Declare a ContextMenuStrip for use by the NotifyIcon.

ContextMenuStrip contextMenuStrip = new ContextMenuStrip();

contextMenuStrip.Items.Add(new ToolStripMenuItem("About..."));

contextMenuStrip.Items.Add(new ToolStripSeparator());

contextMenuStrip.Items.Add(new ToolStripMenuItem("Exit"));

// Assign the ContextMenuStrip to the NotifyIcon.

notifyIcon.ContextMenuStrip = contextMenuStrip;

}

protected override void OnLoad(EventArgs e)

{

// Call the OnLoad method of the base class to ensure the Load

// event is raised correctly.

base.OnLoad(e);

// Load the

images[0] =

images[1] =

images[2] =

images[3] =

images[4] =

images[5] =

images[6] =

images[7] =



basic set of eight icons.

new Icon("moon01.ico");

new Icon("moon02.ico");

new Icon("moon03.ico");

new Icon("moon04.ico");

new Icon("moon05.ico");

new Icon("moon06.ico");

new Icon("moon07.ico");

new Icon("moon08.ico");



}

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

// Change the icon. This event handler fires once every second

// (1000 ms).

notifyIcon.Icon = images[offset];

offset++;

if (offset > 7) offset = 0;

}

[STAThread]

public static void Main(string[] args)

{

Application.Run(new Recipe07_16());

}

}

}



7-17. Validate an Input Control

Problem

You need to alert the user of invalid input in a control, such as a TextBox.



347



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

7-15. Make a Borderless Form Movable

Tải bản đầy đủ ngay(0 tr)

×