CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 221

221

Chương 6: Windows Form

using System;

using System.Windows.Forms;

using System.Drawing;

public class DragForm : System.Windows.Forms.Form {

// (Bỏ qua phần mã designer.)

// Biến cờ dùng để theo vết form.

// Nếu đang ở chế độ kéo rê, việc di chuyển chuột

// trên Label sẽ được chuyển thành việc di chuyển form.

private bool dragging;

// Lưu trữ offset (vị trí được nhắp vào trên Label).

private Point pointClicked;

private void lblDrag_MouseDown(object sender,

System.Windows.Forms.MouseEventArgs e) {

if (e.Button == MouseButtons.Left) {

dragging = true;

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

}

else {

dragging = false;

}

}

private void lblDrag_MouseMove(object sender,

System.Windows.Forms.MouseEventArgs e) {

if (dragging) {

Point pointMoveTo;

// Tìm vị trí hiện tại của chuột trong tọa độ màn hình.

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