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;
namespace 直線描画
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, System.EventArgs e)
{
// Graphics オブジェクトの取得
_bitmap = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width,
Screen.PrimaryScreen.WorkingArea.Height);
pictureBox1.Parent = pictureBox2;
}
Graphics grfx; // Graphics オブジェクト
int start = 0; // 1 = 始点 確定
int startX; // Line X 始点
int startY; // Line Y 始点
int end = 0; // 1 = 終点 確定
int endX; // Line X 終点
int endY; // Line Y 終点
Bitmap _bitmap = null;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
grfx = Graphics.FromImage(_bitmap);
start = 1;
startX = e.X;
startY = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (start == 0) return;
end = 1;
endX = e.X;
endY = e.Y;
pictureBox1.Refresh(); // マウスのドラッグ中の直線表示を要求
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
start = 0;
if (end == 0) return;
end = 0;
grfx.DrawLine(Pens.Magenta, startX, startY, endX, endY);
//pictureBox1.Refresh();
grfx.Dispose();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// マウスのドラッグ中の直線表示
if (end == 0) return;
e.Graphics.DrawLine(Pens.Magenta, startX, startY, endX, endY);
}
}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/03 10:42
2016/06/03 11:45