这是我的工作代码:
首先在form1的顶部:
int firsttime;
private const int ROW_SIZE = 22;
private int NumberOfRows;
Image ima;
private System.Timers.Timer _refreshTimer;
private int _thisProcess;
然后在构造函数中:
firsttime = 0;
dataGridView1.BackgroundColor = System.Drawing.Color.White;
dataGridView1.Columns["Column2"].DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow;
dataGridView1.Columns["Column3"].DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow;
dataGridView1.AllowUserToAddRows = false;
在form1加载事件中:
private void Form1_Load(object sender, EventArgs e)
{
_thisProcess = Process.GetCurrentProcess().Id;
InitializeRefreshTimer();
PopulateApplications();
}
然后定时器init方法我使用定时器来更新进程列表和dataGridView1。
void InitializeRefreshTimer()
{
_refreshTimer = new System.Timers.Timer(5000);
_refreshTimer.SynchronizingObject = this;
_refreshTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerToUpdate_Elapsed);
_refreshTimer.Start();
}
计时器已用完事件:
void TimerToUpdate_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
PopulateApplications();
}
PopulateApplications方法:
void PopulateApplications()
{
dataGridView1.Rows.Clear();
foreach (Process p in Process.GetProcesses())
{
if (p.MainWindowTitle.Length > 1)
{
var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
ima = icon.ToBitmap();
ima = resizeImage(ima, new Size(25, 25));
ima.Save(@"c:\temp\ima.jpg");
String status = p.Responding ? "Running" : "Not Responding";
dataGridView1.Rows.Add(ima,p.ProcessName, status);
}
}
firsttime += 1;
if (firsttime == 1)
{
NumberOfRows = dataGridView1.Rows.Count;
}
if (NumberOfRows != dataGridView1.Rows.Count)
{
int diff = dataGridView1.Rows.Count - NumberOfRows;
this.Height = this.Height + (ROW_SIZE * diff);
NumberOfRows = dataGridView1.Rows.Count;
}
}
调整大小图像方法:
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
在form1设计器中,我添加了带有9列的dataGridView1,仅用于测试。 在设计者中,form1的大小是:652,358 dataGridView1大小为:624,479
现在每次添加新行时,form1 Height会自动调整大小+ = 22。 如果删除了一行,那意味着如果我关闭任何app /进程,它将自动更新dataGridView1并删除一行,form1也会自动调整大小。
使用程序Paint检查它。打开它关闭了几次,它工作。
答案 0 :(得分:0)
编辑:
我用以下代码替换了原来的答案,因为它更好。您不需要lastValue
和currentValue
个变量。相反,我使用了一个名为NumberOfRows
的变量。我还添加了2个常量ROW_SIZE
(添加到每行的表单高度的数量)和BaseFormHeight
(没有行时表单的高度)。
private const int ROW_SIZE = 22;
private int NumberOfRows;
private int BaseFormHeight
void PopulateApplications()
{
dataGridView1.Rows.Clear();
foreach (Process p in Process.GetProcesses())
{
if (p.MainWindowTitle.Length > 1)
{
... add a row to the data grid ...
}
}
if(NumberOfRows != dataGridView1.Rows.Count)
{
NumberOfRows = dataGridView1.Rows.Count;
this.Height = BaseFormHeight + (ROW_SIZE * NumberOfRows);
}
}
在Load
事件处理程序中,您应该记录表单的初始高度和初始行数。
private void Form1_Load(object sender, EventArgs e)
{
NumberOfRows = dataGridView1.Rows.Count;
BaseFormHeight = this.Height - (NumberOfRows * ROW_SIZE);
PopulateApplications();
}
答案 1 :(得分:0)
这是我的工作代码:
首先在form1的顶部:
int firsttime;
private const int ROW_SIZE = 22;
private int NumberOfRows;
Image ima;
private System.Timers.Timer _refreshTimer;
private int _thisProcess;
然后在构造函数中:
firsttime = 0;
dataGridView1.BackgroundColor = System.Drawing.Color.White;
dataGridView1.Columns["Column2"].DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow;
dataGridView1.Columns["Column3"].DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow;
dataGridView1.AllowUserToAddRows = false;
In the form1 load event:
private void Form1_Load(object sender, EventArgs e)
{
_thisProcess = Process.GetCurrentProcess().Id;
InitializeRefreshTimer();
PopulateApplications();
}
然后定时器init方法我使用定时器来更新进程列表和dataGridView1。
void InitializeRefreshTimer()
{
_refreshTimer = new System.Timers.Timer(5000);
_refreshTimer.SynchronizingObject = this;
_refreshTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerToUpdate_Elapsed);
_refreshTimer.Start();
}
计时器已用完事件:
void TimerToUpdate_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
PopulateApplications();
}
PopulateApplications方法:
void PopulateApplications()
{
dataGridView1.Rows.Clear();
foreach (Process p in Process.GetProcesses())
{
if (p.MainWindowTitle.Length > 1)
{
var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
ima = icon.ToBitmap();
ima = resizeImage(ima, new Size(25, 25));
ima.Save(@"c:\temp\ima.jpg");
String status = p.Responding ? "Running" : "Not Responding";
dataGridView1.Rows.Add(ima,p.ProcessName, status);
}
}
firsttime += 1;
if (firsttime == 1)
{
NumberOfRows = dataGridView1.Rows.Count;
}
if (NumberOfRows != dataGridView1.Rows.Count)
{
int diff = dataGridView1.Rows.Count - NumberOfRows;
this.Height = this.Height + (ROW_SIZE * diff);
NumberOfRows = dataGridView1.Rows.Count;
}
}
调整大小图像方法:
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
在form1设计器中,我添加了带有9列的dataGridView1,仅用于测试。在设计器中,form1大小为:652,358 dataGridView1大小为:624,479
现在每次添加一个新行时,form1 Height会自动调整大小+ = 22.如果删除一行意味着如果我关闭任何应用程序/进程它将自动更新dataGridView1并删除一行同时form1将调整大小自动的。
使用程序Paint检查它。打开它关闭了几次,它工作。