在 VBA 按钮中将文本旋转 90 度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27390420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rotate text 90 degrees in a VBA button
提问by DarcyG
采纳答案by Michael
You cannot do this directly in VBA, but you can still accomplish a similar visual effect with the buttons (sorry, the textbox is impossible) by using a 3rd party BMP editor.
您不能直接在 VBA 中执行此操作,但您仍然可以通过使用 3rd 方 BMP 编辑器通过按钮实现类似的视觉效果(对不起,文本框是不可能的)。
- Create a rotated image with the given text that you want to have. Make sure to use the same font family, style, & size (Tahoma, regular, 8pt by default) and set the background color of the image to the system palette background color, which is RGB(240,240,240) or hex #F0F0F0. Also make sure the height and width of the image is roughly the same size as the button you want to rotate.
- Save the rotated image as a bitmap (.bmp) file.
- In the Propertiesbox of the CommandButton1, scroll down to the Pictureproperty, click the ellipses on the right, navigate to and select the rotated image.
- 使用您想要的给定文本创建旋转图像。确保使用相同的字体系列、样式和大小(Tahoma、regular、默认为 8pt)并将图像的背景颜色设置为系统调色板背景颜色,即 RGB(240,240,240) 或十六进制 #F0F0F0。还要确保图像的高度和宽度与您要旋转的按钮的大小大致相同。
- 将旋转后的图像另存为位图 (.bmp) 文件。
- 在CommandButton1的属性框中,向下滚动到图片属性,单击右侧的省略号,导航到并选择旋转的图像。
Final Result
最后结果
Edit 1: Another Option
编辑 1:另一种选择
This next part is mainly a proof-of-concept and is by no means a complete solution. You will need to find the correct customized .ttf or .otf font file and then install it on all the systems you want to use the rotated text on.
下一部分主要是概念验证,绝不是完整的解决方案。您将需要找到正确的自定义 .ttf 或 .otf 字体文件,然后将其安装在您想要使用旋转文本的所有系统上。
Depending on how complicated you want to get, you can actually create a monospaced font where each character is rotated 90° and then use VBA code to insert/delete characters at the proper location.
根据您想要获得的复杂程度,您实际上可以创建一个等宽字体,其中每个字符旋转 90°,然后使用 VBA 代码在适当的位置插入/删除字符。
I used Vertigofrom http://www.1001fonts.com/vertigo-font.htmlwhich was the only TrueType font I could find without either putting in effort to create my own or paying for it. The one big problem I see with this font is that it is not monospaced and that the height of each character (say for example, underscore _) is not consistent, so you cannot properly add spaces or underscores (a major downside I realize, so you will need to find/create your own font).
我使用了http://www.1001fonts.com/vertigo-font.html中的Vertigo,这是我能找到的唯一一种 TrueType 字体,无需努力创建自己的字体或付费。我看到这种字体的一个大问题是它不是等宽的,并且每个字符的高度(例如下划线 _)不一致,因此您无法正确添加空格或下划线(我意识到这是一个主要缺点,所以您需要找到/创建自己的字体)。
To install the font on a Windows system:
在 Windows 系统上安装字体:
- Simply unzip the file anywhere you'd like
- Double click the file you want; you will see the waterfall layout of the font family to ensure you're installing the correct font file
- Click the Install button at the top
- Restart your computer
- 只需将文件解压缩到您想要的任何位置
- 双击你想要的文件;您将看到字体系列的瀑布式布局,以确保您安装了正确的字体文件
- 单击顶部的安装按钮
- 重启你的电脑
Then, on the TextBox1's property menu, scroll to Font, click the ...and select Vertigoas the font family. Also, set MultiLine to true. You will need to make sure the width of the textbox is small enough that only 1 character can fit on a line and tall enough to fit your entire word.
然后,在 TextBox1 的属性菜单上,滚动到字体,单击...并选择Vertigo作为字体系列。此外,将 MultiLine 设置为 true。您需要确保文本框的宽度足够小,一行中只能容纳 1 个字符,并且高度足以容纳整个单词。
The result looks something like this:
结果如下所示:
I realized with this font that the letters were coming in backwards, so I had to add a little basic code on the KeyDown
and KeyPress
events to properly handle basic typing (ignores where the blinker is at and only deals with the most recent character at the end)
我意识到使用这种字体时字母是倒过来的,所以我不得不在KeyDown
和KeyPress
事件上添加一些基本代码以正确处理基本输入(忽略闪光灯所在的位置,只处理最后的最新字符)
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Me.TextBox1.Value = Chr(KeyAscii) & Me.TextBox1.Value
KeyAscii = False
End Sub
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 8 Then
Me.TextBox1.Value = Mid(Me.TextBox1.Value, 2, Len(Me.TextBox1.Value))
KeyCode = False
End If
End Sub