How do I paint a hosted NumericUpDown using the existing ToolStripRenderer? - c#

I have a control which hosts a NumericUpDown in a ToolStripControlHost and exposes the NumericUpDown's properties. Functionally, that's fine, but when it's placed on a ToolStrip it doesn't fit visually with the other ToolStripItems.
I'd like to use the ToolStrip's existing ToolStripRenderer to draw the control in a manner similar to the ToolStripComboBoxes that are also on the control. I'm not interested in creating a custom ToolStripRenderer to do that, as I want others who use the control to be able to use it with whichever renderer they choose.
I've tried overriding OnPaint and OnPaintBackground in the hosting control class, and in the hosted control class, and using the renderer's drawing methods, but all I can achieve is to have an unpainted region around the edges; the spin buttons are still drawn with the system theme.
How do I paint a hosted NumericUpDown using the existing ToolStripRenderer?

The NumericUpDown class is really just a wrapper around the common control class that exists as part of GDI. The underlying common control will draw itself in one of two ways depending on if visual styles are enabled for the application. To override the drawing you will need to perform all the drawing yourself. You cannot tell the control you want to only override the buttons or the border.
Performing all the drawing yourself is much harder than it sounds. Sorry I cannot offer an easy solution but I do not think there is an easy solution.

Related

C# Winform: Remove the border from a standard button?

Is it possible to remove the border of a standard C# Button ? I know that I could set the FlatStyle property to Flat and customize the FlatAppearance property to hide the border but I would prefer to use the Standard property to ensure that the control appears under all operating systems like a default Button. Did you have any suggestions how I can solve this issue? I am working with a Windows Forms Application.
You could use an image and make it clickable, that is one way to avoid the borders, but there are several options. Good luck!
If you are using windows forms and trying to implement Flat UI, Material UI, Metro-Looking controls, you're in hell. It is has limited properties for customizations unless you know how to work with Graphics and Animation. You'll have to use imagebox or picture box and dynamically change its image on different events.
I'll suggest WPF or Windows Presentation Foundation for a more customizable User Interface.

Windows 8 native custom control

I want to make a custom control with the Windows 8 API in C# so that it will run on an RT app distributed through the store.
I looked through what I can add to a project and found Templated Control and User Control. Both allow me to organize other controls and some logic into a new control for reusability.
But this isn't what I want. I want to be able to custom paint and create controls. I would be shocked if they removed this but am honestly uncertain if I can. I'm basically looking for what class to inherit from and what method / event to override to let me draw the control myself.
I found a way to use the Templated Control option that I wasn't aware of before. Essentially my custom control has a canvas that I 'paint' with objects (lines, images, etc.) by adding them as children and manipulating them in events.
This tutorial helped me figure out what I was doing.
I've seen other people say they created a rectangle and painted it with a special Brush, either a ImageBrush (where they drew an image in the background) or a DrawingBrush.
Hopefully one of these two approaches can help anyone looking for the same thing I was.

Windows Forms templated data

I am developing an application that wraps Lua to enable interactivity with geometric shapes and their properties and I am about to finish but... in the main window, I have a flowlayout panel that should show the properties for said shape, as how Lua dictates.
Here comes problem/question as I don't think clearing the panel and re-adding the controls that represent each property, each time I have to update the properties is a good idea nor the most effective solution, is there something I can do to enhance this?

Custom look for C# windows applications

I'm making C# windows application ("Windows Forms Application") with default components, but I need to change default look of the form and all items on it (buttons, etc.) from classic windows style, to custom. How can I achieve it?
For example make buttons round, add transparency to some objects, make other objects with different shape, and so on.
It doesn't have to be customizable, but I just need to change it from default to something else. Maybe there's some components or something else that I can use? Or maybe there's additional GUI library that I can download and add to the project?
You didn't provide us with many details on how you want to customize the form.
You can change colors used on the form by setting the BackColor and ForeColor properties.
You can also apply background image to the form and use the TransparencyKey property in order to create a form with an irregular shape. More information about this method can be found here: http://msdn.microsoft.com/en-us/library/6k15y9et.aspx
Doing this manually and trying to have something looking nice takes waaay too much time. For start, look at this thread:
Best Free Controls for .NET

visual styles independent drawing

Using C# winforms, i want to create custom controls that looks like the real ones.
There are a lot of classes that can be used to draw controls that looks like the real ones: ControlPaint, VisualStyleRenderer, ButtonRenderer, CheckBoxRenderer, ComboBoxRenderer, GroupBoxRenderer, ProgressBarRenderer, RadioButtonRenderer, ScrollBarRenderer, TabRenderer, TextBoxRenderer, TextRenderer, ToolStripProfessionalRenderer, ToolStripRenderer, ToolStripSystemRenderer, TrackBarRenderer.
The problems coming when considering visual styles: I want to be visual styles independent. Meaning: I dont care if user allowing visual-styles or not, i want it to work. if user enabled visual-styles, i want to draw it using visual-styles, otherwise i want to draw it without visual-styles.
By the MSDN documentation, the only classes that are visual-styles independent are ButtonRenderer, CheckBoxRenderer, GroupBoxRenderer, RadioButtonRenderer. That means that for all other cases i need to check myself if visual-styles enabled and use different code to draw the parts.
Suppose i want to draw a Tab control parts myself. TabRenderer class has all needed functionality for that, but it works only if user enabled visual styles. otherwise I need to use ControlPaint class to draw, but it uses completely different model, there is no ControlPaint.DrawTab() method or something like that, and i need to figure out what rectangle types i need to draw so it will look like a real tab. that`s annoying.
The built-in controls, including the Tab control, already have this functionality of drawing themselves with or without visual styles. Why doesn`t microsoft exposing this functionality for the custom control creators? Why are custom control creators should suffer?
It's a pain unfortunately, but that is the cost for writing custom controls. The way I have seen Microsoft do this in many of the WinForm controls, is to check Application.RenderWithVisualStyes, and Control.UseVisualStyleBackColor. Depending on those values they will paint the control accordingly.
A common pattern they use is to create internal classes called MyControlRenderer.cs. Inside this renderer is where the magic actually happens. If you look at the NET REF code, you will see that they also use VisualStyleRenderer class from System.Windows.Forms.VisualStyles namespace, and there is some actual paint code as well.
You will need to utilize a mix of these various classes and tools, and a mix of also doing your own work. Testing your control on a machine with, and without visual styles.
When I hope home from work I will post a bit more.

Categories