C#/ Windows Forms:表单的绘图代码未被执行

时间:2009-11-30 08:17:31

标签: c# windows forms paint

我在几个小时前发布了一个类似但不太具体的问题,但情况发生了变化。我正在开发一个程序来转换图形,将它们呈现在表单左上角的面板中。它之前的绘画很好,但现在不是,我无法撤消,加载旧版本等。以前,应用程序甚至没有响应,甚至菜单中的事件,以及绘画。我开始了一个新项目并启动并运行,菜单工作,我带来了按钮和东西。但它仍然不会在面板中绘制轴和网格线。我在主窗体的paint处理程序和应该执行工作的splitContainer2_Panel1_Paint处理程序中放置了断点,但这里的代码甚至没有被执行。我有一个活跃的计时器,每100毫秒使整个表单无效,那么为什么不调用paint事件处理程序呢?帮助

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.IO;

namespace TransformerA
{
public partial class Transformer : Form
{
    /* Initialize parameters */
    private bool drawAxes = true;
    private bool drawGrid = true;

    private List<ObjectSettings> dispObjects = new List<ObjectSettings>();


    /* Initialize form */

    public Transformer()
    {
        InitializeComponent();
    }

    private void Transformer_Load(object sender, EventArgs e)
    {
        // Populate available objects listbox
        string currentDir = Directory.GetCurrentDirectory();
        string[] fileEntries = Directory.GetFiles(currentDir + @"\Objects");
        foreach (string s in fileEntries) {
            int start = s.LastIndexOf(@"\");
            int end = s.LastIndexOf(@".");
            availObjectsListBox.Items.Add(s.Substring(start + 1, end - start - 1));
        } // end foreach
    }



    /* Paint graphics */

    // Paint main form
    private void Transformer_Paint(object sender, PaintEventArgs e)
    {
        splitContainer2_Panel1_Paint(sender, e);
    }

    // Paint graphics panel
    private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e)
    {
        Rectangle r = splitContainer2.Panel1.ClientRectangle;
        //Graphics g = splitContainer2.Panel1.CreateGraphics();
        Graphics g = e.Graphics;
        Pen axisPen = new Pen(Color.Gray, 2.0f);
        Pen gridPen = new Pen(Color.Gray, 1.0f);

        g.Clear(Color.Blue);

        if (drawAxes) {
            g.DrawLine(axisPen, r.Left + 0.5f * r.Width, r.Top, r.Left + 0.5f * r.Width, r.Bottom);
            g.DrawLine(axisPen, r.Left, r.Top + 0.5f * r.Height, r.Right, r.Top + 0.5f * r.Height);
        }

        if (drawGrid) {
            // Vertical lines
            int xVal = 0;
            int xCenter = r.Width / 2;
            g.DrawLine(gridPen, xCenter, r.Top, xCenter, r.Bottom);
            for (int i = 0; i < 10; i++) {
                xVal += r.Width / 20;
                g.DrawLine(gridPen, xCenter + xVal, r.Top, xCenter + xVal, r.Bottom);
                g.DrawLine(gridPen, xCenter - xVal, r.Top, xCenter - xVal, r.Bottom);
            }

            // Horizontal lines
            int yVal = 0;
            int yCenter = r.Height / 2;
            g.DrawLine(gridPen, r.Left, yCenter, r.Right, yCenter);
            for (int i = 0; i < 10; i++) {
                yVal += r.Height / 20;
                g.DrawLine(gridPen, r.Left, yCenter + yVal, r.Right, yCenter + yVal);
                g.DrawLine(gridPen, r.Left, yCenter - yVal, r.Right, yCenter - yVal);
            }
        }



        // foreach object in displayed objects
        // keep list of displayed objects and their settings (make struct)


        g.Dispose();
        axisPen.Dispose();
        gridPen.Dispose();
    }


    /* File menu */

    private void saveImageToolStripMenuItem_Click(object sender, EventArgs e)
    {

    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Close();
    }


    /* Options menu */

    private void axesOnoffToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (drawAxes == true)
            drawAxes = false;
        else
            drawAxes = true;
    }

    private void gridOnoffToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (drawGrid == true)
            drawGrid = false;
        else
            drawGrid = true;
    }


    /* Help menu */

    private void helpToolStripMenuItem_Click(object sender, EventArgs e)
    {
        AboutBox dlg = new AboutBox();
        dlg.ShowDialog();
    }


    /* Other */

    private void timer1_Tick(object sender, EventArgs e)
    {
        Invalidate();
    }
}
}

1 个答案:

答案 0 :(得分:0)

哎呀,修复它,我需要从设计器中添加事件处理程序...我只是粘贴在旧的unworking版本的处理程序代码中,所以它没有将所需的代码添加到设计器代码中。仍然无法弄清楚为什么它之前停止工作但至少它现在正在运行。