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. OwnerDraw TextBox

OwnerDraw TextBox

Scheduled Pinned Locked Moved C#
questioncsharpjson
6 Posts 3 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.
  • F Offline
    F Offline
    Furty
    wrote on last edited by
    #1

    I am trying to create an owner-drawn TextBox control, but as anyone who may have tried this knows, overriding the OnPaint method is useless as the base class uses the Windows API for painting. I know I can capture the WM_NCPAINT message in WndProc, but the absolute most you can do with this in reality is paint a custom border for the TextBox, anything else just gets painted over by the base class. So the question is, how can I paint the TextBox without having to resort to building the whole damn thing from scratch? Has anyone actually seen a custom painted TextBox actually implementsd in .Net? Or (hopefully) am I missing something here?

    S 1 Reply Last reply
    0
    • F Furty

      I am trying to create an owner-drawn TextBox control, but as anyone who may have tried this knows, overriding the OnPaint method is useless as the base class uses the Windows API for painting. I know I can capture the WM_NCPAINT message in WndProc, but the absolute most you can do with this in reality is paint a custom border for the TextBox, anything else just gets painted over by the base class. So the question is, how can I paint the TextBox without having to resort to building the whole damn thing from scratch? Has anyone actually seen a custom painted TextBox actually implementsd in .Net? Or (hopefully) am I missing something here?

      S Offline
      S Offline
      Stephane Rodriguez
      wrote on last edited by
      #2

      I can't really test it since I don't have .NET at work, but from the class outlook, I can see that : - TextBox inherits the PaintEventHandler from its base class (TextBoxBase) : have you tried to implement an handler for it ? - I don't know why overriding WndProc and processing WM_PAINT (not WM_NCPAINT) should not let you do exactly what you want. In addition to WM_PAINT, I believe you should process WM_ERASEBKGND as well. In fact what those WIN32 messages are for is as follows : - WM_ERASEBKGND : background - WM_PAINT : actual content - WM_NCPAINT : border It's reasonable to think that the .NET PaintEvent event is thrown whenever the base class receives (reflected) the WM_PAINT message.

      F 1 Reply Last reply
      0
      • S Stephane Rodriguez

        I can't really test it since I don't have .NET at work, but from the class outlook, I can see that : - TextBox inherits the PaintEventHandler from its base class (TextBoxBase) : have you tried to implement an handler for it ? - I don't know why overriding WndProc and processing WM_PAINT (not WM_NCPAINT) should not let you do exactly what you want. In addition to WM_PAINT, I believe you should process WM_ERASEBKGND as well. In fact what those WIN32 messages are for is as follows : - WM_ERASEBKGND : background - WM_PAINT : actual content - WM_NCPAINT : border It's reasonable to think that the .NET PaintEvent event is thrown whenever the base class receives (reflected) the WM_PAINT message.

        F Offline
        F Offline
        Furty
        wrote on last edited by
        #3

        Thanks heaps for the suggestions, but unfortunately I'm still no closer to my goal. Intercepting WM_PAINT instead of WM_NCPAINT still only gives me the ability to paint the same 2px wide border - not the background itself. WM_ERASEBKGND also doesn't get me anywhere as the border and control are painted after it anyway. The problem is that the text area is being overlaid on top, wiping out any background painting you do. Even if I were happy to only have access to paint a 2px wide border, the problem when doing that is that it totally messes up the scrollbar painting.. Uggghhh.

        J 1 Reply Last reply
        0
        • F Furty

          Thanks heaps for the suggestions, but unfortunately I'm still no closer to my goal. Intercepting WM_PAINT instead of WM_NCPAINT still only gives me the ability to paint the same 2px wide border - not the background itself. WM_ERASEBKGND also doesn't get me anywhere as the border and control are painted after it anyway. The problem is that the text area is being overlaid on top, wiping out any background painting you do. Even if I were happy to only have access to paint a 2px wide border, the problem when doing that is that it totally messes up the scrollbar painting.. Uggghhh.

          J Offline
          J Offline
          Jeff J
          wrote on last edited by
          #4

          You didn't say, so just to confirm: are you are inheriting from TextBox or TextBoxBase, and explicitly overriding OnPaint and OnPaintBackground, and are calling SetStyle with the ControlStyles.UserPaint (and optionally AllPaintingInWmPaint and DoubleBuffer)? If you comment-out the normally-required base.OnPaint call, the text is still rendered by the CLR stuff?

          F 1 Reply Last reply
          0
          • J Jeff J

            You didn't say, so just to confirm: are you are inheriting from TextBox or TextBoxBase, and explicitly overriding OnPaint and OnPaintBackground, and are calling SetStyle with the ControlStyles.UserPaint (and optionally AllPaintingInWmPaint and DoubleBuffer)? If you comment-out the normally-required base.OnPaint call, the text is still rendered by the CLR stuff?

            F Offline
            F Offline
            Furty
            wrote on last edited by
            #5

            Hi Jeff, I'm inheriting from TextBox not TextBoxBase as I wish to maintin all the functionality of the TextBox control. Overriding OnPaint and/or OnPaintBackground has odd effects, regardless of what Styles have been set, as the TextBox (or TextBoxBase) is overpainting the control with WndProc intercepts.

            J 1 Reply Last reply
            0
            • F Furty

              Hi Jeff, I'm inheriting from TextBox not TextBoxBase as I wish to maintin all the functionality of the TextBox control. Overriding OnPaint and/or OnPaintBackground has odd effects, regardless of what Styles have been set, as the TextBox (or TextBoxBase) is overpainting the control with WndProc intercepts.

              J Offline
              J Offline
              Jeff J
              wrote on last edited by
              #6

              I'll have to remember that, thanks for the heads-up. I suppose I should be glad I wrote a binary editor derived straight from Control, as I can see I would have had the problems you're having. Of course, much of TextBox's functionality didn't apply for me. Still a pain in the "details", so I see why you're trying to avoid it. Still, I'm curious as to why the problems. I'll post back if I find anything.

              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