Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9725

cscの作法 その33

$
0
0

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

マウスで線を描け。

写真

image.png

サンプルコード

using System;
using System.Windows.Forms;
using System.Drawing;

class form1: Form {
    int px = 0;
    int py = 0;
    Bitmap bmp;
    form1() {
        Text = "oe";
        ClientSize = new Size(300, 300);
        bmp = new Bitmap(300, 300);
        this.MouseMove += new MouseEventHandler(mouseMove);
        this.MouseDown += new MouseEventHandler(mouseDown);
    }
    void mouseDown(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Left)
        {
            px = e.X;
            py = e.Y;
        }
    } 
    void mouseMove(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Left)
        {
            Text = "mouse: (" + e.X + ", " + e.Y + ")";
            Graphics g = Graphics.FromImage(bmp);
            Pen p0 = new Pen(Color.Red);
            p0.Width = 3;
            g.DrawLine(p0, px, py, e.X, e.Y);
            px = e.X;
            py = e.Y;
            Invalidate();
        }
    }
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.DrawImage(bmp, 0, 0);
        base.OnPaint(e);
    }
    [STAThread]
    public static void Main() {
        Application.Run(new form1());
    }
}





以上。


Viewing all articles
Browse latest Browse all 9725

Trending Articles