C # development Wpf/silverlight animation and games series Tutorials (Game Course): (23)

Source: Internet
Author: User
Tags silverlight

C # development Wpf/silverlight animation and games series Tutorials (Game Course): (23) Adaptive windowing and Full-screen (WPF only)

As mentioned in the previous section, one of the criteria for testing system architecture is the expansion of the system. In the. NET Web site application, an excellent architecture can be converted from one database to another, easily docking with different banking interfaces, and can be integrated into a variety of plug-ins at will, and the realization of these only requires a small part of the operation; Similarly, in game design, Windowing and Full-screen Adaptive Perfect switch is also a serious test of the rationality of the game structure, Are you ready?

There are two ways to switch between game windowing and Full-screen, the first one is to enlarge and shrink the area of the visible range without scaling all object dimensions. This approach is very simple to implement in this game design, we first add a button as a Test button, and then add the Changewindowmode event for it, then define four variables to record the size of the full screen and the size of the window: double screenwidth, ScreenHeight, WindowWidth, WindowHeight, and to assign values to them in the game initialization:

private void InitializeGameSetting() {

 //设置尺寸

 ScreenWidth = SystemParameters.PrimaryScreenWidth;

 ScreenHeight = SystemParameters.PrimaryScreenHeight;

 WindowWidth = 800;

 WindowHeight = ScreenHeight * WindowWidth / ScreenWidth; //根据屏幕分辨率计算出窗口模式下高度

 ……

}

The first three properties are well understood, and the fourth windowheight Why do I have to use formulas to calculate the value instead of just taking 600来? This involves the Windows Adaptive User computer resolution problem. If your computer is 4:3 types of resolution, such as 800*600, 1024*768, 1280*960, such as the traditional resolution, you can directly set up windowwidth=800, windowheight= 600. However, if the user's computer is widescreen (such as 16:9, etc.), setting the window mode size to 800*600 will result in Full-screen switching errors. The general network game will give several or more optional resolution to allow users to set the window/Full-screen switching, the implementation of these switches based on the system resolution and refresh rate of changes based on; and WPF is much simpler to implement, No need to call WINDOWSAPI to change the system settings, but directly by modifying the form's own properties WindowStyle and WindowState easily implemented, as follows:

private void ChangeWindowMode(object sender, RoutedEventArgs e) {

Button button = e.Source as Button;

string mode = button.Content.ToString();

if (mode == "全屏") {

 this.WindowStyle = WindowStyle.None;

 this.WindowState = WindowState.Maximized;

 button.Content = "窗口";

} else if (mode == "窗口") {

 this.WindowStyle = WindowStyle.SingleBorderWindow;

 this.WindowState = WindowState.Normal;

 button.Content = "全屏";

}

}

When you need to switch full-screen, All we need to do is remove the title bar and border of the window (Windowstyle.none) and set the window mode to be maximized (windowstate.maximized), and if you need to make the game windowed, simply set the window mode to a single border window (Windowstyle.singleb Orderwindow) and restores the window (windowstate.normal). The effect of windowing and full-screen through this method is shown as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.