How to extend winforms tooltip text width size? - c#

private void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (e.ToolTipSize.Width > 10)
{
Size s = TextRenderer.MeasureText(toolTip1.GetToolTip((sender as System.Windows.Forms.ToolTip).Tag as Control), SystemFonts.SmallCaptionFont);
e.ToolTipSize = new Size(4560, s.Height);
}
}
I tried 4560 just for testing because with 456 width it didn't change anything i still need many time to add \n or to add environment.newline.
I changed the ownerdraw to true.
I also use the draw event :
private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
{
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None;
sf.FormatFlags = StringFormatFlags.NoWrap;
using (Font f = new Font("Tahoma", 10))
{
e.Graphics.Clear(BackColor);
e.Graphics.DrawString(e.ToolTipText, f,
SystemBrushes.ActiveCaptionText, e.Bounds, sf);
}
}
}

Related

How do I keep animations from flickering in C# game programming?

Everytime I try to add a new line or move the paddle in this game the screen flickers.
How do I keep the screen from flickering when I move the paddle or add a line?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class GameTest : Form
{
int dx=3, dy=3, i =500, o = 100;
int rex = 400, rey = 450 ;
double c =0;
public GameTest()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Invalidate();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Graphics g = this.CreateGraphics();
if (e.KeyCode == Keys.Left)
{
//Graphics g = this.CreateGraphics();
Invalidate();
Brush black = new SolidBrush(Color.White);
g.FillRectangle(black, rex, rey, 200, 20);
rex -= 40;
Brush red = new SolidBrush(Color.Green);
g.FillRectangle(red, rex, rey, 200, 20);
}
if (e.KeyCode == Keys.Right)
{
//Graphics g = this.CreateGraphics();
Brush white = new SolidBrush(Color.White);
g.FillRectangle(white, rex, rey, 200, 20);
rex += 40;
Brush red = new SolidBrush(Color.Green);
g.FillRectangle(red, rex, rey, 200, 20);
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
DoubleBuffered = false;
Graphics g = this.CreateGraphics();
// Bitmap bmp1 = new Bitmap("G:\c#\Bouncing ball\Pic.jpg");
//TextureBrush tb2 = new TextureBrush(bmp1);
Brush green = new SolidBrush(Color.Green);
g.FillRectangle(green, rex, rey, 200, 20);
Graphics b= this.CreateGraphics();
Brush red = new SolidBrush(Color.Red);
b.FillEllipse(red, i, o, 20, 20);
Pen p = new Pen(Color.Black, 10);
//
g.DrawLine(p, 1000, 480,0,480);
g.DrawLine(p, 1000, 485, 1000, 0);
g .DrawLine(p, 0,480, 0, 0);
}
private void timer1_Tick(object sender, EventArgs e)
{
DoubleBuffered = false;
// int dx=3, dy=3, i = 300, o = 50;
i += dx;
if (i < 0)
{
dx = -dx;
}
else if (i + 50 > 1000)
{
dx = -dx;
}
o += dy;
if ((o +20>= rey) &&(i+20<=rex+200)&&(i+20>=rex))
{
//int rex = 400, rey = 450; RECTANGLE
// int dx=3, dy=3, i(x) = 500, o(y) = 100;
dy =-dy;
//c++;
//label1.Text = c.ToString();
}
// Misgeret\\
if (o < 0)
{
dy = -dy;
}
// Misgeret\\
else if (o + 50 > 600)
{
dy = -dy;
}
this.Invalidate();
}
private void label1_Click(object sender, EventArgs e)
{
label1.Text = c.ToString();
}
}
}
I noticed that you are setting DoubleBuffered to false in multiple areas of your program. That is definitely not helping since the whole reason to use double buffering is to prevent flickering.
The other thing is you are creating Graphics contexts and drawing in multiple places in your application. Try to refactor your code to only draw in the OnPaint() event handler of the form, and do not create a new Graphics context. Use the one provided in the PaintEventArgs of the OnPaint() event.
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
using(Brush green = new SolidBrush(Color.Green))
{
g.FillRectangle(green, rex, rey, 200, 20);
}
// .. etc, etc..
}
Then, when you need to the form to be repainted you just need to invoke the Invalidate() method to tell the GDI that your form, or a portion of it, needs to be repainted. That will in turn cause the Paint event to be fired and the OnPaint() to be called.
As a side note, as #HighCore suggested in the comments, WinForms is definitely not the right framework to create a game in. For games try the XNA framework, or one of several open source ones available on the web such as Unity
UPDATE
To prevent flickering you can use automatic double buffering for your form. This can be enabled in the form constructor, after the call to InitializeComponent(), using a call to SetStyle:
SetStyle( ControlStyles.AllPaintingInWmPaint
| ControlStyles.UserPaint
| ControlStyles.DoubleBuffer , true);

