简体   繁体   English

在Visual Studio 2008 for .NET CF中处理不同的解决方案

[英]Handling Different Resolutions in Visual Studio 2008 for .NET CF

I am developing a .NET CF based Graphics Application, my project involves a lot of drawing images, We have decided to go for porting the application on different handset resolution.(240 X 240 , 480 X 640) etc. 我正在开发一个基于.NET CF的图形应用程序,我的项目涉及大量的绘图图像,我们决定在不同的手机分辨率上移植应用程序。(240 X 240,480 X 640)等。

How would i go onto achieve this within single solution/project? 我将如何在单一解决方案/项目中实现这一目标?

Is there a need to create different projects based on resolutions? 是否需要根据分辨率创建不同的项目? How would i handle common files? 我如何处理常见文件? and i need the changes in one of the common classes to occur across all devices. 我需要在所有设备之间进行其中一个公共类的更改。

Thank you, Cronos 谢谢,Cronos

Don't listen to that idiot MusiGenesis. 不要听那个白痴MusiGenesis。 A much better way of handling different screen resolutions for Windows Mobile devices is to use forms inheritance , which can be tacked onto an existing CF application with minimal effort. 处理Windows Mobile设备的不同屏幕分辨率的更好方法是使用表单继承 ,可以轻松地将其添加到现有CF应用程序中。

Basically, you design each form for a standard 240x320 screen. 基本上,您为标准240x320屏幕设计每个表单。 When you need to re-arrange a form for a new resolution (let's say 240x240), you add a new form to your project and have it inherit from your original 240x320 form: 当您需要为新分辨率重新安排表单时(例如240x240),您可以向项目添加新表单并使其继承自原始240x320表单:

public partial class frmDialog240x240: frmDialog

instead of just Form: 而不只是形式:

public partial class frmDialog240x240: Form

like usual. 像往常一样。 On your original form, you need to set the Modifiers property of each control to Protected (instead of the default Private). 在原始表单上,您需要将每个控件的Modifiers属性设置为Protected (而不是默认的Private)。 In the designer for your new form, you will see all of the controls on the form you're inheriting from, and you can move them and resize them as you see fit to accomodate the new screen dimensions (this will not affect the original form's layout). 在新表单的设计器中,您将看到您继承的表单上的所有控件,您可以移动它们并根据您的需要调整它们以适应新的屏幕尺寸(这不会影响原始表单的尺寸)布局)。

When your program is running, it's easy for it to check the screen resolution of the device it's running on and create the appropriate form (a factory method is good for this). 当你的程序运行时,它很容易检查它运行的设备的屏幕分辨率并创建适当的表格(工厂方法对此有好处)。 Your new form inherits everything from the old form, but uses your new custom layout. 您的新表单从旧表单继承了所有内容,但使用了新的自定义布局。

This approach allows you to avoid code duplication, because there isn't any. 这种方法可以避免代码重复,因为没有代码重复。

Anchoring and Docking is the most common mechanism for handling different resolutions (remember also that many devices can rotate the screen, so you need to handle changes even on a single device). 锚定和对接是处理不同分辨率的最常用机制(还要记住,许多设备可以旋转屏幕,因此您甚至需要在单个设备上处理更改)。 Getting screen size, if needed after that, is as simple as querying the Screen object: 如果需要,获取屏幕大小就像查询Screen对象一样简单:

int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int workingHeight = Screen.PrimaryScreen.WorkingArea.Height;

This code has worked for me in determining the resolution of the screen dynamically: 这段代码对我有用,可以动态确定屏幕的分辨率:

[DllImport("coredll.dll", EntryPoint = ("GetSystemMetrics"))]
public static extern int GetSystemMetrics(int nIndex);

private const int SM_CXSCREEN = 0;
private const int SM_CYSCREEN = 1;

private int width = GetSystemMetrics(SM_CXSCREEN);
private int height = GetSystemMetrics(SM_CYSCREEN);

I don't remember it out of the top of my head, but there is also a way to get the Screen Orientation. 我不记得它的顶部,但也有一种方法来获得屏幕方向。 These would help merge some code in a single Class. 这些将有助于合并单个类中的一些代码。

I would strongly recommend to create a single solution for all resolutions. 我强烈建议为所有分辨率创建单一解决方案。 You could have as many projects as you want under these solution. 在这些解决方案下,您可以拥有任意数量的项目。 These projects could be Windows Forms Applications, Dll Library projects or Set Up Projects. 这些项目可以是Windows窗体应用程序,DLL库项目或设置项目。

