Automação da Interface do Usuário e Escala da Tela
Observação
Esta documentação destina.Os desenvolvedores do NET Framework que desejam usar o gerenciado UI Automation classes definidas na System.Windows.Automation namespace.As informações mais recentes sobre UI Automation, consulte API de automação do Windows: Automação da interface do usuário.
Windows Vistapermite que os usuários alterem a dots per inch (dpi) a configuração e a maior user interface (UI) elementos na tela pareçam maiores. Although this feature has long been available in Microsoft Windows, in previous versions the scaling had to be implemented by applications. In Windows Vista, the Desktop Window Manager performs default scaling for all applications that do not handle their own scaling. Cliente de automação da interface do usuário aplicativos devem considerar este recurso.
Este tópico contém as seguintes seções.
- Scaling in Windows Vista
- Scaling in UI Automation Clients
- Tópicos relacionados
Scaling in Windows Vista
O padrão dpi configuração é de 96, o que significa que 96 pixels ocupam uma largura ou altura de uma polegada de nocional. A medida exata de uma polegada"" depende do tamanho e a resolução física do monitor. For example, on a monitor 12 inches wide, at a horizontal resolution of 1280 pixels, a horizontal line of 96 pixels extends about 9/10 of an inch.
Changing the dpi setting is not the same as changing the screen resolution. With dpi scaling, the number of physical pixels on the screen remains the same. No entanto, o dimensionamento é aplicado para o tamanho e local do UI elementos. This scaling can be performed automatically by the Desktop Window Manager (DWM) for the desktop and for applications that do not explicitly ask not to be scaled.
In effect, when the user sets the scale factor to 120 dpi, a vertical or horizontal inch on the screen becomes bigger by 25 percent. All dimensions are scaled accordingly. The offset of an application window from the top and left edges of the screen increases by 25 percent. If application scaling is enabled and the application is not dpi-aware, the size of the window increases in the same proportion, along with the offsets and sizes of all UI elements it contains.
Observação
By default, the DWM does not perform scaling for non-dpi-aware applications when the user sets the dpi to 120, but does perform it when the dpi is set to a custom value of 144 or higher.However, the user can override the default behavior.
Screen scaling creates new challenges for applications that are concerned in any way with screen coordinates. A tela agora contém dois sistemas de coordenadas: física e lógica. The physical coordinates of a point are the actual offset in pixels from the top left of the origin. The logical coordinates are the offsets as they would be if the pixels themselves were scaled.
Suppose you design a dialog box with a button at coordinates (100, 48). When this dialog box is displayed at the default 96 dpi, the button is located at physical coordinates of (100, 48). At 120 dpi, it is located at physical coordinates of (125, 60). Mas as coordenadas lógicas são os mesmos em qualquer dpi definição: (100, 48).
Logical coordinates are important, because they make the behavior of the operating system and applications consistent regardless of the dpi setting. Por exemplo, Cursor.Position normalmente retorna as coordenadas lógico. If you move the cursor over an element in a dialog box, the same coordinates are returned regardless of the dpi setting. If you draw a control at (100, 100), it is drawn to those logical coordinates, and will occupy the same relative position at any dpi setting.
Scaling in UI Automation Clients
O UI Automation API não usa coordenadas lógico. The following methods and properties either return physical coordinates or take them as parameters.
By default, a UI Automation client application running in a non-96- dpi environment will not be able to obtain correct results from these methods and properties. For example, because the cursor position is in logical coordinates, the client cannot simply pass these coordinates to FromPoint to obtain the element that is under the cursor. In addition, the application will not be able to correctly place windows outside its client area.
The solution is in two parts.
First, make the client application dpi-aware. To do this, call the Win32 function SetProcessDPIAware at startup. In managed code, the following declaration makes this function available.
<System.Runtime.InteropServices.DllImport("user32.dll")> _ Friend Shared Function SetProcessDPIAware() As Boolean End Function
[System.Runtime.InteropServices.DllImport("user32.dll")] internal static extern bool SetProcessDPIAware();
This function makes the entire process dpi-aware, meaning that all windows that belong to the process are unscaled. In the Highlighter Sample, for instance, the four windows that make up the highlight rectangle are located at the physical coordinates obtained from UI Automation, not the logical coordinates. Se a amostra não foram dpi-reconhecimento, o realce poderia ser desenhado nas coordenadas lógicas na área de trabalho, o que resultaria na colocação incorreta em um não-96 - dpi ambiente.
To get cursor coordinates, call the Win32 function GetPhysicalCursorPos. The following example shows how to declare and use this function.
Structure CursorPoint Public X As Integer Public Y As Integer End Structure <System.Runtime.InteropServices.DllImport("user32.dll")> _ Friend Shared Function GetPhysicalCursorPos(ByRef lpPoint As CursorPoint) As Boolean End Function Private Function ShowUsage() As Boolean Dim cursorPos As New CursorPoint() Try Return GetPhysicalCursorPos(cursorPos) Catch e As EntryPointNotFoundException ' Not Windows Vista Return False End Try End Function
public struct CursorPoint { public int X; public int Y; } [System.Runtime.InteropServices.DllImport("user32.dll")] internal static extern bool GetPhysicalCursorPos(ref CursorPoint lpPoint); private bool ShowUsage() { CursorPoint cursorPos = new CursorPoint(); try { return GetPhysicalCursorPos(ref cursorPos); } catch (EntryPointNotFoundException) // Not Windows Vista { return false; } }
Cuidado |
---|
Do not use Cursor.Position.The behavior of this property outside client windows in a scaled environment is undefined. |
If your application performs direct cross-process communication with non- dpi-aware applications, you may have convert between logical and physical coordinates by using the Win32 functions PhysicalToLogicalPoint and LogicalToPhysicalPoint.