2016-07-30 11 views
1

私はこのイメージのように私のコンボボックスをカスタマイズする必要があります。私はカスタムコンボボックスの中央のテキスト

enter image description here
2.背景ターン以下のようにウィンドウを最小化し、左へ
1.テキストを移動:それは2つの問題を持っていますfine.But私のコードが働い

enter image description here
別のウィンドウが自分のアプリケーションをカバーしているときに緑に変わります。ここで

enter image description here

私のコードです:

//DrawItem 
protected override void OnDrawItem(DrawItemEventArgs e) 
{ 
    e.DrawBackground(); 
    e.DrawFocusRectangle(); 
    if (e.Index >= 0) { 
    Graphics g = e.Graphics; 
    Brush brs = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? 
    new SolidBrush(SelectedBackColor) : new SolidBrush(e.BackColor); 
    g.FillRectangle(brs, e.Bounds); 
    using (StringFormat sformat = new StringFormat()) { 
    sformat.LineAlignment = StringAlignment.Center; 
     sformat.Alignment = StringAlignment.Center; 
     e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, sformat); 
} 
//paint 
protected override void OnPaint(PaintEventArgs e) 
{ 
    base.OnPaint(e); 
    if (index >= 0) { 
     using (Brush br = new SolidBrush(this.ForeColor)) { 
     StringFormat sformat = new StringFormat(); 
     sformat.LineAlignment = StringAlignment.Center; 
     sformat.Alignment = StringAlignment.Center; 
     e.Graphics.DrawString(this.Text, this.Font, br, this.ClientRectangle, sformat); 
     e.Graphics.DrawImage(Resource1.arrow,this.ClientRectangle.Right - 34, 0,32,32); 
     } 
    } 
} 

ので、何が間違っていますか?ドロップダウンstate.Iは前DroppedDownをチェックアウトし、私は問題を発見

SizeF size = new SizeF(); 
size = e.Graphics.MeasureString(this.Text, this.Font); 
PointF DrawPoint = new PointF((this.Width - size.Width)/2, (this.Height - size.Height)/2); 
e.Graphics.DrawString(this.Text, this.Font, br, DrawPoint, sformat); 
+0

コードはコンパイルされません。少なくとも2つの閉じ括弧が欠けている。実際のコードだけを投稿してください! - 'e.Bounds'や' ClientRectangle'は実際に矢印のスペースを残しますか?それ以外の場合:エラーの状況は面白く聞こえます。最小化した後、復元を前提にして、配置/配置が間違っていますか? – TaW

+0

私の間違い申し訳ありません。私は問題がe.Boundsだと思う。コンボボックスのサイズは135,34、e.Boudsは133です。ウィンドウを最小化すると、e.Boundsは112になります。e.Boundsは、ミニマイズするとサイズが変更されます。どのように修正するのですか?@TaW – Jandy

答えて

0

あなたは、描画した後、決定する文字列のサイズを取得するためにGraphics.MeasureStringを使用することができますブラシを設定します。

protected override void OnDrawItem(DrawItemEventArgs e) 
{ 
    e.DrawBackground(); 
    if (e.Index >= 0 && DroppedDown) { 
    Brush brs = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? 
    new SolidBrush(SelectedBackColor) : new SolidBrush(Color.Red); 
    e.Graphics.FillRectangle(brs, e.Bounds); 
    using (StringFormat sformat = new StringFormat()) { 
     sformat.LineAlignment = StringAlignment.Center; 
     sformat.Alignment = StringAlignment.Center; 
     e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, sformat); 
} 
+0

私の後半は申し訳ありません。変更はありません。ウィンドウが@ Ashkan Mobayen Khiabaniを最小化するときにe.Boundsと選択されたアイテムスタイルを修正する方法を探しています – Jandy

0

きている:文字列を描画を開始する場所を

関連する問題