Search
 
 

Display results as :
 


Rechercher Advanced Search

Latest topics
» ~=?{The future is here, TastyExploit officially open to the public}?=~
Tue Mar 02, 2010 11:00 pm by Dami

» My first aa script
Tue Mar 02, 2010 7:00 am by wafflemaster

» the real story behind Maverin
Tue Mar 02, 2010 1:33 am by SoundOfDeat

» Random spam
Mon Mar 01, 2010 11:52 pm by Danny1994

» [Request] EMS v56 Exe
Mon Mar 01, 2010 12:39 am by Dami

» [Blacklist] NX-Trading Blacklist.
Mon Mar 01, 2010 12:38 am by Danny1994

» I have a question regarding the meso farmer
Sat Feb 27, 2010 10:30 pm by Dami

» What are you listening to now?
Sat Feb 27, 2010 7:57 pm by Noname111111

» Video(s) Of The Day
Sat Feb 27, 2010 7:37 pm by Noname111111

Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search
Affiliates
free forum
 



[C#]Translucent Label

Go down

[C#]Translucent Label

Post by Dami on Fri Oct 30, 2009 4:09 am

By ZenXChaos

Ok, Well, In case someone reading doesn't know what Translucent is, translucent is just another word for Transparent.It just sounds a But more fancy,More professional like, and a bit Cooler.Well, in my opinion that is.

Anyway, lets get to the tutorial.

Step 1-
You need to Add the following code to Your On Paint
Method.
You can find this by Viewing your Events in the properties
window

Code:
protected override void OnPaint(PaintEventArgs e)
{
   SmoothingMode sm = e.Graphics.SmoothingMode;
   e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
   if (_borderStyle == ShapeBorderStyles.ShapeBSFixedSingle)
      DrawBorder(e.Graphics);
   DrawLabelBackground(e.Graphics);
   DrawText(e);
   e.Graphics.SmoothingMode = sm;
}

Step 2 -
Add Draw borders method

Code:
private void DrawBorder(Graphics g)
{
   Rectangle rect = this.ClientRectangle;
   rect.Width--;
   rect.Height--;
   using (GraphicsPath bp = GetPath(rect, _radius))
   {
      using (Pen p = new Pen(_borderColor))
      {
         g.DrawPath(p, bp);
      }
   }
}

Step 3 -
Now, We are almost done, but there is quite a bit more
Coding to add.

Code:
private void DrawBorder(Graphics g)
{
   Rectangle rect = this.ClientRectangle;
   rect.Width--;
   rect.Height--;
   using (GraphicsPath bp = GetPath(rect, _radius))
   {
      using (Pen p = new Pen(_borderColor))
      {
         g.DrawPath(p, bp);
      }
   }
}

and this

Code:
private void DrawLabelBackground(Graphics g) 
{
   Rectangle rect = this.ClientRectangle;
   iRect = rect;
   rect.X++;
   rect.Y++;
   rect.Width -= 2;
   rect.Height -= 2;
   using (GraphicsPath bb = GetPath(rect, _radius))
   {
      using (Brush br = new SolidBrush(
         Color.FromArgb(_opacity, _backColor)))
      {
         g.FillPath(br, bb);
      }
   }
}

Step 4 -
Get Path Method, called by

Code:
protected GraphicsPath GetPath(Rectangle rc, int r)
{
   int x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height;
   r = r << 1;
   GraphicsPath path = new GraphicsPath();
   if (r > 0)
      //  If the radious of rounded corner is greater than one side then
      //  do the side rounded
   {
      if (r > h) { r = h; };                    //Rounded
      if (r > w) { r = w; };                    //Rounded
      path.AddArc(x, y, r, r, 180, 90);            //Upper left corner
      path.AddArc(x + w - r, y, r, r, 270, 90);      //Upper right corner
      path.AddArc(x + w - r, y + h - r, r, r, 0, 90);  //Lower right corner
      path.AddArc(x, y + h - r, r, r, 90, 90);       //Lower left corner
      path.CloseFigure();
   }
   else
      // If the radious of rounded corner is zero then the path is
      // a rectangle
   {
      path.AddRectangle(rc);
   }

   return path;
}

Step 5 -
Drawtext Method.
Add the following code

Code:
protected void DrawText(PaintEventArgs pe)
{
   //This is a workaround to get MeasureString to work properly
   //pe.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
   SizeF sz = pe.Graphics.MeasureString(_text, base.Font);
   switch (_moving)
   {
   case MoveType.None:
      NoMove();
      break;
   case MoveType.RightToLeft:
      MoveRightToLeft();
      break;
   case MoveType.DownToUp:
      MoveDownToUp();
      break;
   case MoveType.LeftToRight:
      MoveLeftToRight();
      break;
   case MoveType.UpToDown:
      MoveUpToDown();
      break;
   }
   //Rectangle bounds for the text
   txtRect = new Rectangle(this.pointX, this.pointY,
      (int)sz.Width + 1, (int)sz.Height);
   //If the mouse is passing over the text it is selected and will be dimmed
   //otherwise nothing.
   Brush brText = new SolidBrush(base.ForeColor);
   Brush brTextDimmed = new SolidBrush(_dimmedColor);
   if (_isSelected)
      pe.Graphics.DrawString(_text,
      base.Font,
      brTextDimmed,
      txtRect);
   else
      pe.Graphics.DrawString(_text,
      base.Font,
      brText,
      txtRect);
}

Step 6 -
Moving Methods

Code:
protected void NoMove()
{
   //Align text
   switch (_textAlign)
   {
   case TextAlignment.Left:
      pointX = (int)this.iRect.X;
      break;
   case TextAlignment.Center:
      pointX = (this.iRect.Width - this.txtRect.Width) / 2;
      break;
   case TextAlignment.Right:
      pointX = (this.iRect.Width - this.txtRect.Width);
      break;
   }
   pointY = (this.iRect.Height - this.txtRect.Height) / 2;
}
protected void MoveRightToLeft()
{
   if (pointX < -this.txtRect.Width)
   { pointX = this.iRect.X + this.iRect.Width; }
   else
   { pointX -= 2; }
   pointY = (this.iRect.Height - this.txtRect.Height) / 2;
}
protected void MoveDownToUp()
{
   pointX = (this.iRect.Width - this.txtRect.Width) / 2;
   if (pointY < -this.txtRect.Height)
   { pointY = (int)this.iRect.Y + this.iRect.Height; }
   else
   { pointY -= 2; }
}
protected void MoveLeftToRight()
{
   if (pointX > this.iRect.X + this.iRect.Width)
   { pointX = this.iRect.X - this.txtRect.Width; }
   else
   { pointX += 2; }
   pointY = (this.iRect.Height - this.txtRect.Height) / 2;
}
protected void MoveUpToDown()
{
   pointX = (this.iRect.Width - this.txtRect.Width) / 2;
   if (pointY > this.iRect.Y + this.iRect.Height)
   { pointY = (int)this.iRect.Y - this.iRect.Height; }
   else
   { pointY += 2; }
}

Step 7 -
Here is a Fun addon-
This will interact with your Mouse Activities
Mouse Enter and Leave

Code:
protected override void OnMouseEnter(EventArgs e)
{
   base.OnMouseEnter(e);
   _isSelected = true;
   this.Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
   base.OnMouseLeave(e);
   _isSelected = false;
   this.Invalidate();
}

Step 8 -
Add a Timer to your form.
After adding the Timer,Double Click the Timer and
add the following code

Code:

{
   this.Update();
   this.Invalidate();
}

It should look a bit like this

Code:

private void timer1_Tick(object sender, System.EventArgs e)
{
   this.Update();
   this.Invalidate();
}

Full Code Add-On:

Code:
protected override void OnPaint(PaintEventArgs e)
{
   SmoothingMode sm = e.Graphics.SmoothingMode;
   e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
   if (_borderStyle == ShapeBorderStyles.ShapeBSFixedSingle)
      DrawBorder(e.Graphics);
   DrawLabelBackground(e.Graphics);
   DrawText(e);
   e.Graphics.SmoothingMode = sm;
}
private void DrawBorder(Graphics g)
{
   Rectangle rect = this.ClientRectangle;
   rect.Width--;
   rect.Height--;
   using (GraphicsPath bp = GetPath(rect, _radius))
   {
      using (Pen p = new Pen(_borderColor))
      {
         g.DrawPath(p, bp);
      }
   }
}
private void DrawLabelBackground(Graphics g) 
{
   Rectangle rect = this.ClientRectangle;
   iRect = rect;
   rect.X++;
   rect.Y++;
   rect.Width -= 2;
   rect.Height -= 2;
   using (GraphicsPath bb = GetPath(rect, _radius))
   {
      using (Brush br = new SolidBrush(
         Color.FromArgb(_opacity, _backColor)))
      {
         g.FillPath(br, bb);
      }
   }
}
protected GraphicsPath GetPath(Rectangle rc, int r)
{
   int x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height;
   r = r << 1;
   GraphicsPath path = new GraphicsPath();
   if (r > 0)
      //  If the radious of rounded corner is greater than one side then
      //  do the side rounded
   {
      if (r > h) { r = h; };                    //Rounded
      if (r > w) { r = w; };                    //Rounded
      path.AddArc(x, y, r, r, 180, 90);            //Upper left corner
      path.AddArc(x + w - r, y, r, r, 270, 90);      //Upper right corner
      path.AddArc(x + w - r, y + h - r, r, r, 0, 90);  //Lower right corner
      path.AddArc(x, y + h - r, r, r, 90, 90);       //Lower left corner
      path.CloseFigure();
   }
   else
      // If the radious of rounded corner is zero then the path is
      // a rectangle
   {
      path.AddRectangle(rc);
   }

   return path;
}
protected void DrawText(PaintEventArgs pe)
{
   //This is a workaround to get MeasureString to work properly
   //pe.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
   SizeF sz = pe.Graphics.MeasureString(_text, base.Font);
   switch (_moving)
   {
   case MoveType.None:
      NoMove();
      break;
   case MoveType.RightToLeft:
      MoveRightToLeft();
      break;
   case MoveType.DownToUp:
      MoveDownToUp();
      break;
   case MoveType.LeftToRight:
      MoveLeftToRight();
      break;
   case MoveType.UpToDown:
      MoveUpToDown();
      break;
   }
   //Rectangle bounds for the text
   txtRect = new Rectangle(this.pointX, this.pointY,
      (int)sz.Width + 1, (int)sz.Height);
   //If the mouse is passing over the text it is selected and will be dimmed
   //otherwise nothing.
   Brush brText = new SolidBrush(base.ForeColor);
   Brush brTextDimmed = new SolidBrush(_dimmedColor);
   if (_isSelected)
      pe.Graphics.DrawString(_text,
      base.Font,
      brTextDimmed,
      txtRect);
   else
      pe.Graphics.DrawString(_text,
      base.Font,
      brText,
      txtRect);
}
protected void NoMove()
{
   //Align text
   switch (_textAlign)
   {
   case TextAlignment.Left:
      pointX = (int)this.iRect.X;
      break;
   case TextAlignment.Center:
      pointX = (this.iRect.Width - this.txtRect.Width) / 2;
      break;
   case TextAlignment.Right:
      pointX = (this.iRect.Width - this.txtRect.Width);
      break;
   }
   pointY = (this.iRect.Height - this.txtRect.Height) / 2;
}
protected void MoveRightToLeft()
{
   if (pointX < -this.txtRect.Width)
   { pointX = this.iRect.X + this.iRect.Width; }
   else
   { pointX -= 2; }
   pointY = (this.iRect.Height - this.txtRect.Height) / 2;
}
protected void MoveDownToUp()
{
   pointX = (this.iRect.Width - this.txtRect.Width) / 2;
   if (pointY < -this.txtRect.Height)
   { pointY = (int)this.iRect.Y + this.iRect.Height; }
   else
   { pointY -= 2; }
}
protected void MoveLeftToRight()
{
   if (pointX > this.iRect.X + this.iRect.Width)
   { pointX = this.iRect.X - this.txtRect.Width; }
   else
   { pointX += 2; }
   pointY = (this.iRect.Height - this.txtRect.Height) / 2;
}
protected void MoveUpToDown()
{
   pointX = (this.iRect.Width - this.txtRect.Width) / 2;
   if (pointY > this.iRect.Y + this.iRect.Height)
   { pointY = (int)this.iRect.Y - this.iRect.Height; }
   else
   { pointY += 2; }
}
protected override void OnMouseEnter(EventArgs e)
{
   base.OnMouseEnter(e);
   _isSelected = true;
   this.Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
   base.OnMouseLeave(e);
   _isSelected = false;
   this.Invalidate();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
   this.Update();
   this.Invalidate();
}

Original Code - Www.CodeProject.Com
I just Made it a Tutorial hopefull explaining a bit
more than the site does.
So credits to the both of us(ME and site)

HAVE FUN!!!!
It's really nice
avatar
Dami
Dragon
Dragon

Posts : 1414
Join date : 2009-08-10
Age : 29
Location : Finland

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum