Converting a WinForms form to WPF - c#

I've written a media player application in C#, and I was wondering if there was any way to use WPF to change the border of the form without converting the whole project over to WPF. I'm asking this because I only have 2 and a half days to get the border changed, and I don't think converting the whole project over would be possible in that amount of time (I don't think 2 days would be long enough to learn the amount required for a port to WPF). Would it be simpler to try and change the border in C#?

First, remove the border of the form.
form.FormBorderStyle = FormBorderStyle.None;
Then, change the background of the form to an image you create that has a custom border in the image.
form.BackgroundImage = image;
This will get you where you need to be, in the short term. In the long run, you are better off biting the bullet and switching to WPF if this is a project you will be maintaining and adding features to.

Use a WindowsFormsHost to host your WinForms form in a WPF window/application. Then you can have a WPF border for the WindowsFormsHost control.
You will only need to know WPF at a hello-world level and all of the application logic can stay inside the WinForms form.

Related

How to use Visual Studio's C# Windows Forms Design View for a Scalable/Resizable Window?

I am attempting to create a Windows Forms Application that will have components that scale when the window is being resized. I am running into issues with the Form when attempting to resize it when the application is running. First of all, this is what it looks like in the editor at its Minimum Size:
Then I stretch it out at run time and it is even on both sides (after tinkering with the Fixed-Splitter position:
I run into more problems when attempting to put List Boxes in the blue and red panels. In design:
Stretched:
I want the list boxes to nicely fill most of the width of each side, but when I attempt to use the Anchor tags it gets messed up.
So to sum up: Why is the designer all asymmetrical compared to the finished product and how do I make the List Boxes fit and scale in width when the window is resized?
I am using VS17 if that helps! Thanks!
The anchors was always (I don't know why) littlebit broken. Use the composition of nested Panels and use the Dock and Padding properties instead of anchros.

Creating a picturebox/mapview type control with clickable rectangles and animated icons

I'm looking to add a 'mapview' type control to my project.
It must have a 'main map' image with clickable transparent rectangles with borders and icons/images that can be animated when an event occurs.
What would be the best way of achieving this using windows forms in C#?
My first thought was to use a picture box with other items on top of it but I might run into problems with transparency etc.
Are there any libraries or anything out there that would be able to achieve this?
No need for a library, really:
I would go for a regular doublebuffered Panel subclass or even a PictureBox subclass for the board/map along with a movable Label or Panel subclass fpr the rectangles/items.
Important: Make sure the Labels are not just 'put on top' of the PictureBox but really nested!! (lbl.Parent = pbox). Then transparency will work just fine..
Since PictueBox is not a 'container', to nest a control in it you need code. But since you probably want to create them dynamically this is not an issue anyway.
This assumes that the rectangles are not overlapping! For overlapping controls transparency in winforms will not work.
The clearer you understand the 'animate when event' part the easier the rest of the code will be..
Since you mention 'animation', a word of warning: Simple animation, especially in reponse to a user action is doable; for more classy animation you may run into the limits of winforms.

How do I fit/re-size Windows Form to any screen resolution?

I know this is duplicated question but I checked all other related question and their answers did not help ( the result still the same as shown in screenshot 2)
I am new to c# windows forms. As shown in screenshot1, I have Form1 with some controls and each group of controls were put in a panel. I designed the application in PC1 as shown in Screenshot1 which is fit the screen resolution and worked well.
My application was developed in 1366 x 768 screen resolution (as shown in Screenshot1) but when I run this application in different computer (PC2) with different monitor size and different screen resolution the form appeared too big and part of the application is missing or is out of the screen.
However I solved this issue using Anchors but another issues came up which is: the user control does not re-size itself ( as shown in screenshot2) and part of it is cut or went under panel1 . I do not know if the problem is related to user control or related to all controls in Form1 (they should resize themselves)
I even tried the following code but the result still the same:
this.WindowState = FormWindowState.Maximized;
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
Screen.PrimaryScreen.WorkingArea
I have been searching to solve this issue the whole day yesterday but I failed, Please help me with any idea/suggestion it might work. Thank you
If you are working with Windows Forms and you cannot switch to WPF then you will prefer to do all the design in the lowest resolution at which you must run.
In WinForms you are setting the Size of every element so they will not re size according to the app size. What they will do is to be distributed along the empty space (if you program them to do so) increasing the free space between them, that's all.
Another option are LayoutPanels as Sinatr said as they try to offer the WPF panel functionality.
By default in WinForms, all controls that you place on the form while designing it have a fixed size. If you don't do anything special, whatever size the controls are when you place them are the size that they are always going to have, no matter what machine you're running on.
As you've noticed, that isn't always going to give good results. The way you work around it is to use a fluid layout, with liberal use of the TableLayoutPanel and/or FlowLayoutPanel container controls, as well as the Anchor and Dock properties for individual child controls. If you take special care to lay out the controls on your form correctly, they can be dynamically resized and rearranged to fit the available screen space.
This code
this.WindowState = FormWindowState.Maximized;
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
Screen.PrimaryScreen.WorkingArea
doesn't do anything. The only thing you need there is the very first line. Once you make the form maximized, it will fill the entire screen. You don't need to force it by setting its Size or Location properties. In fact, those have no effect on a maximized form.
The problem is presumably that the controls placed on your form do not resize themselves automatically (as discussed above). If you had a big enough screen, you'd see empty space where the form was filling the screen but had no controls on it. You have the opposite problem: on a smaller screen, controls don't fit and therefore overlap each other.
It isn't a perfect situation. Even if you code up a perfect dynamic layout, if you try to run the application on a system with a screen that is significantly smaller than your design intended, you're going to end up with buttons that are too small to poke. That's why applications are not, in general, designed this way. A screenful of buttons is terrible UI. The only time this kind of design is acceptable is when you're designing for a touch screen UI, like a restaurant POS. And in that case, you already have a pretty good idea what size screens your clients are going to be using, since it's all specialty hardware.

How can i use canvas and draw on it in a Windows Form Application? C# [duplicate]

I want to create a Winform application which displays Datagrid, Charts on it. My requirement is, that the user should be able to position those elements according to his choice, like we drop text box, label in winform design view. I know that there is a Canvas class in WPF, but I want to achieve the same with Winforms.
Can I use the WPF Canvas in Winform? Or is there any other solution?
You can use a WPF Canvas in a Windows Forms application. Just place the Canvas inside of an ElementHost control.
I came across this code while searching for an answer, it comes from a Stephens' VB trainer example:
Dim canvas As New Bitmap(
picCanvas.ClientSize.Width,
picCanvas.ClientSize.Height)

I want resize form in C# with effect like fade?

I create a form and I want to resize it with animation like fade effect or anything else?
for example I resize my form with this code and it show with an effect?
this.Size = new System.Drawing.Size(300, 300);
thanks
WinForms is not built with animations in mind.
I spent considerable time trying to smoothly animate WinForms apps prior to WPF. It's very hard and the result is almost always less than what you hope for.
The following tutorial shows how to create a splash screen with transparency. You can apply that logic to your form:
http://www.vcskicks.com/splash-screen.php
If at all possible, consider embedding WPF controls in your WinForms application where you need such effects.
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-wpf-in-winforms

Categories

Resources