简体   繁体   English

DrawString()问题和自定义控件的大小调整

[英]Issues with DrawString() and resizing of a custom control

I am working on some custom controls and I am looking for some feedback. 我正在开发一些自定义控件,并且正在寻找反馈。 One of the controls looks similar to a standard TextBox at design time, and when it is dragged and dropped onto a design surface the name of the control is written in the text area. 在设计时,其中一个控件看上去与标准TextBox相似,并且将其拖放到设计图面上时,控件的名称会写在文本区域中。 This all works fine, but I feel that there is an easier way to accomplish what I have done. 一切都很好,但是我觉得有一种更简单的方法可以完成我所做的事情。 I have the following code: 我有以下代码:

public override void Draw(Graphics graphics)
{
        graphics.FillRectangle(new SolidBrush(BackColor), ClientRectangle);
        graphics.DrawString(Text, Font, new SolidBrush(ForeColor), TextRectangle, format);
        DrawControl(graphics);
}

protected void DrawControl(Graphics graphics)
{
        CotrolPaint.DrawBorder3D(graphics, ClientRectangle, Border3DStyle.Etched);
}

protected Rectangle TextRectangle
{
        get
        {

            return new Rectangle(ClientRectangle.Left + 2,
                                        ClientRectangle.Top + 2,
                                        ClientRectangle.Width - 5,
                                        ClientRectangle.Height - 5);
        }
}

Looking over the code now, TextRectangle should probably be a readonly constant, but I digress. 现在看一下代码,TextRectangle可能应该是一个只读常量,但是我离题了。 The code is fairly straightforward, the bit I am hung up on is my TextRectangle property. 该代码非常简单,我最喜欢的是TextRectangle属性。 The reason I am using this is instead of ClientRectangle is because the text that is written inside the rectangle gets cut off by several pixels. 我之所以使用它而不是ClientRectangle是因为写在矩形内的文本被几个像素切掉。 Thus I have a smaller rectangle that is offset a bit. 因此,我有一个较小的矩形,它稍微偏移了一点。 This works, but it feels like a "hack". 这行得通,但感觉像是“黑客”。 I have to believe that there is a better way to do this than what I have demonstrated here. 我必须相信,有一种比我在这里展示的方法更好的方法。

The next issue I have is difficult to explain, so I have provided a couple images. 下一个问题很难解释,因此提供了一些图像。 If I drag my custom control from the toolbox onto the design surface, it looks similar to this: 如果我将自定义控件从工具箱拖动到设计图面上,则其外观类似于以下内容:

设计1

However if I manually resize the control, the selection rectangle retains its previous size: 但是,如果我手动调整控件的大小,则选择矩形将保留其先前的大小:

设计2

I am somewhat at a loss on how to fix this. 我对如何解决这个问题有些困惑。 Any help on either question would be greatly appreciated! 任何一个问题的帮助将不胜感激!

What you do for the first issue is not really some kind of hack, in fact many default controls are drawn with some hard-coded padding (commonly used are 1 and 2 ...). 您对第一个问题所做的实际上并不是某种破解,实际上,许多默认控件是使用一些硬编码的填充绘制的(通常使用12 ...)。 Normally the maximum Rectangle for the text bounds is ClientRectangle , if you want some padding, of course you can just adjust it (like as you did). 通常,文本边界的最大Rectangle是ClientRectangle ,如果您想要一些填充,您当然可以对其进行调整(就像您所做的那样)。

For the second issue, it's because you don't have any code to invalidate the control after its size is changed, even you have to invalidate when the text , font and forecolor are changed. 对于第二个问题,这是因为在更改控件的大小后没有任何代码可以使控件无效,即使在更改text,font和forecolor时也必须使该控件无效。 Try adding the following code to your code and it should work: 尝试将以下代码添加到您的代码中,它应该可以工作:

protected override void OnSizeChanged(EventArgs e){
  base.OnSizeChanged(e);
  Invalidate();
}
protected override void OnTextChanged(EventArgs e){
  base.OnTextChanged(e);
  Invalidate();
}
protected override void OnFontChanged(EventArgs e){
  base.OnFontChanged(e);
  Invalidate();
}
protected override void OnForeColorChanged(EventArgs e){
  base.OnForeColorChanged(e);
  Invalidate();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM