I want to use an image as a button that has no caption, no background and no border with a tabstop.
I have tried the speedbutton with the flat property set which is perfect except it has no tabstop.
I have tried the bitbtn which also accepts an image and has a tabstop but has no way to remove the button border. I have unchecked seBorder in StyleElements which does nothing and there is no flat option so cannot remove the border.
We all have personal styles and preferences, but when it comes to UI design, it is important to stick with the platform standard. And note that your UI may become an unusable headache in the future. Things like the introduction of the Aero interface and the use of high-density monitors can easily mess up your personalized UI.
With that said,
TSpeedButtonandTBitBtnare two options to implement what you want. By design, Delphi has two types ofTControls. One isTGraphicControl, likeTSpeedButton, which cannot receive the focus. The other isTWinControl, likeTBitBtn, which can be focused. So, one way to do it is to place aTSpeedButtonon a focusable container, make the container transparent by overridingCreateParam, and handle its keypress/keydown events. It is not easy though. Alternatively, you can sub-classTBitBtnand override its drawing, by handling theCN_DRAWITEMmessage.Below is a minimal working example, tested on Delphi 2009 and 10.4. Since you want only the image, I skipped and didn't deal with theme. I also didn't handle button down, enabled, and hovering. Read the source code (
Buttons.pas) for how to deal with them.Add a
TButtonand aTbitBtnto a new form and try this codeEdit: 26 Mar 2023: TBitBtn's Glyph is drawn with transparent background, although its Transparent property is false. A line was added above to make the drawing transparent, like TBitBtn does.