【发布时间】:2013-07-29 20:38:20
【问题描述】:
我只使用布局面板、按钮和标签在 winforms 中构建了一个小型井字游戏。每个游戏有 2 个玩家,每个玩家都与一个标记和颜色相关联。当玩家声称某个区域(单击网格上的按钮)时,该按钮的 BackColor 会更改为该玩家的颜色。
我现在想做的是让网格中的开放字段成为玩家颜色的半透明阴影,而光标在字段上。
由于某种原因,这不适用于我的按钮:
Public Class FieldButton
Inherits Button
' ... Omitting for brevity '
Private _mouseIn As Boolean
Protected Overrides Sub OnMouseEnter(e As EventArgs)
MyBase.OnMouseEnter(e)
_mouseIn = True
End Sub
Protected Overrides Sub OnMouseLeave(e As EventArgs)
MyBase.OnMouseLeave(e)
_mouseIn = False
End Sub
Public Overrides Property BackColor As Color
Get
If Field.HasOwner Then
Return Field.Owner.Color
ElseIf _mouseIn Then
Return Color.FromArgb(16, Presenter.Game.CurrentPlayer.Color)
End If
Return MyBase.BackColor
End Get
Set(value As Color)
MyBase.BackColor = value
End Set
End Property
Private Shared ReadOnly FullPen As New Pen(Brushes.Black, 3)
Private Shared ReadOnly SemiTransparentPen As New Pen(Color.FromArgb(64, Color.Black), 3)
Protected Overrides Sub OnPaint(pevent As PaintEventArgs)
MyBase.OnPaint(pevent)
If Field.HasOwner Then
PaintMark(pevent.Graphics, pevent.ClipRectangle, Field.Owner.Mark, FullPen)
ElseIf _mouseIn And Not Presenter.Game.IsGameOver Then
PaintMark(pevent.Graphics, pevent.ClipRectangle, Presenter.Game.CurrentPlayer.Mark, SemiTransparentPen)
End If
End Sub
' ... '
End Class
在上面的代码中,Field 是另一个对象,表示网格中的一个字段。每个字段都有一个Owner,设置为声明该字段的玩家(或为空)。
不管怎样,应该是行魔术的那一行:
Return Color.FromArgb(16, Presenter.Game.CurrentPlayer.Color)
有以下结果:
由于半透明标记造成的错觉,可能有点难以看清,但按钮背景颜色 FromArgb(16, ...) 与具有 Alpha 通道 255 的按钮完全相同相同。
我做错了什么?
编辑
当FlatStyle = FlatStyle.Flat 时,按钮的FlatButtonAppearance.MouseOverBackColor 属性优先于按钮的BackColor。
我认为这不能解释为什么我的按钮在鼠标悬停时仍显示紫色。我猜MouseOverBackColor 默认为当前背景色,但忽略了 alpha 通道。
【问题讨论】:
-
这看起来像是某种形式的 .net,来自内存 .net 曾经在表单对象上具有 opacity 属性。尝试寻找并改变它。
-
对不起...什么?哦。不,
Button没有 opacity 属性。 -
这是什么语言?
-
VB.NET,但对于 C# 应该是一样的。
-
代码标记似乎在起作用...