Results 1 to 8 of 8

Thread: Paint Events

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    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?
    Using Visual Studio .NET 2005

  2. #2
    New Member raysoft_jack's Avatar
    Join Date
    Jul 2004
    Posts
    6
    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 );
        }
    
    }

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195
    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
    Using Visual Studio .NET 2005

  4. #4
    New Member raysoft_jack's Avatar
    Join Date
    Jul 2004
    Posts
    6
    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195
    Ah, I see. Just out of curiousity, but why don't these controls have Paint events?
    Using Visual Studio .NET 2005

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 ...

  7. #7
    New Member raysoft_jack's Avatar
    Join Date
    Jul 2004
    Posts
    6
    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();
        }
    
    }
    Jack H Hansen

    Een schip op het strand is een baken in zee.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195
    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
    Using Visual Studio .NET 2005

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width