Solved

Problem painting NumericUpDown control

Posted on 2004-09-12
12
1,058 Views
Last Modified: 2012-06-27
I am trying to modify the way a NumericUpDown control draws itself. I just need to hide the up/down arrows. I tried overriding both the OnPaint method and the wm_paint message inside WndProc. Neither way was successful. The overridden OnPaint method is not being called at all. WndProc is being called but my changes have no effect, even though I have successfully done a similar thing with ComboBox. Your help will be greatly appreciated.

This is my test code. Here I try painting the whole control, but nothing happens:

Protected Overrides Sub WndProc(ByRef m As Message)
  MyBase.WndProc(m)
  Select Case m.Msg
    Case &HF
       Dim g As Graphics = Me.CreateGraphics
       Dim r As Rectangle = Me.ClientRectangle
       g.FillRectangle(ArrowBrush, r)
    Case Else
       Exit Select
    End Select
End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  MyBase.OnPaint(e)
  Dim g As Graphics = Me.CreateGraphics
  Dim r As Rectangle = Me.ClientRectangle
  g.FillRectangle(ArrowBrush, r)
End Sub

thanks
0
Comment
Question by:cyberzork
  • 7
  • 5
12 Comments
 
LVL 8

Expert Comment

by:wguerram
Comment Utility
Try this:

<DllImport("user32.dll")> _
    Public Shared Function GetWindowDC(ByVal hWnd As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll")> _
    Public Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
    End Function

Protected Overrides Sub WndProc(ByRef m As Message)
  MyBase.WndProc(m)
  Select Case m.Msg
    Case &HF
       MyBase.WndProc(m) 'This call is need to take care of some messages you are not handling
       'You have to do this every time you handle the WM_PAINT

       Dim hDC As IntPtr = GetWindowDC(m.HWnd)
       Dim g As Graphics = Graphics.FromHdc(hDC)

       Dim r As Rectangle = Me.ClientRectangle
       'You can also use the bounds property
       g.FillRectangle(ArrowBrush, r)

       ReleaseDC(m.HWnd, hDC)
       g.Dispose
    Case Else
       MyBase.WndProc(m) 'Also make this call
    End Select
End Sub

'Let me know if you have problems



0
 

Author Comment

by:cyberzork
Comment Utility
Hi wguerram,

Thanks for your help. I tried the code and it paints only a top and left border (width = 1 pixel). We are probably very close to a solution. Any further help will be appreciated.

thanks
0
 
LVL 8

Expert Comment

by:wguerram
Comment Utility
It looks like the ClientRect is actually bigger than the rect you can draw in.

Try this:

with Me.ClientRectangle
       Dim r As Rectangle = New Rectangle(.X, .Y, .Width - 2, .Height - 2)
End With

Try changin the difference values until you get to paint the border.

if doesn't work try with -3, -4 and so forth.

Let me know what happens.
0
 
LVL 8

Expert Comment

by:wguerram
Comment Utility
Sorry i didn't check you were trying to fill the control.

let me take a look at it.
0
 
LVL 8

Expert Comment

by:wguerram
Comment Utility
I think the problem is because the numeric updown control is composed of four controls:

- TextBox
- Buttons
-The Control container itself

So We are getting the handle of the Control container, that's why its only painting the backside of the control, now we need to get the handle of the textboxes and buttons in order to paint them.

Right now i don't the time to get into it, but as soon as i can i will help you with that.

There must be some kind of message to to get an structures with all handles.
0
 

Author Comment

by:cyberzork
Comment Utility
thanks
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:cyberzork
Comment Utility
I am trying to use GetDCEx instead of GetWindowDC. I am not familiar with the Windows API, but I think this should work.
0
 
LVL 8

Expert Comment

by:wguerram
Comment Utility
Could you get GetDCEx to work?
0
 

Author Comment

by:cyberzork
Comment Utility
No, I couldn't figure out what parameters to pass!
0
 
LVL 8

Accepted Solution

by:
wguerram earned 300 total points
Comment Utility
I thik i found the solution:

You have to subclass the controls in the numeric updown control

This controls has two controls

dim n as numericupdown

Corresponding to the edit and scrollbar
n.Controls(0)
n.Controls(1)

Now in the new we have to subclass this controls and trap their WM_PAINT messages.

Right now i can't make an example since i have been very busy.

But this should make the work.

we have to use SetWindowLong API
0
 

Author Comment

by:cyberzork
Comment Utility
Thanks! This is what I needed. I just wanted to hide the scrollbar, which I can do by calling

   Controls(0).Hide

Thanks again. I am increasing your points to 300. You deserve it.
0
 
LVL 8

Expert Comment

by:wguerram
Comment Utility
Yes you are right, you just wanted to hide the controls, i lost the main objective thinking on drawing the control.

Thanks for the points.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now