【问题标题】:C# Button transparent background on MouseHover鼠标悬停时的 C# 按钮透明背景
【发布时间】:2013-11-07 18:56:59
【问题描述】:

我有这个按钮,用代码创建,它有一个背景图像并且是透明的。到目前为止一切顺利,它可以工作,可怕的灰色背景颜色消失了。 除了当我将鼠标悬停在它上面时,烦人的情绪会不断出现。

所以我尝试了我能想到的方法,那就是添加一个事件监听器,它将在 MouseHover、Enter 和 Leave 上将背景颜色更改为透明。但似乎没有任何效果。

有什么想法吗?

到目前为止,这是我的代码:

    private void CreateFirstScreen() {
        mainForm.BackgroundImage = Image.FromFile(@"[img dir]");

        Button nextBtn = new Button();
        nextBtn.BackColor = Color.Transparent;        <== this works!
        nextBtn.BackgroundImage = Image.FromFile(@"[img dir]");
        nextBtn.FlatStyle = FlatStyle.Flat;

        nextBtn.FlatAppearance.BorderSize = 0;
        nextBtn.SetBounds(1555, 820, 274, 106);

        mainForm.Controls.Add(nextBtn);


        nextBtn.MouseHover += (sender, args) => {
            nextBtn.BackColor = Color.Transparent;    <= doesn't work
        };
        nextBtn.MouseEnter += (sender, args) => {
            nextBtn.BackColor = Color.Transparent;    <= doesn't work
        };
        nextBtn.MouseLeave += (sender, args) => {
            nextBtn.BackColor = Color.Transparent;    <= doesn't work
        };
        nextBtn.Click += (sender, args) => {
            CreateSecondScreen();                     <= does work
            mainForm.Controls.Remove(nextBtn);        <= does work
        };
    }

【问题讨论】:

  • WinForms 不支持透明。

标签: c# button


【解决方案1】:

尝试添加这个:

nextBtn.FlatAppearance.MouseOverBackColor=Color.Transparent;

【讨论】:

  • 您先生,刚刚回答了我的问题,非常棒,正是我所需要的。感谢您的回复。
  • 干净整洁的解决方案。
猜你喜欢
  • 2018-04-07
  • 2013-03-22
  • 1970-01-01
  • 2017-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 2019-02-05
相关资源
最近更新 更多