如果我制作了一个包含多个标签的表单(某些动态标签),我该如何为标签添加抗锯齿功能并删除/使表单背景透明。
另外,如何使表单背景不可见,只留下标签?
(我尝试了一些在代码中注释掉的东西,这有点有效)*
使用visual studio
目前情况如何:
我希望如何:
C#
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
Thread thread1;
public Form1()
{
InitializeComponent();
/*// Problem code:
this.Paint += new System.Windows.Forms.PaintEventHandler(this.SmoothingFonts_Paint);
//end*/
thread1 = new Thread(new ThreadStart(Thread_test));
thread1.IsBackground = true;
thread1.Start();
}
public void Thread_test()
{
int i = 0;
while (true)
{
i++;
label2.Text = Convert.ToString(i);
Thread.Sleep(1000);
}
}
/*//Problem code:
private void SmoothingFonts_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Font TextFont = new Font("Segoe UI", 48, FontStyle.Bold);
Color myColor = Color.FromArgb(30, 35, 42);
SolidBrush myBrush = new SolidBrush(myColor);
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
e.Graphics.DrawString("TestTEXT 2", TextFont, myBrush, 20, 20);
}
//end*/
}
}
答案 0 :(得分:1)
而不是Windows窗体我只是做了WPF,然后AA和透明度开箱即用(超级简单)。
WPF代码:
using System;
using System.Windows;
using System.Windows.Threading;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
DispatcherTimer timer = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
timer.Tick += new EventHandler(timer_tick);
timer.Interval = new TimeSpan(0, 0, 1);
}
int dynCount = 0;
private void timer_tick(object sender, EventArgs e)
{
dynCount++;
dynNum.Content = dynCount.ToString();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
timer.Start();
}
}
}
<强> Download "WPF" visual studio project here 强>
以下是Windows窗体中的一些其他测试:
<强> CODE:强>
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label[] _Labels = new Label[2];
String[] _LabelsText = new string[2];
Graphics g;
Bitmap btm;
Color myColor;
SolidBrush myBrush;
private void Form1_Load(object sender, EventArgs e)
{
_Labels[0] = label1;
_Labels[1] = label2;
_LabelsText[0] = "CPU";
_LabelsText[1] = "0";
label1.Text = "";
label2.Text = "";
draw();
this.BackgroundImage = btm;
}
public void draw()
{
btm = new Bitmap(this.Width, this.Height);
g = Graphics.FromImage(btm);
for (int i = 0; i < _Labels.Length; i++)
{
myColor = _Labels[i].ForeColor;
myBrush = new SolidBrush(myColor);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawString(_LabelsText[i], _Labels[i].Font, myBrush, _Labels[i].Location);
}
}
int dynNumCount = 0;
private void dynamicNumber_Tick(object sender, EventArgs e)
{
dynNumCount++;
_LabelsText[1] = Convert.ToString(dynNumCount);
draw();
this.BackgroundImage = btm;
}
}
}
<强> Download "AA only" visual studio project here 强>
(我在重绘之前清除了问题所以你需要解决这个问题。)
<强> CODE:强>
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport("User32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
public Form1()
{
InitializeComponent();
this.Opacity = 0;
}
Label[] _Labels = new Label[2];
String[] _LabelsText = new string[2];
Graphics g;
Color myColor;
SolidBrush myBrush;
private void Form1_Load(object sender, EventArgs e)
{
_Labels[0] = label1;
_Labels[1] = label2;
_LabelsText[0] = "CPU";
_LabelsText[1] = "0";
label1.Text = "";
label2.Text = "";
draw();
}
private void draw()
{
using (Graphics g = Graphics.FromHdc(GetDC(IntPtr.Zero)))
{
g.TextRenderingHint = TextRenderingHint.AntiAlias;
Font theFont = new Font(FontFamily.GenericSansSerif, 100.0F, FontStyle.Bold);
for (int i = 0; i < _Labels.Length; i++)
{
myColor = _Labels[i].ForeColor;
myBrush = new SolidBrush(myColor);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawString(_LabelsText[i], _Labels[i].Font, myBrush, _Labels[i].Location);
}
}
}
int dynNumCount = 0;
private void dynamicNumber_Tick(object sender, EventArgs e)
{
dynNumCount++;
_LabelsText[1] = Convert.ToString(dynNumCount);
draw();
}
}
}
<强> Download "Draw to desktop" visual studio project here 强>
答案 1 :(得分:0)
你试过这个吗?
this.BackColor = System.Drawing.Color.Black;
this.TransparencyKey= System.Drawing.Color.Black;
label1.BackColor = System.Drawing.Color.Transparent;
label1.ForeColor = Color.White;
前两行使表单透明
并且第二条线使标签透明,所以它就像玻璃一样,标签背面没有任何东西
希望对你有所帮助