Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Overriding custom contol OnPaint

Overriding custom contol OnPaint

Scheduled Pinned Locked Moved C#
csharpwinformsgraphicsquestionlearning
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    I am just starting to learn about creating custom controls using the book "GDI+ Custom Controls with Visual C# 2005". I am a little confused about overriding the control's OnPaint function. As a test, I created a custom control that has just a label. I then created the following code:

    protected override void OnPaint(PaintEventArgs e)
    {
    // base.OnPaint(e);
    }

    I figured that since I am not calling the base.OnPaint(e), that the label would not get displayed, however when I run this, the label still shows up. Can someone explain why overriding a custom control and not calling base.OnPaint(e) does not effect and controls that are part of the custom control?

    L 1 Reply Last reply
    0
    • H hpjchobbes

      I am just starting to learn about creating custom controls using the book "GDI+ Custom Controls with Visual C# 2005". I am a little confused about overriding the control's OnPaint function. As a test, I created a custom control that has just a label. I then created the following code:

      protected override void OnPaint(PaintEventArgs e)
      {
      // base.OnPaint(e);
      }

      I figured that since I am not calling the base.OnPaint(e), that the label would not get displayed, however when I run this, the label still shows up. Can someone explain why overriding a custom control and not calling base.OnPaint(e) does not effect and controls that are part of the custom control?

      L Offline
      L Offline
      Lutoslaw
      wrote on last edited by
      #2

      When you create a label control, a new window is created and as every window it's receiving it's own messages from the main message loop, including the paint message. A method Label.OnPaint is not invoked from the UserControl.OnPaint. It's called by Control.WmPaint(ref Message m), which is called by (...), which is called by Application.ThreadContext.RunMessageLoop(int reason, ApplicationContext context). Try to make a "user control", which derives from Label, like this:

      class MyControl : Label
      {
      ...
      override OnPaint { }
      }

      Then the label will not be drawn.

      Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups