如何确定哪个图像加载在BitBtn中?

huangapple go评论62阅读模式
英文:

How to determine which image is loaded in a BitBtn?

问题

我想在Delphi中为表格添加一个排序按钮。我有一个带有图标(一个向上的箭头)的TBitBtn。当按下该按钮时,我希望发生类似以下的操作:

if btbSort.Glyph := 'Up.bmp' then
   从下到上排序
否则
   从上到下排序

这有意义吗?

似乎没有一个用于“加载的图标”的属性。

英文:

I want a sorting button for a table in Delphi. I have a TBitBtn with a Glyph (an up arrow). When that button is pressed, I want something like this to happen:

if btbSort.Glyph := 'Up.bmp' do
   Sort from down to up
or else
   Sort from up to down

Does this make sense?

There doesn't seem to be a property for the "loaded glyph".

答案1

得分: 2

没有办法询问按钮加载了哪个图标,因为它不会跟踪该信息。因此,你必须自己跟踪它,例如使用按钮的 Tag 属性,如下所示:

btbSort.Glyph.LoadFromFile('Up.bmp');
btbSort.Tag := 1;
...
if btbSort.Tag = 1 then begin
   // 从下到上排序...
end
else begin
   // 从上到下排序...
end;
英文:

There is no way to ask the Button which Glyph is loaded, because it does not keep track of that information. So you have to keep track of it yourself, for instance by using the button's Tag propety, eg:

btbSort.Glyph.LoadFromFile('Up.bmp');
btbSort.Tag := 1;
...
if btbSort.Tag = 1 then begin
   // Sort from down to up ...
end
else begin
   // Sort from up to down ...
end;

答案2

得分: 0

以下是翻译好的部分:

"将其视为只写属性,因为没有简单的读取方式。好消息是,字形通常非常小且加载速度很快。如果控件允许您使用ImageList,那么您只需要更改ImageIndex。

您需要通过更改事件来设置字形。首先以已知状态(例如“开”或“关”)启动它,并在每次状态更改时加载另一幅图像。您可能希望反映多个状态,而不仅仅是两个。

但是,您可以分配具有多个图像的字形,例如页面,最多可以有四个,我认为是:向上,鼠标按下,鼠标释放和向下(或类似的内容)。它会选择适当的字形显示。这可能在某个地方有解释,但我不确定在哪里。

这就像是一场舞蹈:您需要决定谁在主导,谁在跟随 - 是按钮的状态还是(外部)排序顺序标志的状态。"

英文:

Treat it like a write-only property as there's nothing simple to read. The Good News is the Glyphs are usually very small and load up quickly. If the control lets you use an ImageList, then all you need to do is change the ImageIndex.

You need the glyph to be set by an event that changes it. Start it off in a known state (eg., "on" or "off") with the correlated image, then load up the other image each time the state changes. You could have several states that you want to reflect, not just two.

However, you can assign glyphs with multiple images, like pages, up to four I think: up, mouse-down, mouse-up, and down (or something similar). It will pick the appropriate glyph to show. That's probably explained somewhere, but I'm not sure where.

It's like a dance: you need to decide who's leading and who's following -- the button's state or the (external) sort order flag's state.

答案3

得分: 0

一个 TBitBtn 具有一个 Glyph 属性,该属性是一个 TBitmap,但我不确定您是否可以轻松使用它来确定显示哪个箭头。

然而,借鉴自David Schwartz的答案,也许最好的方法是创建一个包含上下箭头的 TImageList,然后将按钮的 ImageIndex 设置为所需的箭头。当用户点击按钮时,查看当前设置的 ImageIndex 是哪个数字,然后进行相应的排序。

英文:

A TBitBtn does have a Glyph property which is a TBitmap, but I'm not sure you could readily use that to determine which arrow is showing.

However, taking a lead from David Schwartz' answer, perhaps the best way is to create a TImageList containing the up and down arrows, then set ImageIndex of the button to the arrow required. When the user clicks the button, see which number ImageIndex is currently set to, and sort accordingly.

huangapple
  • 本文由 发表于 2023年7月3日 17:36:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定