【发布时间】:2017-06-23 09:15:28
【问题描述】:
当我第一次打开我的 Windows 窗体应用程序时,一些按钮无法正确呈现,当窗体稍微移动时它们会出现。
见下文。
拖动它们出现的表单后。
这是我的启动代码。
[STAThread]
static void Main()
{
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "Support_Desk_System", out createdNew))
{
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
GC.KeepAlive(mutex);
}
else
{
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
if (process.Id != current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
break;
}
}
}
}
}
这不是一个大问题,只是烦人,因为我不知道为什么会这样。
这是表单代码。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.status_CodesTableAdapter.Fill(this.supportDeskDataSet.Status_Codes);
this.systemsTableAdapter.Fill(this.supportDeskDataSet.Systems);
this.cases_Quick_ViewTableAdapter.Fill(this.supportDeskDataSet.Cases_Quick_View);
this.dataGridView1.DoubleBuffered(true);
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DataGridViewRow Myrow in dataGridView1.Rows)
{
if (Myrow.Cells[7].Value.ToString() == "High")
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
else
{
Myrow.DefaultCellStyle.BackColor = Color.Green;
}
if(Myrow.Cells[2].Value.ToString() == "Back Burner")
{
Myrow.DefaultCellStyle.BackColor = Color.Gray;
Myrow.DefaultCellStyle.ForeColor = Color.White;
}
}
}
}
public static class DataGridViewExtensioncs
{
public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
var dgvType = dgv.GetType();
var pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, setting, null);
}
}
【问题讨论】:
-
我认为问题不在于该代码...您是否在表单的构造函数或
Load事件中做任何事情? -
我同意@Pikoh。但是,您的 Main 方法看起来很可疑。你确定,带有 SetForegroundWindow 的分支是正确的吗?
-
我不认为是@TcKs,应该是
if (Process.GetProcessesByName(current.ProcessName).Any()) {SetForegroundWindow(process.MainWindowHandle);。但这与问题无关:)