I have wpf project with one Window (MainWindow). Depending upon the config file it shows one of two UserControl's as Content. It may be a horizontal (1920x1080) control or vertical (1080x1920) control. It's fine with horizontal screen, but when vertical is loaded I would like to do:
1) rotate window/control by 270 degrees
2) change primary screen orientation
I would prefer to just rotate application and don't interact with windows API. I can't change orientation manually, because I have only remote access to this computer.
You can not rotate the Window object itself, as it is positioned by the window management system built in Windows. You can, however, transform (and thus rotate) any FrameworkElement inside the window. This includes, but is not limited to, the Grid, the Button and the TextBox elements.
All you need to do is edit the LayoutTransform property on the element you want to rotate, which is most likely the root element in your window. Set the rotation to 270/-90 degrees and WPF will automatically rotate your UI.
Because you are using the LayoutTransform property, the layout system will also scale you UI correctly. The RenderTransform property causes the control to first be rendered, then be rotated.
YES WE CAN CHANGE SCCREEN ORIENTATION USING
DEVMODE & using System.Runtime.InteropServices;
its bit late to reply but I am replaying for the new ones , if someone com across this article for change screen rotation in C# or VB .
Please use the link given below to get help Mr. Hannes Completely write an article to change screen rotation and luckily its working fine for me (Windows 11) as now of..
https://www.codeguru.com/dotnet/creating-a-screen-rotator-in-net/
Let's suppose we have the designed the layout of some WPF application to be used on standard Full HD screen 1920x1080. Then we need to rotate the screen and install it in a box that is mounted on kiosk PC but in Portrait orientation.
I need to find a way on how to rotate the screen easily or at least in some more elegant way.
I tried to use use RenderTransform and RotateTransform applied to the contents of the window but this rotates the image and of course not the layout.
The controls remain of the same width and height.
Is there a way to do it automatically or should I take each control and change it properties one by one ?
The problem is present for TextBlocks and TextBoxes. They are intended to be used horizontally. You can rotate it but the layout is calculated based to it's horizontal width.
BTW. Rotation of the entire window is not allowed. It throws an exception.
It looks like that I have found the solution myself. If we choose the Layout transform instead of RenderTransform then the visual system does the arrangement and measurement of the layout automatically before the rendering.
The WPF framework does the job in this order
LayoutTransform
Measure
Arrange
RenderTransform
Render
This is best described here LAYOUTTRANSFORM VS. RENDERTRANSFORM - WHAT'S THE DIFFERENCE?
By default map viewer responds to map click with dragging map around viewer. I'd like to create functionality wher user can click, draw rectangle and map zooms into this rectangle.
Is this supported by map or is there any way to achieve such behavior?
Adding Shapes to bing map
and after adding the shape you can change the zoom level of the map
We are using WPF Bing Maps to visiualize clustered data. We are trying to update data on every OnViewChangeOnFrame event (because there is a significant delay in OnViewChangeEnd). However zooming and updating on every frame leads to "jerking" effect. The idea is to find out the target boundary rectangle OnViewChangeStart and update data only once in the beginning of zoom or pan for the target settings.
However there is a problem Bing Maps - Map class does not support TargetBoundingRectangle property. Is there an algorithm knowing viewportsize, target zoomlevel and target center to calculate TargetBoundingRectangle or maybe another Map property which has it?
Thanks!
Got an answer from MSDN forums.
WPF Bing Maps does not provide target boundary property right now.
I would like to understand the general requirements for WPF/Silverlight layout for making it possible to implement pan&zoom (drag and zoom) features. I don't mean pan&zoom for an image but for a total page (window) layout (or part of it) with some controls.
What features of the layout and what features of used custom controls make layout fixed and pan&zoom impossible?
General rule
With few exceptions, everything in WPF can be panned, zoomed, rotated, stretched, etc to your heart's content. This include single controls like Button, compound controls like ListBox, and containers like StackPanel.
The exceptions
Here are the exceptions:
If you are using Adorner and your AdornerDecorator is outside the panned/zoomed area, then the Adorners attached to your panned/zoomed area will pan but not zoom. The solution is to put an additional AdornerDecorator inside the panned/zoomed area.
If you use a Popup, it will display at the panned/zoomed location of its PlacementTarget but it will not itself be scaled. It will also not move as you pan the area containing its PlacementTarget (basically it sits in its own surface above the target control). To get around this, use a zero-size Canvas with high Z order instead when you want something to pop up within the zoom/pan area.
Any ContextMenu you define will be shown inside a popup, so the menu items will display normal size even when the area you clicked on is zoomed in or out. Because of the nature of a context menu, this is probably desirable behavior. If not, you can wrap the menu items in a ViewBox and tie the zoom to your main area's zoom.
Your ToolTips will display normal size even if the UI is panned or zoomed. Same solution as for ContextMenu.
If you used WinForms integration to integrated legacy WinForms controls and UI, they will not properly pan, zoom and clip in certain situations. There is an advanced technique for working around this, where you implement the WinForms control off-screen, then using BitBlt or similar copy the image into your window as an image, and forward mouse clicks and keystrokes to the offscreen window. This is a lot of work, though.
If you bypass WPF and directly use GDI+ or DirectX, or use Win32 hWnds to display content or UI, that content or UI will not be properly panned, zoomed or clipped to the window unless you do it yourself in your interface code.
Final notes
A good WPF UI always uses panels like Grid, DockPanel, etc to lay out controls in a flexible manner so they automatically adjust to container sizes, rather than using fixed sizes and positions. This is also true for the internal contents of your pan/zoom area as well, BUT there is an exception to this rule: the outermost element in your pan/zoom area must have a specified size. Otherwise what will define the area being panned/zoomed over?
The easy way to implement pan/zoom capabilities is to adjust the RenderTransform of the outermost control in your pan/zoom area. There are many different ways to implement controls for panning and zooming, for example you could use toolbar buttons and sliders, scroll bars, mouse wheel, spacebar+drag to pan, draggable areas of panned UI itself, or any combination of these. Whichever interface you choose, just have it update the RenderTransform appropriately from the code-behind and you're good to go.
If your chosen panning mechanism is scroll bars, you might want to use a ScrollViewer and only use the RenderTransform for the zoom.
Be sure you set clipping on the pan/zoom area. Otherwise if you zoom in or pan items off the side, they will still be visible outside the pan/zoom area.
Use a MultiScaleImage or Canvas area, and place everything you need to pan and zoom in it
<Canvas x:Name="panZoomPanel" Background="Transparent">
</Canvas>
In code use make a TranslateTransform and a ScaleTransform in a TransformGroup to pan and zoom
Check out other SO post or this example or this one
In general you can treat any composite set of UI elements the same as you would treat a single UIElement so the case of an image isn't really different than doing the same for an entire application. The best way to handle zooming based on user input (as opposed to automatic scaling that Viewbox does) is applying a ScaleTransform. This can be set on a high level parent element, like a Grid at the root of a Window layout. For panning you can combine in a TranslateTransform or in some cases use a ScrollViewer to handle moving the view of the content.
One really easy way of implementing zoom in XAML is to use a Silverlight ViewBox. This zooms the XAML not the pixels. You can specify the stretch to use and the ViewBox will scale based on this (Fill, None, Uniform etc). There are some great Viewbox blog posts on the web if you search for Silverlight+Viewbox on Google.
The panning is easily accomplished with a similar mechanism to drag and drop and there are also numerous how-to blog posts on this, available via Google. Just amounts to capturing MouseDown, MouseMove and MouseUp events.