简体   繁体   中英

Underlining first letter of Label C++ (Underline doesn't show)

Problem: I wish to underline the first letter of certain static text controls (such as Login and Password). The letters become underlined when the ALT key is pressed.

What I have tried: In C#, I was able to acheive this by using an ampersand. Such as "&Log in" or "&Password". I am trying to find a similar method in C++. The below picture shows an example in C#:

例

I am using MFC/C++ in Visual Studio 2010.

Edit: Added information about the ALT key. Here is an example of what I am trying in Visual Studio 2010's properties box. I am adding an ampersand to the front of the "Caption"'s text.

例子2

When I run my program in the debugger, the first letter is not underlined (until ALT is pressed): 在此处输入图片说明

There's a fundamental difference between a menu and a static control.

To do this in a menu, you do it just like in C#. Here's a screen shot of editing a menu in a C++ project:

在此处输入图片说明

...and here's the result:

在此处输入图片说明

For a static control, you have to clear the SS_NOPREFIX style for the control to get the same behavior. However, it's been my observation that under some circumstances the underline doesn't show (but I haven't ever pinned down the precise circumstances under which the underline didn't show--I think when it happened, I fixed it by changing the font, but I don't remember for sure).

After help from the SO community, it seems that using the ampersand (&) symbol before the desired underlined letter is the correct way. There was a setting on my personal machine that would keep the underlined letters hidden until the ALT key was pressed.

According to the MSDN:

A user often has to press ALT in order to see access key designations. To ensure that you address them throughout the development process, set your computer to persistently display access keys. 在此处输入图片说明

In Windows 8: Open Control Panel -> Ease of Access Center -> Make the Keyboard easier to use.

At the bottom of the screen, check "Underline keyboard shortcuts and access keys".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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