Lukas
Lukas

Reputation: 613

Rotate WPF control or change screen orientation

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.

Upvotes: 0

Views: 2145

Answers (2)

Tahir Rehman
Tahir Rehman

Reputation: 332

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/

Upvotes: 0

Paul
Paul

Reputation: 188

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.

Upvotes: 1

Related Questions