所以我一直把这个图形转换程序放在一起,然后我突然发现一些改变使得应用程序没有响应。菜单不再起作用,它应该在其中一个面板上绘制轴和网格......没有。有什么想法吗?
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 Transformer
{
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)
{
}
// Paint graphics panel
private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e)
{
Rectangle r = splitContainer2.Panel1.ClientRectangle;
Graphics g = splitContainer2.Panel1.CreateGraphics();
Pen axisPen = new Pen(Color.Gray, 2.0f);
Pen gridPen = new Pen(Color.Gray, 1.0f);
g.Clear(Color.White);
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 stuff */
private void timer1_Tick(object sender, EventArgs e)
{
Invalidate();
}
// ">>" button
private void availToDispButton_Click(object sender, EventArgs e)
{
dispObjectsListBox.Items.Add(availObjectsListBox.SelectedItem);
}
// "<<" button
private void dispToAvailButton_Click(object sender, EventArgs e)
{
availObjectsListBox.Items.Add(dispObjectsListBox.SelectedItem);
dispObjectsListBox.Items.Remove(dispObjectsListBox.SelectedItem);
}
// Clear all button
private void clearAllButton_Click(object sender, EventArgs e)
{
}
// Update preview box
private void availObjectsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
谢谢!
答案 0 :(得分:1)
尝试(单独)注释掉“加载”和“绘制”代码,看看问题是什么。
如果问题是油漆...我想 - 而不是创建自己的Graphics
,使用给你的那个?即,e.Graphics
。请注意,您没有创建它,因此Dispose()
它不是您的工作(所以不要这样做)。我还会在字段中缓存Pen
等,而不是每次都创建它们。请注意,如果您执行在方法中创建Pen
(等),则using
是Dispose()
更好的方法。
油漆代码中还有foreach
条评论表明某些内容已被删除 - 这可能与问题有关......
答案 1 :(得分:0)
如果在加载时发生这种情况,那么显然目录中的文件数量可能导致GUI线程挂起。
除此之外,我的快速查看只会让我考虑检查您用来控制绘图的bool,并确保您使用paint事件的面板实际上是可见的。
您还应检查您的计时器是否正在滴答,并检查其间隔。
我还会考虑使用using语句,或至少使用finally块进行处理。但这不是你的问题所在。
其中大部分是显而易见的,你可能已经在发布之前检查了所有内容,但我想如果你错过它我会提出一些东西。希望我有机会更详细地浏览并发现其他内容。