-
Paint Events
I touched on this in my last topic. I've been unable to get a 'Paint' event for a TextBox working - this is documented in the .NET help, but it is not listed in the Events list of the control (In VC#). I tried to write one into the code myself but it never triggers.
Code:
// In InitializeComponent ()
this.txtText.Paint += new System.Windows.Forms.PaintEventHandler(this.txtText_Paint);
// Paint function
private void txtText_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// This function never runs
}
Am I doing something wrong? I also notice that the TreeView control doesn't have a Paint event at all - is there a suitable alternative I can use?
-
You can use gdi32 instead.
Code:
using System.Runtime.InteropServices;
using System.Drawing;
public class API {
[DllImport("user32.dll", EntryPoint="GetWindowDC")]
public static extern int GetWindowDC (
int hwnd
);
[DllImport("user32.dll", EntryPoint="ReleaseDC")]
public static extern int ReleaseDC (
int hwnd,
int hdc
);
}
public class Form1 : System.Windows.Forms.Form {
private TreeView TV;
//
// IDE generated codes ............
//
private void DrawOnTheControl( ref Control c ) {
int hWnd= c.Handle.ToInt32();
int hDC = API.GetWindowDC( hWnd );
Graphics canvas = Graphics.FromHdc( new IntPtr( hDC ) );
// Do some drawings;
// Don't forget to release the gdi object
API.REleaseDC( hWnd , hDC );
}
}
-
Surely I need an event to call the DrawOnTheControl function? And since the function performs drawing it'd be a Paint event (which the TextBox and Tree controls don't seem to have)
... Unless I'm missing something
-
A Paint event of a control is raised when there is something changed on the control or it's surrounding.
So add DrawOnTheControl( ref treeviewInstance ) to the following events' handlers :
SizeChanged , Resize , DrawItem ( if exist ) , SelectedItemChanged , etc.
-
Ah, I see. Just out of curiousity, but why don't these controls have Paint events?
-
Quote:
Originally posted by raysoft_jack
You can use gdi32 instead.
Code:
using System.Runtime.InteropServices;
using System.Drawing;
public class API {
[DllImport("user32.dll", EntryPoint="GetWindowDC")]
public static extern int GetWindowDC (
int hwnd
);
[DllImport("user32.dll", EntryPoint="ReleaseDC")]
public static extern int ReleaseDC (
int hwnd,
int hdc
);
}
public class Form1 : System.Windows.Forms.Form {
private TreeView TV;
//
// IDE generated codes ............
//
private void DrawOnTheControl( ref Control c ) {
int hWnd= c.Handle.ToInt32();
int hDC = API.GetWindowDC( hWnd );
Graphics canvas = Graphics.FromHdc( new IntPtr( hDC ) );
// Do some drawings;
// Don't forget to release the gdi object
API.REleaseDC( hWnd , hDC );
}
}
I wouldn't use this unmanaged code . You can get same functionality in managed GDI+ .Just a thought ...
-
Well, I see ... ... I forget it has a CreateGraphics() method.
Code:
public class Form1 : System.Windows.Forms.Form {
private TreeView TV;
//
// IDE generated codes ............
//
private void DrawOnTheControl( ref Control c ) {
Graphics canvas = c.CreateGraphics();
// Call some mathods of canvas;
canvas.Flush();
canvas.Dispose();
}
}
-
Urgh... I still can't get this right. I tried the suggested events for calling the DrawOnTheControl function, but it still won't paint it when the control first appears. I tried instead to draw the borders when the form is initialised or resize, for example:
Code:
private void frmMain_Load(object sender, System.EventArgs e)
{
drawBorder (textBox1, Color.Blue);
}
private void drawBorder (Control ctrl, Color colour)
{
Graphics g = ctrl.CreateGraphics ();
Pen p = new Pen (colour, 2);
g.DrawRectangle (p,
ctrl.ClientRectangle.Left, ctrl.ClientRectangle.Top,
ctrl.ClientRectangle.Width, ctrl.ClientSize.Height);
g.Flush ();
g.Dispose ();
}
It works ok with the resize event (although you can still see 'trails' of the original black colour), but the Load function doesn't work at all - I'm guessing this is because the control hasn't been loaded yet.
Is there not a better way to do this?
Also, while I've got the code posted, how can I pass the control in by reference? I've tried using 'ref' but it complains about me passing a TextBox rather a Control.
Thanks with your help so far, though :(