c#:在自定义控件中旋转文本
我正在尝试旋转标签内的一些文本。 我有一个自定义标签,它允许我控制文本渲染过程。
protected override void OnPaint ( PaintEventArgs pe )
{
Graphics g = pe.Graphics;
g.RotateTransform( angle );
g.drawString( text );
g.ResetTransform();
}
我遇到的问题是旋转似乎发生在控件的原点周围,即坐标 (0,0)。 有没有一种方法可以让文本围绕控件的中心而不是原点旋转?
我知道函数“g.RotateTransform( )”,因此一种可能的解决方案是旋转文本,然后将其转换到控件的中心。 如果这是管理工作的唯一方法,是否有一种通用方式可以计算转换以确保文本位于控件的中心?
谢谢
I am attempting to rotate some text within a label. I have a cusom label which allows me to have control over the text rendering process.
protected override void OnPaint ( PaintEventArgs pe )
{
Graphics g = pe.Graphics;
g.RotateTransform( angle );
g.drawString( text );
g.ResetTransform();
}
The problem I am having is that the rotation appears to occur aroundthe origin of the control i.e. co-ordinates (0,0). Is there a method to allow the text to rotate about the center of the control rather than the oragin?
I am aware of the function 'g.RotateTransform( )' so one possible solution would be to rotate the text and then translate it to the centre of the control. If this is the only way to manage the job, is there a generic manner which I can caluclate the transfor to ensure that the text is in the centre of the control?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
围绕任意点的旋转通常是将该点平移到原点,然后再旋转并平移回来。 问题可能是确定旋转后文本的尺寸以相应地将其移回原处。
A rotation around an arbitrary point is usually a translation of that point to the origin, then a rotation and a translation back again. The problem would probably be to determine the dimensions of the text after rotation to move it back accordingly.