简体   繁体   中英

how to change screen resolution when application launches?

i am working on a POS applciation with WPF,C#, SQLCE , VS 2010 . I created a bill entry form with fixed window of 800 * 600 width and height.

but other windows in the project are made to work with system resolutions. now i want to change the system resolution to 800*600 when this bill entry form opens in My POS application . For reporting and maintenance of Stocks & Inventory i used rich UI Controls with Window size maximazed but for BILL ENTRY FORM ( considering my needs) i kept its window size to 800*600 . when i open the form it looks very small in 1440*900 resolution. so i want to change system resolution to 800*600 whn this window opens and when this window closes it should return back to 1400*900 resolution .. is there any way to do this

You don't need to change the desktop resolution. WPF is resolution independent and can scale everything the way you need it. It will look much better too.

For simple scaling you can use the Viewbox class:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="800" Height="600">
    <Viewbox>
        <ListView Height="150" Width="200">
            <ListViewItem>item 1</ListViewItem>
            <ListViewItem>item 2</ListViewItem>
        </ListView>
    </Viewbox>
</Window>

It will automatically scale whatever is inside to whatever size is avalibale:

小

中间

大

It's not a good practice to programmatically change a users's settings - instead you should display a message to the user asking them to change the screen resolution themselves. If you really require a particular reolution, you can cancel the application load until they have changed it to the required setting.

I strongly recommend Not to Do this

But for your problem ... Use this function ChangeDisplaySettingsEx from GDI(not easy, but have no builtin C# function for this) have a look here http://msdn.microsoft.com/en-us/library/dd183413%28VS.85%29.aspx

You need to do a DLL import of "User32.dll" to use this function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM