Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / UIAutomation / Win32Providers / MS / Internal / AutomationProxies / WindowsFormsHelpers.cs / 1 / WindowsFormsHelpers.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description:
// This proxy is the base class for all proxies that support Windows
// Forms controls.
// All generic Windows Forms functionality goes here.
//
// History:
// 07/01/2003 : a-jeanp Created
//---------------------------------------------------------------------------
using System;
using System.Text;
using System.Collections;
using System.Windows.Automation;
using System.Windows.Automation.Provider;
using System.Runtime.InteropServices;
using MS.Win32;
namespace MS.Internal.AutomationProxies
{
// Helper static class used by the Win32 proxies to get Winforms information
static class WindowsFormsHelper
{
#region Proxy Create
// Static Create method called by UIAutomation to create proxies for Winforms controls.
// returns null if unsuccessful
internal static IRawElementProviderSimple Create(IntPtr hwnd, int idChild, int idObject)
{
// Currently there is an issue with CLR remoting that causes Accessible.CreateNativeFromEvent() to fail
// for Winforms controls. Until that is resolved use AccessibleObjectFromWindow() instead. It will
// return a Native IAccessble and not a OleAcc implementaion. Winforms does provide a Native IAccessible.
Accessible acc = null;
if (Accessible.AccessibleObjectFromWindow(hwnd, idObject, ref acc) != NativeMethods.S_OK || acc == null)
{
return null;
}
switch (acc.Role)
{
// ===========================================================
// WinformsSpinner controls are not identifiable by classname or
// other simple properties. The following case calls the
// WinformsSpinner constructor which in turn tries to establish
// the class of the control as a fact or returns null.
case AccessibleRole.Combobox:
return WinformsSpinner.Create( hwnd, idChild, idObject );
// ===========================================================
case AccessibleRole.SpinButton:
return WindowsUpDown.Create( hwnd, idChild, idObject );
case AccessibleRole.Grouping:
return new WindowsButton(hwnd, null, WindowsButton.ButtonType.GroupBox, Misc.GetWindowStyle(hwnd) & NativeMethods.BS_TYPEMASK, acc);
case AccessibleRole.StatusBar:
WindowsStatusBar sb = new WindowsStatusBar(hwnd, null, 0, acc);
if (sb == null)
{
return null;
}
return idChild == NativeMethods.CHILD_SELF ? sb : sb.CreateStatusBarPane(idChild);
default:
break;
}
return null;
}
// Static Create method called by UIAutomation to create a Button proxy for Winforms Buttons.
// returns null if unsuccessful
internal static IRawElementProviderSimple CreateButton(IntPtr hwnd)
{
// Currently there is an issue with CLR remoting that causes Accessible.CreateNativeFromEvent() to fail
// for Winforms controls. Until that is resolved use AccessibleObjectFromWindow() instead. It will
// return a Native IAccessble and not a OleAcc implementaion. Winforms does provide a Native IAccessible.
Accessible acc = null;
if (Accessible.AccessibleObjectFromWindow(hwnd, NativeMethods.OBJID_CLIENT, ref acc) != NativeMethods.S_OK || acc == null)
{
return null;
}
switch (acc.Role)
{
case AccessibleRole.CheckButton:
return new WindowsButton(hwnd, null, WindowsButton.ButtonType.CheckBox, Misc.GetWindowStyle(hwnd) & NativeMethods.BS_TYPEMASK, acc);
case AccessibleRole.Grouping:
return new WindowsButton(hwnd, null, WindowsButton.ButtonType.GroupBox, Misc.GetWindowStyle(hwnd) & NativeMethods.BS_TYPEMASK, acc);
case AccessibleRole.PushButton:
return new WindowsButton(hwnd, null, WindowsButton.ButtonType.PushButton, Misc.GetWindowStyle(hwnd) & NativeMethods.BS_TYPEMASK, acc);
case AccessibleRole.RadioButton:
return new WindowsButton(hwnd, null, WindowsButton.ButtonType.RadioButton, Misc.GetWindowStyle(hwnd) & NativeMethods.BS_TYPEMASK, acc);
default:
break;
}
return null;
}
#endregion
#region Internal Methods
// ------------------------------------------------------
//
// Internal Methods
//
// -----------------------------------------------------
// Checks if an hwnd is a winform.
// Returns True/False if the hwnd is a winform.
static internal FormControlState GetControlState(IntPtr hwnd)
{
return IsWindowsFormsControl(hwnd) ? FormControlState.True : FormControlState.False;
}
// Checks if an a class name is a winform classname.
// Returns True/False if the classname is a winform.
static internal bool IsWindowsFormsControl(string className)
{
return className.IndexOf(_WindowsFormsClassName, StringComparison.OrdinalIgnoreCase) > -1;
}
// Checks if an a class name is a winform classname.
// Returns True/False if the classname is a winform.
static internal bool IsWindowsFormsControl(IntPtr hwnd)
{
return IsWindowsFormsControl(Misc.GetClassName(hwnd));
}
static internal bool IsWindowsFormsControl(IntPtr hwnd, ref FormControlState state)
{
if (state == FormControlState.Undeterminate)
{
state = GetControlState(hwnd);
}
return state == FormControlState.True ? true : false;
}
// The control name is the only real "Persistent" ID in Windows Forms
static internal string WindowsFormsID(IntPtr hwnd)
{
return GetControlName(hwnd);
}
// Extract the internal Name property of the Windows Forms control using
// the WM_GETCONTROLNAME message.
static internal string GetControlName(IntPtr hwnd)
{
string winFormsID = "";
if (XSendMessage.XSend(hwnd, WM_GETCONTROLNAME, new IntPtr(Misc.MaxLengthNameProperty), ref winFormsID, Misc.MaxLengthNameProperty))
{
return winFormsID;
}
return null;
}
#endregion
#region Internal Fields
// ------------------------------------------------------
//
// Internal Fields
//
// ------------------------------------------------------
// The different states for figurating out if an hwnd is a winform.
// Underminate implies value not set yet. Must call GetControlState,
// otherwise the state is cached.
internal enum FormControlState
{
Undeterminate,
False,
True,
}
#endregion
#region Private Fields
// -----------------------------------------------------
//
// Private Fields
//
// ------------------------------------------------------
// Use this string to determine if this is a Windows Forms control or not
private const string _WindowsFormsClassName = "windowsforms";
// Private Message to know the underlying name for a control
private static int WM_GETCONTROLNAME = Misc.RegisterWindowMessage("WM_GETCONTROLNAME");
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description:
// This proxy is the base class for all proxies that support Windows
// Forms controls.
// All generic Windows Forms functionality goes here.
//
// History:
// 07/01/2003 : a-jeanp Created
//---------------------------------------------------------------------------
using System;
using System.Text;
using System.Collections;
using System.Windows.Automation;
using System.Windows.Automation.Provider;
using System.Runtime.InteropServices;
using MS.Win32;
namespace MS.Internal.AutomationProxies
{
// Helper static class used by the Win32 proxies to get Winforms information
static class WindowsFormsHelper
{
#region Proxy Create
// Static Create method called by UIAutomation to create proxies for Winforms controls.
// returns null if unsuccessful
internal static IRawElementProviderSimple Create(IntPtr hwnd, int idChild, int idObject)
{
// Currently there is an issue with CLR remoting that causes Accessible.CreateNativeFromEvent() to fail
// for Winforms controls. Until that is resolved use AccessibleObjectFromWindow() instead. It will
// return a Native IAccessble and not a OleAcc implementaion. Winforms does provide a Native IAccessible.
Accessible acc = null;
if (Accessible.AccessibleObjectFromWindow(hwnd, idObject, ref acc) != NativeMethods.S_OK || acc == null)
{
return null;
}
switch (acc.Role)
{
// ===========================================================
// WinformsSpinner controls are not identifiable by classname or
// other simple properties. The following case calls the
// WinformsSpinner constructor which in turn tries to establish
// the class of the control as a fact or returns null.
case AccessibleRole.Combobox:
return WinformsSpinner.Create( hwnd, idChild, idObject );
// ===========================================================
case AccessibleRole.SpinButton:
return WindowsUpDown.Create( hwnd, idChild, idObject );
case AccessibleRole.Grouping:
return new WindowsButton(hwnd, null, WindowsButton.ButtonType.GroupBox, Misc.GetWindowStyle(hwnd) & NativeMethods.BS_TYPEMASK, acc);
case AccessibleRole.StatusBar:
WindowsStatusBar sb = new WindowsStatusBar(hwnd, null, 0, acc);
if (sb == null)
{
return null;
}
return idChild == NativeMethods.CHILD_SELF ? sb : sb.CreateStatusBarPane(idChild);
default:
break;
}
return null;
}
// Static Create method called by UIAutomation to create a Button proxy for Winforms Buttons.
// returns null if unsuccessful
internal static IRawElementProviderSimple CreateButton(IntPtr hwnd)
{
// Currently there is an issue with CLR remoting that causes Accessible.CreateNativeFromEvent() to fail
// for Winforms controls. Until that is resolved use AccessibleObjectFromWindow() instead. It will
// return a Native IAccessble and not a OleAcc implementaion. Winforms does provide a Native IAccessible.
Accessible acc = null;
if (Accessible.AccessibleObjectFromWindow(hwnd, NativeMethods.OBJID_CLIENT, ref acc) != NativeMethods.S_OK || acc == null)
{
return null;
}
switch (acc.Role)
{
case AccessibleRole.CheckButton:
return new WindowsButton(hwnd, null, WindowsButton.ButtonType.CheckBox, Misc.GetWindowStyle(hwnd) & NativeMethods.BS_TYPEMASK, acc);
case AccessibleRole.Grouping:
return new WindowsButton(hwnd, null, WindowsButton.ButtonType.GroupBox, Misc.GetWindowStyle(hwnd) & NativeMethods.BS_TYPEMASK, acc);
case AccessibleRole.PushButton:
return new WindowsButton(hwnd, null, WindowsButton.ButtonType.PushButton, Misc.GetWindowStyle(hwnd) & NativeMethods.BS_TYPEMASK, acc);
case AccessibleRole.RadioButton:
return new WindowsButton(hwnd, null, WindowsButton.ButtonType.RadioButton, Misc.GetWindowStyle(hwnd) & NativeMethods.BS_TYPEMASK, acc);
default:
break;
}
return null;
}
#endregion
#region Internal Methods
// ------------------------------------------------------
//
// Internal Methods
//
// -----------------------------------------------------
// Checks if an hwnd is a winform.
// Returns True/False if the hwnd is a winform.
static internal FormControlState GetControlState(IntPtr hwnd)
{
return IsWindowsFormsControl(hwnd) ? FormControlState.True : FormControlState.False;
}
// Checks if an a class name is a winform classname.
// Returns True/False if the classname is a winform.
static internal bool IsWindowsFormsControl(string className)
{
return className.IndexOf(_WindowsFormsClassName, StringComparison.OrdinalIgnoreCase) > -1;
}
// Checks if an a class name is a winform classname.
// Returns True/False if the classname is a winform.
static internal bool IsWindowsFormsControl(IntPtr hwnd)
{
return IsWindowsFormsControl(Misc.GetClassName(hwnd));
}
static internal bool IsWindowsFormsControl(IntPtr hwnd, ref FormControlState state)
{
if (state == FormControlState.Undeterminate)
{
state = GetControlState(hwnd);
}
return state == FormControlState.True ? true : false;
}
// The control name is the only real "Persistent" ID in Windows Forms
static internal string WindowsFormsID(IntPtr hwnd)
{
return GetControlName(hwnd);
}
// Extract the internal Name property of the Windows Forms control using
// the WM_GETCONTROLNAME message.
static internal string GetControlName(IntPtr hwnd)
{
string winFormsID = "";
if (XSendMessage.XSend(hwnd, WM_GETCONTROLNAME, new IntPtr(Misc.MaxLengthNameProperty), ref winFormsID, Misc.MaxLengthNameProperty))
{
return winFormsID;
}
return null;
}
#endregion
#region Internal Fields
// ------------------------------------------------------
//
// Internal Fields
//
// ------------------------------------------------------
// The different states for figurating out if an hwnd is a winform.
// Underminate implies value not set yet. Must call GetControlState,
// otherwise the state is cached.
internal enum FormControlState
{
Undeterminate,
False,
True,
}
#endregion
#region Private Fields
// -----------------------------------------------------
//
// Private Fields
//
// ------------------------------------------------------
// Use this string to determine if this is a Windows Forms control or not
private const string _WindowsFormsClassName = "windowsforms";
// Private Message to know the underlying name for a control
private static int WM_GETCONTROLNAME = Misc.RegisterWindowMessage("WM_GETCONTROLNAME");
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- CharacterMetricsDictionary.cs
- StrokeRenderer.cs
- TypeGeneratedEventArgs.cs
- FormsAuthenticationCredentials.cs
- LicenseContext.cs
- PortCache.cs
- DataGridViewColumnCollection.cs
- DataGridItem.cs
- SortFieldComparer.cs
- DesignerInterfaces.cs
- CardSpacePolicyElement.cs
- PrintPreviewControl.cs
- TraceSection.cs
- BindingNavigator.cs
- CheckBoxField.cs
- ByteStack.cs
- HttpRuntimeSection.cs
- XmlSchemaInclude.cs
- MouseDevice.cs
- securitycriticaldata.cs
- HwndMouseInputProvider.cs
- FusionWrap.cs
- TextRangeEditLists.cs
- DefaultWorkflowLoaderService.cs
- RegistryConfigurationProvider.cs
- XamlSerializerUtil.cs
- FactoryGenerator.cs
- DeclarativeCatalogPart.cs
- EventsTab.cs
- TimeEnumHelper.cs
- FrameworkContentElement.cs
- XPathDocumentIterator.cs
- XmlNodeChangedEventArgs.cs
- NotifyCollectionChangedEventArgs.cs
- SQLByteStorage.cs
- XmlEncodedRawTextWriter.cs
- NamespaceCollection.cs
- DataGridViewControlCollection.cs
- MemoryStream.cs
- CallContext.cs
- Int64AnimationUsingKeyFrames.cs
- BinaryWriter.cs
- OracleCommandSet.cs
- SignedPkcs7.cs
- TreeIterator.cs
- StringReader.cs
- StylusShape.cs
- XPathMultyIterator.cs
- SqlTriggerAttribute.cs
- xsdvalidator.cs
- EncryptedPackageFilter.cs
- BehaviorEditorPart.cs
- UmAlQuraCalendar.cs
- Crc32.cs
- XmlSerializerVersionAttribute.cs
- EntityClientCacheEntry.cs
- SettingsProviderCollection.cs
- ColumnMap.cs
- SystemColorTracker.cs
- ListMarkerSourceInfo.cs
- AuthenticationModuleElementCollection.cs
- FlowDocument.cs
- objectquery_tresulttype.cs
- HttpCookie.cs
- BoundingRectTracker.cs
- SchemaComplexType.cs
- SynchronizedInputHelper.cs
- TabControlCancelEvent.cs
- ParserStreamGeometryContext.cs
- ThreadExceptionEvent.cs
- DbConnectionPoolGroup.cs
- RSAPKCS1KeyExchangeDeformatter.cs
- FilteredSchemaElementLookUpTable.cs
- IERequestCache.cs
- ScopedMessagePartSpecification.cs
- ThemeDirectoryCompiler.cs
- X500Name.cs
- XmlSecureResolver.cs
- Propagator.JoinPropagator.cs
- TextBoxLine.cs
- errorpatternmatcher.cs
- ExternalFile.cs
- WebPartZoneBase.cs
- ObjectPersistData.cs
- Opcode.cs
- PageBreakRecord.cs
- SqlWebEventProvider.cs
- SplitterEvent.cs
- DataTableReader.cs
- AutomationPattern.cs
- AccessDataSourceView.cs
- Lasso.cs
- CodeEntryPointMethod.cs
- NetTcpBindingCollectionElement.cs
- TdsParserSafeHandles.cs
- XmlArrayAttribute.cs
- TextEffect.cs
- XamlBuildTaskServices.cs
- peernodestatemanager.cs
- ChildTable.cs