当我调整代码垃圾中的标签大小时,新的空白空间上会短暂显示
我正在调整大小并移动控件。
该代码按我想要的方式工作,只是在正确的显示出现之前,我在新空间中短暂显示了一个黑色矩形。
当另一个控件改变其高度时,控件就会移动。
我在从 Label 派生的类中对 OnPaint 进行了更改,如果我首先或最后调用基本 OnPaint 似乎并不重要。
public ref class ResizeLabel : public System::Windows::Forms::Label
{
private void resizelabel()
{
int width = ... ;
int height = ... ;
__super::Width = width;
__super::Height = height;
this->LinkedControl->Top = __super::Top + __super::Height;
}
protected: virtual void OnPaint(System::Windows::Forms::PaintEventArgs^ e) override {
resizeLabel();
__super::OnPaint(e);
}
};
我可能做错了什么,但我不知道是什么。
有人可以帮忙吗?
I'm resizing and moving controls.
The code is working as I want, except that I'm getting a black rectangle displayed briefly in the new space before the correct display appears.
The moving of a control is done when another control changes its height.
I make the changes from OnPaint in a class derived from Label and it doesn't seem to matter if I call the base OnPaint first or last.
public ref class ResizeLabel : public System::Windows::Forms::Label
{
private void resizelabel()
{
int width = ... ;
int height = ... ;
__super::Width = width;
__super::Height = height;
this->LinkedControl->Top = __super::Top + __super::Height;
}
protected: virtual void OnPaint(System::Windows::Forms::PaintEventArgs^ e) override {
resizeLabel();
__super::OnPaint(e);
}
};
I'm probably doing something wrong, but I don't know what.
Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在做的一件逻辑上错误的事情是在绘制事件中调整大小。这没有逻辑意义。在绘制事件内部,要重新绘制的控件部分已经确定(并且确实在事件参数中提供)。调整大小不会改变这一点。它只是将控件的其他部分标记为无效(如果大小增加),因此将来会发生更多绘制事件。
在实际导致调整大小发生的事件或方法内调整大小(无论是什么),而不是在导致绘制控件的事件内。例如,如果需要在标签文本更改时调整大小,请将调整大小代码放入由文本更改触发的方法中。 “我的文字已经改变,我需要为自己制定一个新的尺寸,我的新的未上漆的部分,这将在闲暇时以通常的方式重新上漆。”是这种控制的逻辑。对于调整大小的其他原因也是如此。
One logically wrong thing that you're doing is resizing from within a paint event. That makes no logical sense. Inside a paint event, the portion of the control to be repainted has already been determined (and is indeed supplied in the event arguments). Resizing doesn't change that. It simply marks other parts of the control as invalid (if the size increases), and thus due for more paint events in the future.
Resize inside the event or method that actually causes the resize to happen, whatever that may be, not inside the event that causes the control to be painted. If resizing needs to happen whenever the text of the label changes, for example, put the resizing code inside the method that is triggered by the text changing. "My text has changed, I need to work out a new size for myself, the new unpainted parts of me that that creates to be repainted at leisure in the usual fashion." is the logic for such a control. The same goes for other causes for resizing.