|
■No53581 (すなふきぬ さん) に返信
> 2010/09/19(Sun) 23:54:42 編集(投稿者)
> 2010/09/19(Sun) 21:23:27 編集(投稿者)
> #なんか知らないけど、荒らしとか言われるのも心外なので、ソース削除しておきます。
ちょっと来なかったら荒らしのカキコ削除されてた。
必要ないかもしれませんが、ソースだけ再UP。
public partial class Form1 : Form
{
DataTable _dt;
public Form1()
{
InitializeComponent();
_dt = new DataTable();
_dt.Columns.Add("Value", typeof(int));
_dt.Columns.Add("Name", typeof(string));
_dt.Columns.Add("canSelected", typeof(bool));
for (int i = 0; i < 6; i++)
{
DataRow r = _dt.NewRow();
r[0] = i;
r[1] = string.Format("項目{0}", i + 1);
r[2] = ((i % 2) == 1);
_dt.Rows.Add(r);
}
this.comboBox1.ValueMember = "Value";
this.comboBox1.DisplayMember = "Name";
this.comboBox1.DataSource = _dt;
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
this.comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);
this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
}
void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!this.Created) return;
DataRowView drv = (DataRowView)this.comboBox1.SelectedItem;
bool isSelected = (bool)drv.Row["canSelected"];
if (!isSelected) this.comboBox1.DroppedDown = true;
}
void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0) return;
DataRowView drv = (DataRowView)((ComboBox)sender).Items[e.Index];
string name = drv.Row["Name"].ToString();
bool canSelected = (bool)drv.Row["canSelected"];
bool isHilight = this.isActiveEditItem(e.State) | this.isHoverItem(e.State);
Brush brFore = (isHilight & canSelected) ?
SystemBrushes.ControlLightLight:
SystemBrushes.ControlText;
Brush brBack = !canSelected ?
SystemBrushes.ControlLight :
(isHilight ?
SystemBrushes.Highlight :
SystemBrushes.ControlLightLight
);
e.Graphics.FillRectangle(brBack, e.Bounds);
int drawOffsetX = e.Bounds.X + (canSelected ? 10 : 0);
Font fnt = new Font(((Control)sender).Font, (canSelected ? FontStyle.Regular : FontStyle.Bold));
e.Graphics.DrawString(name, fnt, brFore, drawOffsetX, e.Bounds.Y + 1);
if (isHilight & canSelected)
{
using (Pen p = new Pen(Color.FromArgb(SystemColors.Highlight.ToArgb() ^ 0x00FFFFFF)))
{
p.DashStyle = DashStyle.Dot;
Rectangle rct = e.Bounds;
rct.Height--;
rct.Width--;
e.Graphics.DrawRectangle(p, rct);
}
}
}
public bool isActiveEditItem(DrawItemState state)
{
return ((state & DrawItemState.Selected) == DrawItemState.Selected) &&
((state & DrawItemState.Focus) == DrawItemState.Focus) &&
((state & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit);
}
public bool isHoverItem(DrawItemState state)
{
return ((state & DrawItemState.Selected) == DrawItemState.Selected) &&
((state & DrawItemState.Focus) == DrawItemState.Focus);
}
}
|