How can put a Label on another Label

How can i put a label on another label and the text of them shows simultaneously?
When ever i do this work ,one of the labels goes top of another and the text of upper label shows.
Even i select BackColor = Transparent but it doesn't worked.
See Image below.
These are two labels and label1 goes under label2 and the text of label1 is missing.
And i want to have this result :
Just imagine i have two labels.one of them with 24pt font size,and another one is 8pt.
When i use larger Font ,the label has larger Frame than the other label.and i can't make them closer.
You can try this way:
label.Parent = parent with background
label.BackColor = Color.Transparent
label.Location = location you want offset with parent
Here is the converted code in C#:
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
public class TransparentLabel {
public TransparentLabel() {
// This call is required by the designer.
InitializeComponent();
// Add any initialization after the InitializeComponent() call.
// Add any initialization after the InitializeComponent() call.
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.components = new System.ComponentModel.Container();
RF = new RectangleF(0, 0, base.Width, base.Height);
LabelForeColorBrush = new SolidBrush(base.ForeColor);
}
private StringFormat sFormat;
private RectangleF RF;
private SolidBrush LabelForeColorBrush = null;
private void UpdateText() {
try {
sFormat = new StringFormat();
int x = 0;
int y = 0;
// With...
switch (TextAlignment) {
case ContentAlignment.BottomCenter:
sFormat.Alignment = StringAlignment.Center;
sFormat.LineAlignment = StringAlignment.Far;
break;
case ContentAlignment.BottomLeft:
sFormat.Alignment = StringAlignment.Near;
sFormat.LineAlignment = StringAlignment.Far;
break;
case ContentAlignment.BottomRight:
sFormat.Alignment = StringAlignment.Far;
sFormat.LineAlignment = StringAlignment.Far;
break;
case ContentAlignment.MiddleLeft:
sFormat.Alignment = StringAlignment.Near;
sFormat.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.MiddleCenter:
sFormat.Alignment = StringAlignment.Center;
sFormat.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.MiddleRight:
sFormat.Alignment = StringAlignment.Far;
sFormat.LineAlignment = StringAlignment.Center;
break;
case ContentAlignment.TopCenter:
sFormat.Alignment = StringAlignment.Center;
sFormat.LineAlignment = StringAlignment.Near;
break;
case ContentAlignment.TopLeft:
sFormat.Alignment = StringAlignment.Near;
sFormat.LineAlignment = StringAlignment.Near;
break;
case ContentAlignment.TopRight:
sFormat.Alignment = StringAlignment.Far;
sFormat.LineAlignment = StringAlignment.Near;
break;
}
sFormat.FormatFlags = StringDirection;
ResizeControl();
}
catch (Exception ex) {
}
}
private void ResizeControl() {
RF.Size = new Size(base.Size);
Invalidate();
}
private StringFormatFlags _StringDirection = (StringFormatFlags.NoClip < Description("The Direction of the Text."));
public StringFormatFlags StringDirection {
get {
return _StringDirection;
}
set {
_StringDirection = value;
UpdateText;
}
}
private System.Drawing.ContentAlignment _TextAlignment = (ContentAlignment.MiddleCenter < Description("The Text Alignment that will appear on this control."));
public System.Drawing.ContentAlignment TextAlignment {
get {
return _TextAlignment;
}
set {
_TextAlignment = value;
UpdateText();
}
}
public override System.Drawing.Color ForeColor {
get {
return base.ForeColor;
}
set {
base.ForeColor = value;
LabelForeColorBrush = new SolidBrush(value);
}
}
private string _Labeltext = ("TransparentLabel" < Description("The text to be displayed in supports with real transparency."));
public string LabelText {
get {
return _Labeltext;
}
set {
_Labeltext = value;
Invalidate();
}
}
[Browsable(false)]
[EditorBrowsable(false)]
public override System.Drawing.Color BackColor {
get {
return base.BackColor;
}
set {
base.BackColor = value;
}
}
protected override System.Windows.Forms.CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle = (cp.ExStyle | 32);
return cp;
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
try {
base.OnPaint(e);
// draw the text on the control
e.Graphics.DrawString(LabelText, base.Font, LabelForeColorBrush, RF, sFormat);
// MyBase.OnPaint(e)
}
catch (Exception ex) {
}
}
private void TransparentLabel_Resize(object sender, System.EventArgs e) {
ResizeControl();
}
}
Winforms labels don't support transparency. You need to create your own label if you want to make that happen.
I don't know if I got your question right. But isn't label drop shadowing what you want?
If that is the case then you should check out this site: Label drop shadow
Small sample of usage:
using System;
using System.Drawing;
using System.Windows.Forms;
class WinFormsDropShadow: Form
{
public static void Main()
{
Application.Run(new WinFormsDropShadow());
}
public WinFormsDropShadow()
{
Text = "Windows Forms Drop Shadow";
BackColor = Color.White;
Size = new Size(640, 480);
}
protected override void OnPaint(PaintEventArgs args)
{
Graphics grfx = args.Graphics;
Font fnt = new Font("Arial Black", 96);
string str = "Shadow";
grfx.DrawString(str, fnt, Brushes.Gray, grfx.DpiX / 12, grfx.DpiY / 12);
grfx.DrawString(str, fnt, Brushes.Black, 0, 0);
}
}
This isn't labels, but it should get you what you're trying to achieve:
public void Form1()
{
Paint += Form1_Paint;
}
public void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(SystemColors.Control);
DrawOverlappingLabels(e, *new point for label 1*, *new point for label 2*);
}
private void DrawOverlappingLabels(PaintEventArgs e, _
Point positionLabel1, Point positionLabel2)
{
var graphics = e.Graphics();
var rectLabel1 = new Rectangle(new positionLabel1, new Size(150, 30));
var rectLabel2 = new Rectangle(new positionLabel2, new Size(150, 30));
graphics.DrawString("Label1", new Font(Font.FontFamily, 24f), _
new SolidBrush(Color.Black), rectLabel1);
graphics.DrawString("Label2", new Font(Font.FontFamily, 8f), _
new SolidBrush(Color.Black), rectLabel2);
}
Define a second Rectangle to tweak the positioning of the 2nd "label".

