consolidate propertygrid heigh changing logic

This commit is contained in:
adelikat 2020-08-29 11:27:00 -05:00
parent 0951ef084f
commit be6b1fa6e4
3 changed files with 29 additions and 54 deletions

View File

@ -335,31 +335,38 @@ namespace BizHawk.Client.EmuHawk
public static bool IsCtrlShift(this KeyEventArgs e, Keys key) public static bool IsCtrlShift(this KeyEventArgs e, Keys key)
=> !e.Alt && e.Control && e.Shift && e.KeyCode == key; => !e.Alt && e.Control && e.Shift && e.KeyCode == key;
// For inexplicable reasons, property grid does not expose a way to do this /// <summary>
// https://www.codeproject.com/Articles/28193/Change-the-height-of-a-PropertyGrid-s-description?msg=3379905#xx3379905xx /// Changes the description heigh area to match the rows needed for the largest description in the list
public static void SetDescriptionRowHeight(this PropertyGrid grid, int numRows) /// </summary>
public static void AdjustDescriptionHeightToFit(this PropertyGrid grid)
{ {
try try
{ {
var controlsProp = grid.GetType().GetProperty("Controls"); int maxLength = 0;
var controlsCollection = (Control.ControlCollection)controlsProp.GetValue(grid, null); string desc = "";
foreach(Control c in controlsCollection) foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(grid.SelectedObject))
{ {
Type ct = c.GetType(); if (property.Description?.Length > maxLength)
string sName = ct.Name;
if (sName == "DocComment")
{ {
var controlsProp2 = ct.GetProperty("Lines"); maxLength = property.Description.Length;
controlsProp2.SetValue(c, numRows, null); desc = property.Description;
}
}
FieldInfo fi = ct.BaseType.GetField("userSized", BindingFlags.Instance | BindingFlags.NonPublic); foreach (Control control in grid.Controls)
fi.SetValue(c, true); {
if (control.GetType().Name == "DocComment")
{
FieldInfo field = control.GetType().GetField("userSized", BindingFlags.Instance | BindingFlags.NonPublic);
field?.SetValue(control, true);
int height = (int)Graphics.FromHwnd(control.Handle).MeasureString(desc, control.Font, grid.Width).Height;
control.Height = Math.Max(20, height) + 16; // magic for now
return;
} }
} }
} }
catch (Exception ex) catch
{ {
// Eat it // Eat it
} }

View File

@ -35,7 +35,7 @@ namespace BizHawk.Client.EmuHawk
if (_s != null) if (_s != null)
{ {
propertyGrid1.SelectedObject = _s; propertyGrid1.SelectedObject = _s;
ChangeDescriptionHeight(propertyGrid1); propertyGrid1.AdjustDescriptionHeightToFit();
} }
else else
{ {
@ -45,7 +45,7 @@ namespace BizHawk.Client.EmuHawk
if (_ss != null) if (_ss != null)
{ {
propertyGrid2.SelectedObject = _ss; propertyGrid2.SelectedObject = _ss;
ChangeDescriptionHeight(propertyGrid2); propertyGrid2.AdjustDescriptionHeightToFit();
} }
else else
{ {
@ -58,38 +58,6 @@ namespace BizHawk.Client.EmuHawk
} }
} }
private static void ChangeDescriptionHeight(PropertyGrid grid)
{
if (grid == null)
{
throw new ArgumentNullException(nameof(grid));
}
int maxLength = 0;
string desc = "";
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(grid.SelectedObject))
{
if (property.Description?.Length > maxLength)
{
maxLength = property.Description.Length;
desc = property.Description;
}
}
foreach (Control control in grid.Controls)
{
if (control.GetType().Name == "DocComment")
{
FieldInfo field = control.GetType().GetField("userSized", BindingFlags.Instance | BindingFlags.NonPublic);
field?.SetValue(control, true);
int height = (int)System.Drawing.Graphics.FromHwnd(control.Handle).MeasureString(desc, control.Font, grid.Width).Height;
control.Height = Math.Max(20, height) + 16; // magic for now
return;
}
}
}
private void OkBtn_Click(object sender, EventArgs e) private void OkBtn_Click(object sender, EventArgs e)
{ {
if (_s != null && _settingsChanged) if (_s != null && _settingsChanged)

View File

@ -26,7 +26,7 @@ namespace BizHawk.Client.EmuHawk
private void GreenzoneSettings_Load(object sender, EventArgs e) private void GreenzoneSettings_Load(object sender, EventArgs e)
{ {
SettingsPropertyGrid.SetDescriptionRowHeight(9); SettingsPropertyGrid.AdjustDescriptionHeightToFit();
} }
private void OkBtn_Click(object sender, EventArgs e) private void OkBtn_Click(object sender, EventArgs e)