如何更改 Rectangle 的颜色并在叠加层中更新它?我目前使用这种颜色更改方法:
static public Color w = Color.FromArgb(255, 35, 35, 35);
static public Color a = Color.FromArgb(255, 35, 35, 35);
static public Color s = Color.FromArgb(255, 35, 35, 35);
static public Color d = Color.FromArgb(255, 35, 35, 35);
static public void Draw(System.Drawing.Graphics g, int size, int x, int y)
{
// Background
Brush bBrushW = new SolidBrush(w);
Brush bBrushA = new SolidBrush(a);
Brush bBrushS = new SolidBrush(s);
Brush bBrushD = new SolidBrush(d);
...
但是在更改颜色时,对象会保留其旧颜色,我在计时器中以类似的方式更改颜色:
private void Letters_Tick(object sender, EventArgs e)
{
// Keystokes W
if (Input.isPressed(Keys.W) != 0)
{
Keystokes.w = Color.FromArgb(255, 255, 0, 0);
} else
{
Keystokes.w = Color.FromArgb(255, 35, 35, 35);
}
...
该函数Draw仅在 Paint 表单中调用一次:
private void Overlay_Paint(object sender, PaintEventArgs e)
{
g = e.Graphics;
Keystokes.Draw(g, 50, Width - 350, 250);
}
如何使它以最小的负载更新颜色?
每次需要更新颜色时:
或者如果您知道要更新的区域的坐标更好:
更好的是,扔掉计时器并留意键盘:
代替“this.Text = ... vkCode; //this == form1” - 替换您需要的键上的 if-else,或者更确切地说是 func(int vkCode) 函数,在该函数中使用调用 Invalidate()
示例取自此处:https ://www.cyberforum.ru/post9660429.html