Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delphi DrawGrid.Canvas.TextRect changes & to _

I have a TDrawGrid where I am using its DrawCell event and in here the Canvas.TextRect to fill out the cells with strings. One of the strings contains an & but it gets displayed as an underscore _ .

I don’t see anything wrong, there is nothing fancy with this grid and elsewhere it seems to work fine. Also the debugger confirms that the string is correct when passing it to Canvas.TextRect

What am I missing?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Delphi 11, 64-bit and Windows 11.

>Solution :

This is expected. In Microsoft Windows, menu items, buttons, and control labels use an underscore to indicate the corresponding keyboard shortcut, and this underlined character is indicated using a prefix ampersand in code.

For instance, the &File menu item (displayed as File with F underlined) can be accessed by pressing Alt+F. A &Save button (displayed as Save with S underlined) may be invoked by pressing Alt+S (or only S if the currently focused control doesn’t accept character input). You can set focus to a text field with label &Name: (displayed as Name: with N underlined) by pressing Alt+N.

This is why a string like Lost & Found is displayed as Lost _Found in Windows.

If you don’t want ampersands to be treated as accelerator character indicators, simply use the tfNoPrefix flag:

Canvas.TextRect(R, S, [tfNoPrefix])

This VCL flag corresponds to the Win32 API DT_NOPREFIX flag:

Turns off processing of prefix characters. Normally, DrawText interprets the mnemonic-prefix character & as a directive to underscore the character that follows, and the mnemonic-prefix characters && as a directive to print a single &. By specifying DT_NOPREFIX, this processing is turned off.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading