Jump to content
  • Advertisement
Sign in to follow this  
MatsK

Drawing custom buttons on forms (C#)

This topic is 2544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to implement a custom form and have done away with the form itself. Now I need some buttons!
I've tried implementing my own button class, but it just doesn't want to draw!!


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace PDPatcher
{
class UIButton : Button
{
private Graphics m_Gfx;
private Bitmap m_Img;
private int m_X, m_Y, m_BtnHeight, m_BtnWidth;
public int XPosition
{
get { return m_X; }
}
public int YPosition
{
get { return m_Y; }
}
public int Width
{
get { return m_BtnWidth; }
}
public int Height
{
get { return m_BtnHeight; }
}
public Bitmap BtnImg
{
get { return m_Img; }
}
public UIButton(Form Parent, string ImgPath, int X, int Y, int ButtonWidth, int ButtonHeight)
: base()
{
this.Parent = Parent;
m_Gfx = this.CreateGraphics();
m_Img = new Bitmap(Bitmap.FromFile(ImgPath));
m_BtnWidth = ButtonWidth;
m_BtnHeight = ButtonHeight;
}
public UIButton(Form Parent, Bitmap Img, int X, int Y, int ButtonWidth, int ButtonHeight)
: base()
{
m_Gfx = this.CreateGraphics();
m_Img = new Bitmap(Img);
m_BtnWidth = ButtonWidth;
m_BtnHeight = ButtonHeight;
}
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
pevent.Graphics.DrawImage(m_Img, new Rectangle(m_X, m_Y, m_BtnWidth, m_BtnHeight));
}
}
}


Here's the code for my form:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace PDPatcher
{
public partial class Form1 : Form
{
private Point m_MouseOffset;
private UIButton m_ExitBtn, m_MinimizeBtn;
public Form1()
{
InitializeComponent();
m_ExitBtn = new UIButton(this, Properties.Resources._2ab5bffa_Patcher_XCloseBtn, 0, 0, 16, 16);
this.MouseDown += new MouseEventHandler(Form1_MouseDown);
this.MouseMove += new MouseEventHandler(Form1_MouseMove);
m_ExitBtn.Paint += new PaintEventHandler(m_ExitBtn_Paint);
}
void m_ExitBtn_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(m_ExitBtn.BtnImg, new Rectangle(m_ExitBtn.XPosition, m_ExitBtn.YPosition,
m_ExitBtn.Width, m_ExitBtn.Height));
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point MousePosition = Control.MousePosition;
MousePosition.Offset(m_MouseOffset);
this.Location = MousePosition;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
m_MouseOffset = new Point(-e.X, -e.Y);
}
}
}



Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!