How to change the background color of unused space tab in C# winforms?

Ex
|Tab1|Tab2|Tab3| { }
| |
| |
| |
| |
|_____________________|
I am able to change the backcolor and forecolor of Tab.. but I want to change the color of that { } -- > Empty space is this possible to do that. .. It shows default winforms color..help me in dis..
private void Form1_Load(object sender, EventArgs e)
{
}
private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Font fntTab;
Brush bshBack;
Brush bshFore;
if ( e.Index == this.tabControl1.SelectedIndex)
{
fntTab = new Font(e.Font, FontStyle.Bold);
bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, SystemColors.Control, SystemColors.Control, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
bshFore = Brushes.Black;
//bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.LightSkyBlue , Color.LightGreen, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
//bshFore = Brushes.Blue;
}
else
{
fntTab = e.Font;
bshBack = new SolidBrush(Color.Red);
bshFore = new SolidBrush(Color.Aqua);
//bshBack = new SolidBrush(Color.White);
//bshFore = new SolidBrush(Color.Black);
}
string tabName = this.tabControl1.TabPages[e.Index].Text;
StringFormat sftTab = new StringFormat();
e.Graphics.FillRectangle(bshBack, e.Bounds);
Rectangle recTab = e.Bounds;
recTab = new Rectangle( recTab.X, recTab.Y + 4, recTab.Width, recTab.Height - 4);
e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);
}
Try adding the following code to your DrawItem event handler. Don't forget to set the DrawMode to "OwnerdrawFixed".
You might have to tweak it a bit to cover some margins which aren't painted.
private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
SolidBrush fillbrush= new SolidBrush(Color.Red);
//draw rectangle behind the tabs
Rectangle lasttabrect = tabControl1.GetTabRect(tabControl1.TabPages.Count - 1);
Rectangle background = new Rectangle();
background.Location = new Point(lasttabrect.Right, 0);
//pad the rectangle to cover the 1 pixel line between the top of the tabpage and the start of the tabs
background.Size = new Size(tabControl1.Right - background.Left, lasttabrect.Height+1);
e.Graphics.FillRectangle(fillBrush, background);
}
'This answer is much better than prior one. But the tabCtrl is not defined. It has to be tabControl1 control.
I think the only way to give that space a color is to override the OnPaintBackground method of the window, so just paste this on your form (window)
you must also change the Appearance Property to "Normal"
private void Form1_Load(object sender, EventArgs e)
{
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
Rectangle lasttabrect = tabControl1.GetTabRect(tabControl1.TabPages.Count - 1);
RectangleF emptyspacerect = new RectangleF(
lasttabrect.X + lasttabrect.Width + tabControl1.Left,
tabControl1.Top + lasttabrect.Y,
tabControl1.Width - (lasttabrect.X + lasttabrect.Width),
lasttabrect.Height);
Brush b = Brushes.BlueViolet; // the color you want
e.Graphics.FillRectangle(b, emptyspacerect );
}
for me it's working perfectly
you can also create a custom tabcontrol as you did
public class mytab : TabControl
{
public mytab()
: base()
{
this.DrawMode = TabDrawMode.OwnerDrawFixed;
this.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
}
private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Font fntTab;
Brush bshBack;
Brush bshFore;
if (e.Index == this.SelectedIndex)
{
fntTab = new Font(e.Font, FontStyle.Bold);
bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, SystemColors.Control, SystemColors.Control, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
bshFore = Brushes.Black;
//bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.LightSkyBlue , Color.LightGreen, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
//bshFore = Brushes.Blue;
}
else
{
fntTab = e.Font;
bshBack = new SolidBrush(Color.Red);
bshFore = new SolidBrush(Color.Aqua);
//bshBack = new SolidBrush(Color.White);
//bshFore = new SolidBrush(Color.Black);
}
string tabName = this.TabPages[e.Index].Text;
StringFormat sftTab = new StringFormat();
e.Graphics.FillRectangle(bshBack, e.Bounds);
Rectangle recTab = e.Bounds;
recTab = new Rectangle(recTab.X, recTab.Y + 4, recTab.Width, recTab.Height - 4);
e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);
Rectangle r = this.GetTabRect(this.TabPages.Count - 1);
RectangleF tf =
new RectangleF(r.X + r.Width,
r.Y-5, this.Width - (r.X + r.Width)+5, r.Height+5);
Brush b = Brushes.BlueViolet;
e.Graphics.FillRectangle(b, tf);
}
}

ListBox AllowHTML in c#

is there a way to display different TextFormat/Style per item in ListBox
Example:
Data1: value
Data2: value world
Data3: value hello
i tried this but i cant make the next string in bold
private void lbDetails_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Rectangle rec = e.Bounds;
string[] temp = lbDetails.Items[e.Index].ToString().Split(':');
e.Graphics.DrawString(temp[0],
new Font("Arial",8, FontStyle.Regular),
Brushes.Black,
rec,
StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
SOLUTION
private void lbDetails_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
e.DrawBackground();
string[] temp = lbDetails.Items[e.Index].ToString().Split(':');
SizeF prevStr = e.Graphics.MeasureString(temp[0] + ": ", new Font("Arial", 8, FontStyle.Regular));
SizeF nextStr = e.Graphics.MeasureString(temp[1], new Font("Arial", 8, FontStyle.Bold));
RectangleF firstRec = new RectangleF(e.Bounds.X, e.Bounds.Y, prevStr.Width, prevStr.Height);
RectangleF secondRec = new RectangleF(prevStr.Width, e.Bounds.Y, nextStr.Width, nextStr.Height);
e.Graphics.DrawString(temp[0] + ": ",
new Font("Arial", 8, FontStyle.Regular),
Brushes.Black,
firstRec,
StringFormat.GenericDefault);
e.Graphics.DrawString(temp[1],
new Font("Arial", 8, FontStyle.Bold),
Brushes.Black,
secondRec,
StringFormat.GenericDefault);
//e.Graphics.DrawRectangle(Pens.Red, firstRec.X, firstRec.Y, firstRec.Width, firstRec.Height);
e.DrawFocusRectangle();
}
catch (Exception ex)
{
}
}
You need to draw two strings, the second one using a bold font. A full example:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
listBox1.Items.AddRange(new object[] { "Data1: value", "Data2: hello world", "Data3: hello hello"});
}
void listBox1_DrawItem(object sender, DrawItemEventArgs e) {
var box = (ListBox)sender;
e.DrawBackground();
if (e.Index < 0) return;
string[] temp = box.Items[e.Index].ToString().Split(':');
int size = (int)(TextRenderer.MeasureText(e.Graphics, temp[0], box.Font).Width + 0.5);
var rc = new Rectangle(e.Bounds.Left, e.Bounds.Top, (int)size, e.Bounds.Height);
var fmt = TextFormatFlags.Left;
TextRenderer.DrawText(e.Graphics, temp[0] + ":", box.Font, rc, e.ForeColor, fmt);
if (temp.Length > 1) {
using (var bold = new Font(box.Font, FontStyle.Bold)) {
rc = new Rectangle(e.Bounds.Left + size, e.Bounds.Top, e.Bounds.Width - size, e.Bounds.Height);
TextRenderer.DrawText(e.Graphics, temp[1], bold, rc, e.ForeColor, fmt);
}
}
e.DrawFocusRectangle();
}
}
Produces:
You can do it as mention in this Link. I know you are trying it differently, but it can help you.
There is a other link as well. This link describe how you can display items in a ListBox with different font style.
Here is other good link as well.

Set TabPage Header Color

Greetings,
I have a tab control and I want to have 1 of the tabs have it's text color changed on a event.
I've found answers like C# - TabPage Color event
and C# Winform: How to set the Base Color of a TabControl (not the tabpage)
but using these sets all colors instead of one.
So I was hoping there is a way to implement this with the tab I wish to change as a method instead of a event?
Something like:
public void SetTabPageHeaderColor(TabPage page, Color color)
{
//Text Here
}
If you want to color the tabs, try the following code:
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);
private Dictionary<TabPage, Color> TabColors = new Dictionary<TabPage, Color>();
private void SetTabHeader(TabPage page, Color color)
{
TabColors[page] = color;
tabControl1.Invalidate();
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
//e.DrawBackground();
using (Brush br = new SolidBrush (TabColors[tabControl1.TabPages[e.Index]]))
{
e.Graphics.FillRectangle(br, e.Bounds);
SizeF sz = e.Graphics.MeasureString(tabControl1.TabPages[e.Index].Text, e.Font);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2, e.Bounds.Top + (e.Bounds.Height - sz.Height) / 2 + 1);
Rectangle rect = e.Bounds;
rect.Offset(0, 1);
rect.Inflate(0, -1);
e.Graphics.DrawRectangle(Pens.DarkGray, rect);
e.DrawFocusRectangle();
}
}
For WinForms users reading this - This ONLY works if you set your tab control's DrawMode to OwnerDrawFixed - the DrawItem event never fires if it's set to Normal.
To add to Fun Mun Pieng's answer which works beautifully on Horizontal tabs, if you were to use Vertical tabs (like I was) then you would need something like this:
private void tabControl2_DrawItem(object sender, DrawItemEventArgs e)
{
using (Brush br = new SolidBrush(tabColorDictionary[tabControl2.TabPages[e.Index]]))
{
// Color the Tab Header
e.Graphics.FillRectangle(br, e.Bounds);
// swap our height and width dimensions
var rotatedRectangle = new Rectangle(0, 0, e.Bounds.Height, e.Bounds.Width);
// Rotate
e.Graphics.ResetTransform();
e.Graphics.RotateTransform(-90);
// Translate to move the rectangle to the correct position.
e.Graphics.TranslateTransform(e.Bounds.Left, e.Bounds.Bottom, System.Drawing.Drawing2D.MatrixOrder.Append);
// Format String
var drawFormat = new System.Drawing.StringFormat();
drawFormat.Alignment = StringAlignment.Center;
drawFormat.LineAlignment = StringAlignment.Center;
// Draw Header Text
e.Graphics.DrawString(tabControl2.TabPages[e.Index].Text, e.Font, Brushes.Black, rotatedRectangle, drawFormat);
}
}
I will echo the point that ROJO1969 made, if this is in WinForms - then you must set DrawMode to OwnerDrawFixed.
Special thanks goes out to this wonderful blog entry which described how to do a rotation of text on a form.
private void MainForm_Load(object sender, EventArgs e)
{
...
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);
...
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
// Draw the background of the control for each item.
//e.DrawBackground();
if (e.Index == this.tabControl1.SelectedIndex)
{
Brush _BackBrush = new SolidBrush(tabControl1.TabPages[e.Index].BackColor);
Rectangle rect = e.Bounds;
e.Graphics.FillRectangle(_BackBrush, (rect.X) + 4, rect.Y, (rect.Width) - 4, rect.Height);
SizeF sz = e.Graphics.MeasureString(tabControl1.TabPages[e.Index].Text, e.Font);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black,
e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2,
e.Bounds.Top + (e.Bounds.Height - sz.Height) / 2 + 1);
}
else
{
// 파스톤계 배경색 없앨려면 FromArgb 를 없애면 된다.
Brush _BackBrush = new SolidBrush(Color.FromArgb(50, tabControl1.TabPages[e.Index].BackColor));
Rectangle rect = e.Bounds;
e.Graphics.FillRectangle(_BackBrush, rect.X, (rect.Y)-0, rect.Width, (rect.Height)+6);
SizeF sz = e.Graphics.MeasureString(tabControl1.TabPages[e.Index].Text, e.Font);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black,
e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2,
e.Bounds.Top + 5);
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
If any one need to put color for tab header use try this. My tab name tabControl
tabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControl.DrawItem += tabControl1_DrawItem;
declear this under main class
then,
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Color tabTextColor = Color.FromArgb(0x000001);
var color = Color.FromArgb(tabTextColor.R, tabTextColor.G, tabTextColor.B);
TextRenderer.DrawText(e.Graphics, tabControl.TabPages[e.Index].Text, e.Font, e.Bounds, color);
}
declare this function it will generate output
final out

Categories

Resources