これは C# デスクトップ アプリケーションです。myのDrawStyle
プロパティListBox
は に設定されていOwnerDrawFixed
ます。
問題: DrawItem をオーバーライドして、異なるフォントでテキストを描画すると、機能します。しかし、実行時にフォームのサイズ変更を開始すると、選択したアイテムは正しく描画されますが、残りのアイテムは再描画されず、選択されていないアイテムのテキストが破損しているように見えます。
これが私のコードです:
private void listDevices_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
string textDevice = ((ListBox)sender).Items[e.Index].ToString();
e.Graphics.DrawString(textDevice,
new Font("Ariel", 15, FontStyle.Bold), new SolidBrush(Color.Black),
e.Bounds, StringFormat.GenericDefault);
// Figure out where to draw IP
StringFormat copy = new StringFormat(
StringFormatFlags.NoWrap |
StringFormatFlags.MeasureTrailingSpaces
);
copy.SetMeasurableCharacterRanges(new CharacterRange[] {new CharacterRange(0, textDevice.Length)});
Region[] regions = e.Graphics.MeasureCharacterRanges(
textDevice, new Font("Ariel", 15, FontStyle.Bold), e.Bounds, copy);
int width = (int)(regions[0].GetBounds(e.Graphics).Width);
Rectangle rect = e.Bounds;
rect.X += width;
rect.Width -= width;
// draw IP
e.Graphics.DrawString(" 255.255.255.255",
new Font("Courier New", 10), new SolidBrush(Color.DarkBlue),
rect, copy);
e.DrawFocusRectangle();
}
listDevices.Items.Add("Device001");
listDevices.Items.Add("Device002");
また、正しく描画されたアイテム (選択されたアイテム) は、フォームのサイズ変更時にちらつきます。大したことではありませんが、誰かが理由を知っていれば.... tnx