Reputation: 1291
If I have made a form with multiple labels (some dynamic labels) how can I add Anti aliasing to the labels and remove/ make the form background transparent.
Also how do I make the form background invisible so its just the labels left?
(I experimented with some stuff that are commented out in the code, that's somewhat works)*
Using visual studio
Currently how it is:
How I want it to be:
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*/
}
}
Download Visual Studio Project Here
Upvotes: 0
Views: 1804
Reputation: 1291
Instead of windows form I just did WPF, then AA and transparency works out of the box (super easy).
WPF CODE:
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
Here are a few other tests in windows form:
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
(I had problem clearing before redrawing so you would need to solve that).
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
Upvotes: 1
Reputation: 96
Did You try this?
this.BackColor = System.Drawing.Color.Black;
this.TransparencyKey= System.Drawing.Color.Black;
label1.BackColor = System.Drawing.Color.Transparent;
label1.ForeColor = Color.White;
first two line make form transparent
and the second two line make the label transparent, so it's like glass and there is nothing in back ground of your label
hope it help you
Upvotes: 0