Winform复选框按钮图像对齐

时间:2015-06-09 13:15:10

标签: c# winforms

我试图在C#Winform应用程序中创建一个切换按钮。现在,我已经设法通过我之前的帖子提出了切换按钮的外观和感觉。

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

Toggle state 1 Toggle state 2

如何摆脱这种白色边缘?

到目前为止所做的事情:

  1. Flat的FlatAppearance设置为Flat。
  2. 尝试使用透明的背景颜色但是没有用。
  3. ImageAlign设置为MiddleCenter。
  4. 由winfrom设计师生成的代码

      // 
      // 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);
    

    更新我通过从图片大小减少按钮大小1px(x,y)来设法摆脱这些白边。图像大小为:99x63,按钮大小为98x62。但我不确定这是否是正确的方法。

1 个答案:

答案 0 :(得分:1)

这很简单。选择那些设置:

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时,checkboxbutton会保留8个水平像素,左边6个,右边2个,除非你像这样放大,否则会从图像中剪切8个像素:

checkBox1.Size = new Size(imageList1.ImageSize.Width + 8, imageList1.ImageSize.Height);

现在显示所有像素,但是在向左移动6个像素之前,控件不会明显左对齐!

看看你的例子,这两个问题可能并不重要,但有时它们是......

相关问题