Make full screen wpf app look the same on all resolutions - c#

I want to make a full screen WPF app that looks the same on all machines with different screen resoltion. I created my MainWindow.xaml with a 800*480 px resoultion. I made a menu on the top of the window like this:
<Grid Height="480" Width="800">
<Menu FontSize="25" Margin="0,0,0,442" >
<MenuItem Header="File" />
</Menu>
</Grid>
But when I started the app in Debug Mode, the menu was in the center of the screen. I guess it was because my screen resoultion is 1366*768 px.
So what should I do to make my program look the same on different resoultions in full screnn mode?
UPDATE: I want it to be like Photoshop for example. Photoshop looks almost the same on different resoultions. Images:
http://i.stack.imgur.com/W1SL6.png
http://i.stack.imgur.com/7KYxX.png
UPDATE : I just want to know what these values should be to make the program work like I want it to:
Height of Window,
Width of Window,
Height of Grid,
Width of Grid,
Sorry bros, I'm such a beginner :\

Another method is to use ViewBox:
<Viewbox StretchDirection="Both" Stretch="Uniform" />
For example:
<Window
Height="480"
Width="800">
<Viewbox StretchDirection="Both" Stretch="Uniform">
<Grid Height="480" Width="800">
</Grid>
</ViewBox>
</Window>
Now when you resize your window, all elements resize too.

It's not really going to be possible to make an application look exactly the same in every resolution. One of the problems with this is text - it's difficult to scale text in the same sense as you can a button, or a ListBox, or whatever.
But, one of the things you can do to make it so that your application looks substantially similar is to use relative positioning and sizing rather than absolute, as you are now.
I've rarely found it's useful or successful to use margins like the one you have above, where the Menu is offset by 400-some pixels. Typically, this ends up making your design look good only at the exact size at which you designed them. If you want this at the top of the control, you could do the following:
<Menu ... VerticalAlignment="Top" ...>
This would always have this menu aligned to the top of your Window. Likewise, if you wanted the menu to take up the same amount of relative vertical space in the window regardless of the absolute size of the window, you could use the Grid.RowDefinitions property:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="10*"/>
<RowDefinition Height="90*"/>
</Grid.RowDefinitions>
<Menu Grid.Row=0 />
</Grid>
This way, the Menu occupies the entire top row, and will consume 10% of the vertical space in the window regardless of resizing. There are some edge cases, obviously, particularly when controls get resized to the point where the text they contain cannot be seen anymore - if these are concerns you should use MinHeight and MinWidth on your Window or a specific control in order to provide a floor at which the control in question doesn't shrink anymore.
Note that, if you don't explicitly set the size of the Grid, it fills the entirety of its parent container by default - the entirety of the Window in this case. If you want your application to look the same, you can't give the parent Grid an absolute size like 800x480.

Related

How do you get XAML elements to scale to fit their containers?

I understand that there are a variety of ways to size child elements according to parent elements. If you're using a grid for example, you can use row and column definitions and you get lots of freedom regarding automatic sizes or fixed sizes or "star" sizes. However, if the child elements themselves have a fixed width and height then it won't matter if the parent tells the child to fill all available space. The child element will remain the same size.
I have a window that was designed to always display its contents at the same pixel dimensions no matter what size the window is resized to. Rather than go and change every single child element in every XAML page so that it doesn't have a fixed size, I'd like to get the main Grid to just scale to fit the window. So far the only way to get elements with fixed dimensions to display at different sizes is to use Transform scaling, either with a RenderTransform or a LayoutTransform. But if I go that route, I'll have to code the scaling in C# to respond to resizing events rather than have it happen automatically. Is there some native builtin way to do this in XAML? This feels like the kind of thing I should be able to do with some special property, or perhaps a ContentControl or ContentPresenter.
I've seen Resize WPF Window and contents depening on screen resolution but it's asking about conventional resizing and not scaling fixed elements. I've also seen How to make all controls resize accordingly proportionally when window is maximized? and that has the same problem though the second answer at least talks about handling resizing events.
Here's a simplified example of a fixed-dimension child element not resizing as desired:
<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Window1" Height="200" Width="300" Background="LightBlue">
<Grid>
<Frame Background="Blue" Width="200" Height="100">
</Frame>
</Grid>
</Window>
Actual results:
Desired results:
As you can see, what I'm looking for is a sort of letterboxing effect, meaning I want the aspect ratio to be maintained. However, I haven't found a way to get automatic scaling even without worrying about the aspect ratio, so I thought I'd consider the letterboxing as a sort of second phase that I'd worry about later.
The control you are looking for is a Viewbox. It grows to fill its container (you can set the stretch style for the letterboxing) and scales all its contents accordingly. Just make it the root element of your application (or whatever you want stretched):
<Viewbox>
<Grid> //Or whatever
<OtherStuff>
</Grid>
</Viewbox>
Note that because the viewbox is scaling its contents traditional Grid behavior and similar will stop working since the size of the content never actually changes.
Another option is to use a MultiValueConverter to set the height and width.
You could give the Converter the ActualWidth and ActualHeight of the root container als parameters and let it calculate the needed aspect ratio.
A tutorial which describes the MultiValueConverter:
http://www.wpftricks.com/2017/05/wpf-multivalueconverter.html

Border around screen C#

