Custom Combobox中的中心文本

时间:2016-07-30 10:43:11

标签: c# winforms

我需要像这张图片一样自定义我的组合框。

enter image description here
我的代码运行正常。但它有两个问题:
1.当我最小化窗口时,文本向左移动

enter image description here
2.当另一个窗口覆盖我的应用程序时,背景变为绿色。

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

那有什么不对?

2 个答案:

答案 0 :(得分:0)

您可以使用Graphics.MeasureString来获取要绘制的字符串的大小,然后决定从哪里开始绘制字符串:

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

答案 1 :(得分:0)

我发现问题是DropDown状态。我必须在设置Brush之前检查DroppedDown。

$encoded_content = chunk_split( base64_encode( $variables['pdfFileContents'] ));

$uid = md5( time() );

$fileName = str_replace( " ", "_", $variables['emailContains'] );
$fileName .= ".pdf";

////// attachment header ///////////////////////////////
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "From: cashxcash.com<{$sender}>";
$headers[] = "Content-Type: multipart/mixed; boundary=\"{$uid}\"";
$headers[] = "This is a multi-part message in MIME format.";
$headers[] = "--{$uid}";
$headers[] = "Content-type:text/plain; charset=iso-8859-1"; // Set message content type
$headers[] = "Content-Transfer-Encoding: 7bit";
$headers[] = ""; // Dump message
$headers[] = "--{$uid}";
$headers[] = "Content-Type: application/octet-stream; name=\"{$fileName}\""; // Set content type and file name
$headers[] = "Content-Transfer-Encoding: base64"; // Set file encoding base
$headers[] = "Content-Disposition: attachment; filename=\"{$fileName}\""; // Set file Disposition
$headers[] = $encoded_content; // Dump file
$headers[] = "--{$uid}--"; //End boundary

return mail(
    $reciever,
    "{$variables['emailContains']} Report from Cashxcash!",
    "", 
    implode("\r\n", $headers)
);