Windows 8 native custom control - c#

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.

Related

Custom Control in WPF as necessary as in Winforms?

During an interview, the company was asking about my use of custom controls in WPF. I have found with all of the power of the WPF way of creating a control (datatemplate, control template, styles,triggers etc... ) that having to write a custom control that overrides the OnRender method really hasn't been necessary. Later found out that most of their development has been in Winforms.
If coming at a control from a 100% WPF direction, how often is it necessary to write a customcontrol with OnRender overrides? The Winform approach is really not making use of the WPF composition technique of creating controls and it seemed like a question not based on much WPF knowledge.
Thanks
Harold
Good question (though a bit opinion-based) and no answers? Fixing.
If you are winforms-experienced developer, then thinking winform-way is still acceptable in wpf. For a while. This is where you may find self making mostly custom controls (containing xaml and code, or even without xaml). But the more you learn, the less you need that. Many many tasks can be completed in wpf simply because it is very flexibly. Every entity consist of something what can be customized: templates, styles, converters, behaviors or even plain event handling.
You can start with custom control and then find out what you don't really need it (or it can be downgraded to simple restyling).
When I started making first serious wpf project, there were 3 custom controls and they are still. Here is why.
Outlined TextBlock. Simply because you need custom OnRender (to build and draw geometry for outline).
Animated content. To apply transition animation when changing content. I could almost make it without custom control, but there is a problem - calculating animations logic when transitioning left-to-right, right-to-left, up-down or down-up. It's waaaay easy to have in one custom control. But possible with UserControl and view, not as pretty still.
Graph. Simply because it's too complicated to be presented with Visual and because of performance using gdi+ gives millions of points (hundered thousands figures) to be drawn within ms.
Conclusion: it's good and useful, though way less than it was in winforms (where you simply had no other option).

Pull image from BitmapCache WinRT

I have an application for the windows store that I need to make emulate the tabs like Internet Explorer. The problem is that I have one control and it won't load into both the AppBar and the main view.
Is there a way to setup the CacheMode in the constructor of the control and then pull a bitmap from the BitmapCache and load that in the tab view in the AppBar? Or am I going about this the wrong way. I don't want to use two different controls due to it being a hack I want this to work with one control.
I am writing this application in C# and XAML.
Any help is appreciated.
A control can only have one parent in all popular XAML technologies, so you can't put it in two places.
You can use RenderTargetBitmap to take a screenshot of a control and put it elsewhere in your layout, but there are plenty of problems with that
It uses additional memory to store the bitmap
You need to figure out when to take that screenshot - typically after all images load and after every layout or content update.
A bitmap is not interactive.
Your best bet might really be to have two copies of the same control. You can create a UserControl to encapsulate any functionality and properties and reuse that control in multiple places easily. If you reference an image by URI - the platform should automatically share the resources for the image used in multiple controls.

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?

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

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.

How can I create a button with an embedded close button

I am trying to create a panel which will have a set of "buttons" on it.
These buttons should have the following behaviour:
Appear similar to a tag (with
rounded edges)
Contain a red
cross to remove the filter/tag from
the panel, similar to the way internet
explorer tabs have an embedded cross to close the individual tab.
allow the user to click
on the tag and respond like a normal
button (as long as the click is not
in the red cross)
Number 1 is no problem, this is just appearance, however, regarding numbers 2 and 3, I am not sure if there is already code out there do to something similar...and I dont really want to reinvent the wheel if I can avoid it!
My question is: Does anyone know if there is something out there in infragistics which will do this simply, or will I need to write this myself by subclassing winform buttons?
Thanks in advance!
Is this new development or maintenance of an existing project?
If it is maintenance, you have a somewhat tougher time ahead. You'll implement a UserControl, probably segmented into two buttons. Use docking to get the behavior as correct as possible. The far right button would contain your cross image; the left (which would need to auto-expand as you resize the control) would contain your primary button behavior. Play with the visual styles until you get them right (EG, removing borders, etc).
If this is new development, and you haven't gotten too far into it, you might consider using Windows Presentation Framework (WPF) instead of WinForms. It will be easier to build the control and get it to look exactly how you want it. WPF includes an extremely powerful control compositing system which allows you to layer multiple controls on top of each other and have them work exactly as you'd expect, and it carries the added advantage of allowing full visual control out-of-the-box.
Either way, this is more work than dropping in an external component ... I've used Infragistics for years, and I can't think of anything they have which is comparable. The closest, but only if you're building an MDI application and these controls are for window navigation, is the Tabbed MDI window management tools -- and there, only the tabs (which replace window title bars) have this behavior.
I don't think that infragistics can do something like this. The UltraButton control can't.
Implementing a own control wouldn't be that hard.
your probably going to have to make a costume control for this type of work.

Categories