S
Sagaert Johan
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up.
I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the OnPaint .
The recangle draws correct when i press the mouse, but when i release the mouse the background is not restored
What should i do in the Onpaint to make sure the background (the form) is restored correctly ?
This problem already keeps me busy for 2 days ...
Johan
copy this code in a new win c# app
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
using System.Reflection;
namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Hotspot spot ;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Desktop;
this.ClientSize = new System.Drawing.Size(152, 141);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
spot=new Hotspot() ;
spot.Size=new Size(50,50);
spot.Location=new Point(40,40);
spot.BringToFront();
spot.Visible=true;
this.Controls.Add(spot);
}
}
public class Hotspot : Control
{
//private Image image;
private bool bPushed;
private Bitmap m_bmpOffscreen;
Brush GrayBrush;
public Hotspot()
{
bPushed = false;
//default minimal size
this.Size = new Size(21, 21);
GrayBrush=new SolidBrush(Color.Gray);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e )
{
Graphics gxOff; //Offscreen graphics
if (m_bmpOffscreen == null) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
}
gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(Color.Empty);
if (bPushed)
{
gxOff.FillRectangle(GrayBrush,5,5,ClientSize.Width-10, ClientSize.Height-10);
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
}
base.OnPaint(e);
}
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e )
{
}
protected override void OnMouseDown ( System.Windows.Forms.MouseEventArgs e )
{
bPushed = true;
Invalidate();
}
protected override void OnMouseUp ( System.Windows.Forms.MouseEventArgs e )
{
bPushed = false;
Invalidate();
}
}
}
I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the OnPaint .
The recangle draws correct when i press the mouse, but when i release the mouse the background is not restored
What should i do in the Onpaint to make sure the background (the form) is restored correctly ?
This problem already keeps me busy for 2 days ...
Johan
copy this code in a new win c# app
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
using System.Reflection;
namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Hotspot spot ;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Desktop;
this.ClientSize = new System.Drawing.Size(152, 141);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
spot=new Hotspot() ;
spot.Size=new Size(50,50);
spot.Location=new Point(40,40);
spot.BringToFront();
spot.Visible=true;
this.Controls.Add(spot);
}
}
public class Hotspot : Control
{
//private Image image;
private bool bPushed;
private Bitmap m_bmpOffscreen;
Brush GrayBrush;
public Hotspot()
{
bPushed = false;
//default minimal size
this.Size = new Size(21, 21);
GrayBrush=new SolidBrush(Color.Gray);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e )
{
Graphics gxOff; //Offscreen graphics
if (m_bmpOffscreen == null) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
}
gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(Color.Empty);
if (bPushed)
{
gxOff.FillRectangle(GrayBrush,5,5,ClientSize.Width-10, ClientSize.Height-10);
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
}
base.OnPaint(e);
}
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e )
{
}
protected override void OnMouseDown ( System.Windows.Forms.MouseEventArgs e )
{
bPushed = true;
Invalidate();
}
protected override void OnMouseUp ( System.Windows.Forms.MouseEventArgs e )
{
bPushed = false;
Invalidate();
}
}
}