前提・実現したいこと
現在お絵かきソフトを作っています。
以下のソースのままエラーがでず、実行はできるのですが、クリックしながら黒いペンで描いているつもりでも、描画がされません。
Panelをnew Panelで作成することは、初期のWindowSizeとほぼ一緒にするために、前提として非常に重要なことだと考えていますので、
PanelをWindow Formのツールを使い作成すること以外で、描画を行うということはどのようにできるでしょうか。
該当のソースコード
C#
1 public partial class Form1 : Form 2 { 3 Panel panel1; 4 Graphics g; 5 int x = -1; 6 int y = -1; 7 bool Moving = false; 8 Pen pen; 9 10 public Form1() 11 { 12 InitializeComponent(); 13 CreateMyPanel(); 14 pen = new Pen(Color.Black, 5); 15 g = panel1.CreateGraphics(); 16 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 17 } 18 19 private void Form1_Load(object sender, EventArgs e) 20 { 21 this.Width = Screen.PrimaryScreen.WorkingArea.Width; 22 this.Height = Screen.PrimaryScreen.WorkingArea.Height; 23 this.DesktopLocation = new Point(0, 0); 24 } 25 26 public void CreateMyPanel() 27 { 28 panel1 = new Panel(); 29 panel1.Location = new Point(50, 50); 30 panel1.Size = new Size(Screen.PrimaryScreen.WorkingArea.Width - 100, Screen.PrimaryScreen.WorkingArea.Height - 100); 31 panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; 32 this.Controls.Add(panel1); 33 panel1.MouseDown += new MouseEventHandler(this.Panel_MouseDown); 34 panel1.MouseUp += new MouseEventHandler(this.Panel_MouseUp); 35 panel1.MouseMove += new MouseEventHandler(this.Panel_MouseMove); 36 } 37 private void Panel_MouseDown(object sender, MouseEventArgs e) 38 { 39 Moving = true; 40 y = e.X; 41 y = e.Y; 42 } 43 private void Panel_MouseUp(object sender, MouseEventArgs e) 44 { 45 Moving = false; 46 x = -1; 47 y = -1; 48 } 49 private void Panel_MouseMove(object sender, MouseEventArgs e) 50 { 51 if(Moving && x != -1 && y != -1) 52 { 53 g.DrawLine(pen, new Point(x, y), e.Location); 54 x = e.X; 55 y = e.Y; 56 } 57 } 58 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/23 14:08
2020/04/23 14:11
2020/04/23 14:18
2020/04/23 14:22
2020/04/23 14:25
2020/04/23 14:28
2020/04/23 14:44