namespace WindowsFormsApplication5
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
/// <summary>
/// Main application form
/// </summary>
public partial class Form1 : Form
{
/// <summary>
/// Initializes a new instance of the WindowsFormsApplication5.Form1 class
/// </summary>
public Form1() {
InitializeComponent();
DoubleBuffered = true;
}
private Point selectionStart;
private Point selectionEnd;
private Rectangle selection;
private bool mouseDown;
private void GetSelectedTextBoxes() {
List<TextBox> selected = new List<TextBox>();
foreach (Control c in Controls) {
if (c is TextBox) {
if (selection.IntersectsWith(c.Bounds)) {
selected.Add((TextBox)c);
}
}
}
// Replace with your input box
MessageBox.Show("You selected " + selected.Count + " textbox controls.");
}
protected override void OnMouseDown(MouseEventArgs e) {
selectionStart = PointToClient(MousePosition);
mouseDown = true;
}
protected override void OnMouseUp(MouseEventArgs e) {
mouseDown = false;
SetSelectionRect();
Invalidate();
GetSelectedTextBoxes();
}
protected override void OnMouseMove(MouseEventArgs e) {
if (!mouseDown) {
return;
}
selectionEnd = PointToClient(MousePosition);
SetSelectionRect();
Invalidate();
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
if (mouseDown) {
using (Pen pen = new Pen(Color.Black, 1F)) {
pen.DashStyle = DashStyle.Dash;
e.Graphics.DrawRectangle(pen, selection);
}
}
}
private void SetSelectionRect() {
int x, y;
int width, height;
x = selectionStart.X > selectionEnd.X ? selectionEnd.X : selectionStart.X;
y = selectionStart.Y > selectionEnd.Y ? selectionEnd.Y : selectionStart.Y;
width = selectionStart.X > selectionEnd.X ? selectionStart.X - selectionEnd.X : selectionEnd.X - selectionStart.X;
height = selectionStart.Y > selectionEnd.Y ? selectionStart.Y - selectionEnd.Y : selectionEnd.Y - selectionStart.Y;
selection = new Rectangle(x, y, width, height);
}
}
}
'개발지식창고 > C#' 카테고리의 다른 글
ClassLibrary 프로젝트에서 종속 DLL path 변경법 (0) | 2015.03.01 |
---|---|
COM+ 생성방법 (0) | 2015.02.25 |
COM+ 컴포넌트 멀티쓰레드 실행을 위한 IIS설정 (0) | 2015.02.14 |
.net Framework Error (HRESULT 0x8007000B) (0) | 2015.02.11 |
C#에서 DebugView 사용하기 (0) | 2015.02.02 |