2016-07-30 3 views
1

이 이미지처럼 내 콤보 박스를 맞춤 설정해야합니다. 내가맞춤 콤보 박스의 가운데 텍스트

enter image description here
2. 배경 차례 아래로 창을 최소화 할 때 왼쪽
1. 텍스트 이동 :

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); 
     } 
    } 
} 

그렇게 잘못된 것입니다 : 여기

enter image description here

내 코드? 드롭 다운 주라는 전에 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

코드가 컴파일되지 않습니다. 적어도 두 개의 닫는 중괄호가 빠져 있습니다. 실제 코드 만 게시하십시오! -'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

한 경우 : 문자열을 그리기 시작 위치를

관련 문제