I would aim to create a single Windows Forms Application Project. 我的目标是创建一个Windows窗体应用程序项目。 I would use the above technique and read the static images from the File System. 我将使用上述技术并从文件系统中读取静态图像。 If this doesn't work for you and/or you prefer to read your images as resources, then create an 'engine' dll project, containing all the code that is common to all resolutions. 如果这对您不起作用和/或您更喜欢将图像作为资源读取,则创建一个“引擎”dll项目,其中包含所有分辨率共有的所有代码。 Add then the output of this project as reference to as many Windows Forms Applications projects as you need. 然后将此项目的输出添加为您需要的任意数量的Windows窗体应用程序项目的引用。

There is no simple answer to this question. 这个问题没有简单的答案。 Designing forms to handle different screen resolutions is easy to do in Windows where even the smallest screen size is usually 600 x 800. In the CF world, the screen can be as small as 240 x 240 (or smaller for smartphones) or as large as 640 x 480 (or larger). 设计表单以处理不同的屏幕分辨率很容易在Windows中进行,即使最小的屏幕尺寸通常为600 x 800.在CF世界中,屏幕可以小到240 x 240(或更小的智能手机)或大到640 x 480(或更大)。 Anchoring and Docking tend to work very poorly over these size ranges. 在这些尺寸范围内,锚定和对接往往效果很差。

One option is to use a least-common-denominator approach and design everything to fit on the smallest possible screen. 一种选择是使用最小公分母方法并设计所有内容以适应尽可能小的屏幕。 This is guaranteed to work on every device, but obviously will waste useful space on larger screens. 这保证可以在每台设备上运行,但显然会在较大的屏幕上浪费有用的空间。

Another option is to design each form to be resolution-aware. 另一种选择是将每个表单设计为分辨率感知。 For example, if you have a form that displays data in a grid and a filter combobox above it, you can write code in the form's Resize event that adjusts the dimensions of the grid to fill whatever space is available. 例如,如果您有一个在网格中显示数据的表单和在其上方的过滤器组合框,您可以在表单的Resize事件中编写代码,该事件调整网格的尺寸以填充可用的空间。

An alternative option to resolution-aware code is to create a separate form for each resolution for a particular UI function. 分辨率感知代码的另一种选择是为特定UI功能的每个分辨率创建单独的表单。 This is more work and leads to code duplication, of course, but for some particular functions this is just something you have to do. 当然,这是更多工作并导致代码重复,但对于某些特定功能,这只是您必须要做的事情。 As long as you abstract out the common logic, you'll be fine. 只要你抽象出共同的逻辑,你就没事了。

You have the right idea about wanting to achieve this within a single project. 您有正确的想法想要在单个项目中实现这一目标。 Different screen resolutions can usually be handled with a little bit of work and thought, and are definitely not grounds for splitting your product into different projects. 通常可以通过一些工作和思考来处理不同的屏幕分辨率,并且绝对不是将您的产品分成不同项目的理由。

I gave up on the designer for all but the most basic compact applications. 除了最基本的紧凑应用之外,我放弃了设计师。 I try to place every control mathematically using the screen dimensions. 我尝试使用屏幕尺寸以数学方式放置每个控件。 It sounds painful but once you get going it becomes second nature. 这听起来很痛苦但是一旦你开始它就变成了第二天性。

For every form I create a 'regenerate' method which is fired whenever the form is shown or resized. 对于每个表单,我创建一个'regenerate'方法,只要表单显示或调整大小,就会触发该方法。 In that method I generally set location, size and, if necessary, font size for each control on the form. 在该方法中,我通常为表单上的每个控件设置位置,大小和(如果需要)字体大小。 When placing the control, my thought process is: 放置控件时,我的思维过程是:

  1. OK for regular portrait device? 普通人像设备好吗?
  2. OK for regular landscape device? 对常规景观设备好吗?
  3. OK for square device? 方形设备好吗?
  4. OK for VGA (640x480) (high DPI) device? 适用于VGA(640x480)(高DPI)设备?

For font sizes and dealing with high DPI (Dots Per Inch) VGA devices, I use the property -: 对于字体大小和处理高DPI(每英寸点数)VGA设备,我使用属性 - :

CurrentAutoScaleDimensions.Height / 96 CurrentAutoScaleDimensions.Height / 96

...to produce a font-scaling factor. ...生成字体缩放因子。 (Regular devices are 96 DPI) (常规设备为96 DPI)

As a last resort, you can use conditional (if) statements in your regenerate code to test for various screen sizes/shapes. 作为最后的手段,您可以在再生代码中使用条件(if)语句来测试各种屏幕尺寸/形状。

It's still possible to use the designer for basic layout but you'll need to run the forms on various emulators/devices and try switching portrait/landscape to fully test the 'regenerate' methods. 仍然可以使用设计器进行基本布局,但是您需要在各种仿真器/设备上运行表单,并尝试切换纵向/横向以完全测试“再生”方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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