C# Winform复选框按钮图像对齐

C# Winform复选框按钮图像对齐,c#,winforms,C#,Winforms,我试图在C#Winform应用程序中创建一个切换按钮。现在,我已经设法使切换按钮的外观和感觉与建议,从我以前的职位 现在的问题是,我无法在按钮上正确地将ImageList中的图像居中对齐,因此它在边缘上显示了一些背景颜色。请参阅下面的图片以获得清晰的视图 我怎样才能去掉这些白边 到目前为止已经尝试过的事情: 扁平按钮的外观设置为扁平 尝试使用透明背景色,但不起作用 ImageAlign设置为中间居中 winfrom designer生成的代码 // // checkBox1 /

我试图在C#Winform应用程序中创建一个切换按钮。现在,我已经设法使切换按钮的外观和感觉与建议,从我以前的职位

现在的问题是,我无法在按钮上正确地将ImageList中的图像居中对齐,因此它在边缘上显示了一些背景颜色。请参阅下面的图片以获得清晰的视图

我怎样才能去掉这些白边

到目前为止已经尝试过的事情:

  • 扁平按钮的外观设置为扁平
  • 尝试使用透明背景色,但不起作用
  • ImageAlign设置为中间居中 winfrom designer生成的代码

      // 
      // checkBox1
      // 
      this.checkBox1.Appearance = System.Windows.Forms.Appearance.Button;
      this.checkBox1.BackColor = System.Drawing.Color.White;
      this.checkBox1.CausesValidation = false;
      this.checkBox1.CheckAlign = System.Drawing.ContentAlignment.BottomLeft;
      this.checkBox1.Cursor = System.Windows.Forms.Cursors.Hand;
      this.checkBox1.FlatAppearance.BorderSize = 0;
      this.checkBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
      this.checkBox1.ForeColor = System.Drawing.Color.White;
      this.checkBox1.ImageIndex = 0;
      this.checkBox1.ImageList = this.imageList1;
      this.checkBox1.Location = new System.Drawing.Point(88, 178);
      this.checkBox1.Margin = new System.Windows.Forms.Padding(0);
      this.checkBox1.Name = "checkBox1";
      this.checkBox1.Size = new System.Drawing.Size(98, 62);
      this.checkBox1.TabIndex = 0;
      this.checkBox1.Text = "Sample Button";
      this.checkBox1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
      this.checkBox1.UseVisualStyleBackColor = true;
      this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged_1);
    

    更新:I'v通过将按钮大小从图像大小减少1px(x,y)来去除这些白色边缘。图像大小为:99x63,按钮大小为98x62。但我不确定这样做是否正确

    这很简单。选择以下设置:

    checkBox1.FlatStyle = FlatStyle.Flat;
    checkBox1.FlatAppearance.BorderSize = 0;
    
    // make all four (!) BackColors transparent!
    checkBox1.BackColor = System.Drawing.Color.Transparent;
    checkBox1.FlatAppearance.CheckedBackColor = Color.Transparent;
    checkBox1.FlatAppearance.MouseDownBackColor = Color.Transparent;
    
    请注意,使用FlatStyle.Flat时,复选框按钮保留8个水平像素,6个位于左侧,2个位于右侧边缘,并且将从图像中删除8个像素,除非您将其放大:

    checkBox1.Size = new Size(imageList1.ImageSize.Width + 8, imageList1.ImageSize.Height);
    
    现在显示所有像素,但是控件在向左移动6个像素之前不会明显左对齐


    看看你的例子,这两个问题可能都不重要,但有时它们很重要。

    winform designer为示例按钮生成了什么代码?@ASh:更新了我的帖子。我认为你需要将所有四种背景颜色都更改为透明。请参阅我对正确大小和对齐方式的评论!