I'm developing an application and I want to display a colored border (not into a window) around the screen (Skype-like). I've searched all the day on the Internet but I didn't find anything.
EDIT
I've already tried this:
<Border BorderThickness="10" BorderBrush="Red">
into my XAML and it displays the border around the window, but I want it around the whole screen even if I minimize the window.
EDIT:
I don't want to have a border for my app but a red border around all the screen, i think there must exist a windows API that allows it.
you can do something like this
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" WindowStyle="None" ResizeMode="NoResize">
<Border BorderThickness="2" BorderBrush="Blue" >
<Grid>
</Grid>
</Border>
</Window>
this will give u
When your window is minimized, it's minimal in size by definition. What you want is actually a maximized window when it is known as minimized by the OS?!
go to a big window size (equal the maximized state) when you handle the minimized event.
thin out your app window that only the border is visible, leaving the inner space for the desktop. To give you an idea what the idea is: click here (just an example to give the unusual shape) In your case, it would be a thin line - rectangulare shape.
us this, but change the color and thickness if you wish. If this wasn't what you were asking please develop your question further and give examples.
<Border BorderBrush="Black" BorderThickness="2">

Why is the image only partially visible inside my Button?

I am developing a Windows Store app and I've done this before, months ago, but all of a sudden, in this new app, I can't get the image to display inside the button (properly).
<Button x:Name="ShowView" Grid.Column="1" Width="32" Height="32" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,61,20,33">
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Image x:Name="ShowViewImage" Source="/Assets/ShowView.png" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</StackPanel>
</Button>
As you can see, the code is fine (unless things have changed drastically, which by the looks of it they haven't). So what gives? This is the only code I have so far in my XAML file other than the defauls that VS generates as it's a new Project.
P.S. I've also tried taking out the StackPanel and just having Button > Image, but this produces the same result.
So, when the BUtton displays at runtime, all I can see is a very tiny, 2pixels of the image (but the image is actually 32x32pixels. How do I properly display an "Image Button"?
The problem is that your Width and Height for the button are far too small. You've made it 32x32 pixels, but the button will use almost all of that itself for the space it leaves around the visible border, the border itself, and the padding between the border and the button's content.
(It leaves space around the edge to provide a larger hit target than the visible appearance. This is useful on touchscreens, where accurate finger placement is difficult.)
All that's left for your image is a few pixels.
You'll need to make the button about 62x52 pixels to leave enough space in the middle for a 32x32 pixel bitmap.
You could get away with a slightly smaller button if you explicitly set smaller Margin and Padding properties, although as mentioned above, the margin is there for a reason.
You have a couple options, the Padding property for instance is Template bound with some pre-set padding added to it. So with your Button having a fixed Height and Width set to 32 something as simple as setting Padding="0" could fix it for you depending on the actual size of your Image.
If worse comes to worse though, you could always just make your own Button Template. There's a couple easy ways to do this. One of which would be just go make a copy of the default Button Template, rip out all the Padding/Margin/Height/Width crap preset in there and just change its name then apply your new template directly to your button like;
<Button x:Name="ShowView" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,61,20,33"
Style="{StaticResource YourCustomButtonTemplateForImages}">
<Image x:Name="ShowViewImage" Source="/Assets/ShowView.png"/>
</Button>
Or... another option would be, embed your Image inside of a ViewBox inside your button and it will fit and re-scale itself accordingly to its set available size.
Oh, you might also want to make your Background="Transparent" while you're at it to make it look a little cleaner as just an image.
Hope this helps.

Image-Shaped Window in WPF

I want to have a WPF-Window that is shaped like an image. So basically I have an image that contains transparency and at the transparent places I just want to see through the window to my desktop. In the non-transparent places I want to see my image. Similar to what is done here, but in a way that is simple and understandable(in the example I linked there is LOTS of XAML and I don't understand a word).
Much of the XAML you see in that example is stuff unrelated to just showing a window that only shows the image. The rest of it is basically handling the minimize/maximize/close buttons.
To get you started, the below is all you really need to show a window that just has your image. You can then go from here and add buttons and things as needed, using that example XAML from the article to lead you on.
<Window x:Class="CSWPFIrregularShapeWindow.PictureBasedWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="PictureBasedWindow" Height="300" Width="300" Background="Transparent"
WindowStyle="None" AllowsTransparency="True" >
<Grid>
<Image Height="250" Name="test" Source="/CSWPFIrregularShapeWindow;component/Images/transparentPic.png"
Stretch="Fill" />
</Grid>
</Window>

Draggable and Auto-Resized UserControls in WPF Window

I'm making a WPF Hud, and I'm not such a WPF expert.
I need a Window with draggable usercontrols and which are auto-resized when I expand/reduce the Window.
I can achieve the first feature by using a Canvas container and using MouseMove event of the Window.
I can achieve the second feature by using a Grid container and setting childrens both properties HorizontalAlignment VerticalAlignment to Strech.
Of course I cannot use both containers, so is there a third container that can help me on doing such what requested?
Actually I'm trying to use the Canvas and determining the width/height of the childrens manually, but I don't like this approach.
Any suggestion?
Thanks,
Luca
Leaving out the details on dragging controls, etc., try something like:
<Window x:Class="AdHocWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="439" Width="616"
Title="MainWindow">
<Viewbox>
<Canvas Width="600" Height="400" Background="Blue">
<Rectangle Stroke="Orange" Width="200" Height="100" Canvas.Left="10" Canvas.Top="5"/>
<Ellipse Stroke="Yellow" Width="350" Height="200" Canvas.Left="225" Canvas.Top="150"/>
</Canvas>
</Viewbox>
</Window>
This will create a Canvas object with an initial size of 600x400. When you resize the window the canvas and the objects within it will be scaled so that they take up the same amount of relative space. You'll need to size the containing Window appropriately as well...normal resizable windows have 39 pixels of chrome vertically and 16 pixels horizontally, so take that into account.

Categories

Resources