private static void ShowHTMLFile(Control parent, string url, HelpNavigator command, object param) { Debug.WriteLineIf(Help.WindowsFormsHelpTrace.TraceVerbose, "Help:: ShowHTMLHelp:: " + url + ", " + command.ToString("G") + ", " + param); Uri file = Resolve(url); if (file == null) { throw new ArgumentException(string.Format(SR.HelpInvalidURL, url), "url"); } switch (file.Scheme) { case "http": case "https": Debug.WriteLineIf(IntSecurity.SecurityDemand.TraceVerbose, "WebPermission Demanded"); // new WebPermission(NetworkAccess.Connect, url).Demand(); break; default: Debug.WriteLineIf(IntSecurity.SecurityDemand.TraceVerbose, "UnmanagedCode Demanded"); IntSecurity.UnmanagedCode.Demand(); break; } switch (command) { case HelpNavigator.TableOfContents: case HelpNavigator.Find: case HelpNavigator.Index: // nothing needed... // break; case HelpNavigator.Topic: if (param != null && param is string) { file = new Uri(file.ToString() + "#" + (string)param); } break; } HandleRef handle; if (parent != null) { handle = new HandleRef(parent, parent.Handle); } else { handle = new HandleRef(null, UnsafeNativeMethods.GetActiveWindow()); } Debug.WriteLineIf(Help.WindowsFormsHelpTrace.TraceVerbose, "\tExecuting '" + file.ToString() + "'"); UnsafeNativeMethods.ShellExecute_NoBFM(handle, null, file.ToString(), null, null, NativeMethods.SW_NORMAL); }
private static DialogResult ShowCore(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options) { if (!Enum.IsDefined(typeof(MessageBoxButtons), buttons)) { throw new InvalidEnumArgumentException("buttons", (int)buttons, typeof(DialogResult)); } if (!Enum.IsDefined(typeof(MessageBoxIcon), icon)) { throw new InvalidEnumArgumentException("icon", (int)icon, typeof(DialogResult)); } if (!Enum.IsDefined(typeof(MessageBoxDefaultButton), defaultButton)) { throw new InvalidEnumArgumentException("defaultButton", (int)defaultButton, typeof(DialogResult)); } // options intentionally not verified because we don't expose all the options Win32 supports. if (!SystemInformation.UserInteractive && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0) { throw new InvalidOperationException(SR.GetString(SR.CantShowModalOnNonInteractive)); } if (owner != null && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0) { throw new ArgumentException(SR.GetString(SR.CantShowMBServiceWithOwner), "style"); } IntSecurity.SafeSubWindows.Demand(); int style = (int)buttons | (int)icon | (int)defaultButton | (int)options; IntPtr handle = IntPtr.Zero; if ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0) { if (owner == null) { handle = UnsafeNativeMethods.GetActiveWindow(); } else { handle = owner.Handle; } } Application.BeginModalMessageLoop(); DialogResult result = Win32ToDialogResult(SafeNativeMethods.MessageBox(new HandleRef(owner, handle), text, caption, style)); Application.EndModalMessageLoop(); return(result); }
/// <include file='doc\CommonDialog.uex' path='docs/doc[@for="CommonDialog.ShowDialog"]/*' /> /// <devdoc> /// <para> /// Runs a common dialog box. /// </para> /// </devdoc> public DialogResult ShowDialog() { IntSecurity.SafeSubWindows.Demand(); if (!SystemInformation.UserInteractive) { throw new InvalidOperationException(SR.GetString(SR.CantShowModalOnNonInteractive)); } if (helpMsg == 0) { helpMsg = SafeNativeMethods.RegisterWindowMessage("commdlg_help"); } IntPtr hwndOwner = UnsafeNativeMethods.GetActiveWindow(); if (hwndOwner == IntPtr.Zero) { hwndOwner = Application.GetParkingWindow(null).Handle; } defOwnerWndProc = UnsafeNativeMethods.GetWindowLong(new HandleRef(null, hwndOwner), NativeMethods.GWL_WNDPROC); // define ownerproc out here so it won't be garbage collected before the finally // clause runs NativeMethods.WndProc ownerProc = new NativeMethods.WndProc(this.OwnerWndProc); DialogResult result = DialogResult.Cancel; try { UnsafeNativeMethods.SetWindowLong(new HandleRef(null, hwndOwner), NativeMethods.GWL_WNDPROC, ownerProc); Application.BeginModalMessageLoop(); try { result = RunDialog(hwndOwner) ? DialogResult.OK : DialogResult.Cancel; GC.KeepAlive(ownerProc); } finally { Application.EndModalMessageLoop(); } } finally { if (UnsafeNativeMethods.IsWindow(new HandleRef(null, hwndOwner))) { UnsafeNativeMethods.SetWindowLong(new HandleRef(null, hwndOwner), NativeMethods.GWL_WNDPROC, new HandleRef(null, defOwnerWndProc)); } } return(result); }
// Enables/disables all top-level Controls on this thread internal void Enable(bool state) { if (!_onlyWinForms && !state) { _activeHwnd = UnsafeNativeMethods.GetActiveWindow(); Control activatingControl = ThreadContext.FromCurrent().ActivatingControl; if (activatingControl != null) { _focusedHwnd = activatingControl.Handle; } else { _focusedHwnd = UnsafeNativeMethods.GetFocus(); } } for (int i = 0; i < _windowCount; i++) { IntPtr hWnd = _windows[i]; Debug.WriteLineIf(CompModSwitches.MSOComponentManager.TraceInfo, "ComponentManager : Changing enabled on window: " + hWnd.ToString() + " : " + state.ToString()); if (UnsafeNativeMethods.IsWindow(new HandleRef(null, hWnd))) { SafeNativeMethods.EnableWindow(new HandleRef(null, hWnd), state); } } // OpenFileDialog is not returning the focus the way other dialogs do. // Important that we re-activate the old window when we are closing // our modal dialog. // // edit mode forever with Excel application // But, DON'T change other people's state when we're simply // responding to external MSOCM events about modality. When we are, // we are created with a TRUE for onlyWinForms. if (!_onlyWinForms && state) { if (_activeHwnd != IntPtr.Zero && UnsafeNativeMethods.IsWindow(new HandleRef(null, _activeHwnd))) { UnsafeNativeMethods.SetActiveWindow(new HandleRef(null, _activeHwnd)); } if (_focusedHwnd != IntPtr.Zero && UnsafeNativeMethods.IsWindow(new HandleRef(null, _focusedHwnd))) { UnsafeNativeMethods.SetFocus(new HandleRef(null, _focusedHwnd)); } } }
/// <summary> /// Displays HTMLFile with the specified parameters /// </summary> private static void ShowHTMLFile(Control parent, string url, HelpNavigator command, object param) { Debug.WriteLineIf(Help.WindowsFormsHelpTrace.TraceVerbose, "Help:: ShowHTMLHelp:: " + url + ", " + command.ToString("G") + ", " + param); Uri file = Resolve(url); if (file == null) { throw new ArgumentException(string.Format(SR.HelpInvalidURL, url), "url"); } switch (command) { case HelpNavigator.TableOfContents: case HelpNavigator.Find: case HelpNavigator.Index: // nothing needed... // break; case HelpNavigator.Topic: if (param != null && param is string) { file = new Uri(file.ToString() + "#" + (string)param); } break; } HandleRef handle; if (parent != null) { handle = new HandleRef(parent, parent.Handle); } else { handle = new HandleRef(null, UnsafeNativeMethods.GetActiveWindow()); } Debug.WriteLineIf(Help.WindowsFormsHelpTrace.TraceVerbose, "\tExecuting '" + file.ToString() + "'"); Shell32.ShellExecuteW(handle, null, file.ToString(), null, null, User32.SW.NORMAL); }
private static DialogResult ShowCore(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, bool showHelp) { if (!ClientUtils.IsEnumValid(buttons, (int)buttons, (int)MessageBoxButtons.OK, (int)MessageBoxButtons.RetryCancel)) { throw new InvalidEnumArgumentException(nameof(buttons), (int)buttons, typeof(MessageBoxButtons)); } // valid values are 0x0 0x10 0x20 0x30 0x40, chop off the last 4 bits and check that it's between 0 and 4. if (!WindowsFormsUtils.EnumValidator.IsEnumWithinShiftedRange(icon, /*numBitsToShift*/ 4, /*min*/ 0x0, /*max*/ 0x4)) { throw new InvalidEnumArgumentException(nameof(icon), (int)icon, typeof(MessageBoxIcon)); } // valid values are 0x0 0x100, 0x200, chop off the last 8 bits and check that it's between 0 and 2. if (!WindowsFormsUtils.EnumValidator.IsEnumWithinShiftedRange(defaultButton, /*numBitsToShift*/ 8, /*min*/ 0x0, /*max*/ 0x2)) { throw new InvalidEnumArgumentException(nameof(defaultButton), (int)defaultButton, typeof(DialogResult)); } // options intentionally not verified because we don't expose all the options Win32 supports. if (!SystemInformation.UserInteractive && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0) { throw new InvalidOperationException(SR.CantShowModalOnNonInteractive); } if (owner != null && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0) { throw new ArgumentException(SR.CantShowMBServiceWithOwner, "options"); } if (showHelp && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0) { throw new ArgumentException(SR.CantShowMBServiceWithHelp, "options"); } int style = (showHelp) ? HELP_BUTTON : 0; style |= (int)buttons | (int)icon | (int)defaultButton | (int)options; IntPtr handle = IntPtr.Zero; if (showHelp || ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0)) { if (owner == null) { handle = UnsafeNativeMethods.GetActiveWindow(); } else { handle = Control.GetSafeHandle(owner); } } IntPtr userCookie = IntPtr.Zero; if (Application.UseVisualStyles) { // CLR4.0 or later, shell32.dll needs to be loaded explicitly. if (UnsafeNativeMethods.GetModuleHandle(ExternDll.Shell32) == IntPtr.Zero) { if (UnsafeNativeMethods.LoadLibraryFromSystemPathIfAvailable(ExternDll.Shell32) == IntPtr.Zero) { int lastWin32Error = Marshal.GetLastWin32Error(); throw new Win32Exception(lastWin32Error, string.Format(SR.LoadDLLError, ExternDll.Shell32)); } } // Activate theming scope to get theming for controls at design time and when hosted in browser. // NOTE: If a theming context is already active, this call is very fast, so shouldn't be a perf issue. userCookie = UnsafeNativeMethods.ThemingScope.Activate(); } Application.BeginModalMessageLoop(); DialogResult result; try { result = Win32ToDialogResult(SafeNativeMethods.MessageBox(new HandleRef(owner, handle), text, caption, style)); } finally { Application.EndModalMessageLoop(); UnsafeNativeMethods.ThemingScope.Deactivate(userCookie); } // Right after the dialog box is closed, Windows sends WM_SETFOCUS back to the previously active control // but since we have disabled this thread main window the message is lost. So we have to send it again after // we enable the main window. // UnsafeNativeMethods.SendMessage(new HandleRef(owner, handle), Interop.WindowMessages.WM_SETFOCUS, 0, 0); return(result); }
/// <include file='doc\Help.uex' path='docs/doc[@for="Help.ShowHTML10Help"]/*' /> /// <devdoc> /// Displays HTML 1.0 Help with the specified parameters /// </devdoc> /// <internalonly/> private static void ShowHTML10Help(Control parent, string url, HelpNavigator command, object param) { Debug.WriteLineIf(Help.WindowsFormsHelpTrace.TraceVerbose, "Help:: ShowHTML10Help:: " + url + ", " + command.ToString("G") + ", " + param); // See if we can get a full path and file name and if that will // resolve the out of memory condition with file names that include spaces. // If we can't, though, we can't assume that the path's no good: it might be in // the Windows help directory. Uri file = null; string pathAndFileName = url; //This is our best guess at the path yet. file = Resolve(url); if (file != null) // Can't assume we have a good url { pathAndFileName = file.AbsoluteUri; } if (file == null || file.IsFile) { StringBuilder newPath = new StringBuilder(); string localPath = (file != null && file.IsFile) ? file.LocalPath : url; // If this is a local path, convert it to a short path name. Pass 0 as the length the first time uint requiredStringSize = UnsafeNativeMethods.GetShortPathName(localPath, newPath, 0); if (requiredStringSize > 0) { //It's able to make it a short path. Happy day. newPath.Capacity = (int)requiredStringSize; requiredStringSize = UnsafeNativeMethods.GetShortPathName(localPath, newPath, requiredStringSize); //If it can't make it a short path, just leave the path we had. pathAndFileName = newPath.ToString(); } } HandleRef handle; if (parent != null) { handle = new HandleRef(parent, parent.Handle); } else { handle = new HandleRef(null, UnsafeNativeMethods.GetActiveWindow()); } object htmlParam; string stringParam = param as string; if (stringParam != null) { int htmlCommand = MapCommandToHTMLCommand(command, stringParam, out htmlParam); string stringHtmlParam = htmlParam as string; if (stringHtmlParam != null) { SafeNativeMethods.HtmlHelp(handle, pathAndFileName, htmlCommand, stringHtmlParam); } else if (htmlParam is int) { SafeNativeMethods.HtmlHelp(handle, pathAndFileName, htmlCommand, (int)htmlParam); } else if (htmlParam is NativeMethods.HH_FTS_QUERY) { SafeNativeMethods.HtmlHelp(handle, pathAndFileName, htmlCommand, (NativeMethods.HH_FTS_QUERY)htmlParam); } else if (htmlParam is NativeMethods.HH_AKLINK) { // According to MSDN documentation, we have to ensure that the help window is up // before we call ALINK lookup. // SafeNativeMethods.HtmlHelp(NativeMethods.NullHandleRef, pathAndFileName, HH_DISPLAY_TOPIC, (string)null); SafeNativeMethods.HtmlHelp(handle, pathAndFileName, htmlCommand, (NativeMethods.HH_AKLINK)htmlParam); } else { Debug.Fail("Cannot handle HTML parameter of type: " + htmlParam.GetType()); SafeNativeMethods.HtmlHelp(handle, pathAndFileName, htmlCommand, (string)param); } } else if (param == null) { SafeNativeMethods.HtmlHelp(handle, pathAndFileName, MapCommandToHTMLCommand(command, null, out htmlParam), 0); } else if (param is NativeMethods.HH_POPUP) { SafeNativeMethods.HtmlHelp(handle, pathAndFileName, HH_DISPLAY_TEXT_POPUP, (NativeMethods.HH_POPUP)param); } else if (param.GetType() == typeof(int)) { throw new ArgumentException(string.Format(SR.InvalidArgument, "param", "Integer")); } }
public DialogResult ShowDialog(IWin32Window owner) { if (!SystemInformation.UserInteractive) { throw new InvalidOperationException(SR.CantShowModalOnNonInteractive); } NativeWindow native = null;//This will be used if there is no owner or active window (declared here so it can be kept alive) IntPtr hwndOwner = IntPtr.Zero; DialogResult result = DialogResult.Cancel; try { if (owner != null) { hwndOwner = Control.GetSafeHandle(owner); } if (hwndOwner == IntPtr.Zero) { hwndOwner = UnsafeNativeMethods.GetActiveWindow(); } if (hwndOwner == IntPtr.Zero) { //We will have to create our own Window native = new NativeWindow(); native.CreateHandle(new CreateParams()); hwndOwner = native.Handle; } if (helpMsg == 0) { helpMsg = SafeNativeMethods.RegisterWindowMessage("commdlg_help"); } NativeMethods.WndProc ownerProc = new NativeMethods.WndProc(this.OwnerWndProc); hookedWndProc = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(ownerProc); System.Diagnostics.Debug.Assert(IntPtr.Zero == defOwnerWndProc, "The previous subclass wasn't properly cleaned up"); IntPtr userCookie = IntPtr.Zero; try { //UnsafeNativeMethods.[Get|Set]WindowLong is smart enough to call SetWindowLongPtr on 64-bit OS defOwnerWndProc = UnsafeNativeMethods.SetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC, ownerProc); if (Application.UseVisualStyles) { userCookie = UnsafeNativeMethods.ThemingScope.Activate(); } Application.BeginModalMessageLoop(); try { result = RunDialog(hwndOwner) ? DialogResult.OK : DialogResult.Cancel; } finally { Application.EndModalMessageLoop(); } } finally { IntPtr currentSubClass = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC); if (IntPtr.Zero != defOwnerWndProc || currentSubClass != hookedWndProc) { UnsafeNativeMethods.SetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC, new HandleRef(this, defOwnerWndProc)); } UnsafeNativeMethods.ThemingScope.Deactivate(userCookie); defOwnerWndProc = IntPtr.Zero; hookedWndProc = IntPtr.Zero; //Ensure that the subclass delegate will not be GC collected until after it has been subclassed GC.KeepAlive(ownerProc); } } finally { if (null != native) { native.DestroyHandle(); } } return(result); }
private static DialogResult ShowCore(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, bool showHelp) { DialogResult result; if (!System.Windows.Forms.ClientUtils.IsEnumValid(buttons, (int)buttons, 0, 5)) { throw new InvalidEnumArgumentException("buttons", (int)buttons, typeof(MessageBoxButtons)); } if (!WindowsFormsUtils.EnumValidator.IsEnumWithinShiftedRange(icon, 4, 0, 4)) { throw new InvalidEnumArgumentException("icon", (int)icon, typeof(MessageBoxIcon)); } if (!WindowsFormsUtils.EnumValidator.IsEnumWithinShiftedRange(defaultButton, 8, 0, 2)) { throw new InvalidEnumArgumentException("defaultButton", (int)defaultButton, typeof(DialogResult)); } if (!SystemInformation.UserInteractive && ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0)) { throw new InvalidOperationException(System.Windows.Forms.SR.GetString("CantShowModalOnNonInteractive")); } if ((owner != null) && ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)) { throw new ArgumentException(System.Windows.Forms.SR.GetString("CantShowMBServiceWithOwner"), "options"); } if (showHelp && ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)) { throw new ArgumentException(System.Windows.Forms.SR.GetString("CantShowMBServiceWithHelp"), "options"); } if ((options & ~(MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign)) != 0) { System.Windows.Forms.IntSecurity.UnmanagedCode.Demand(); } System.Windows.Forms.IntSecurity.SafeSubWindows.Demand(); int type = showHelp ? 0x4000 : 0; type |= ((buttons | ((MessageBoxButtons)((int)icon))) | ((MessageBoxButtons)((int)defaultButton))) | ((MessageBoxButtons)((int)options)); IntPtr zero = IntPtr.Zero; if (showHelp || ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0)) { if (owner == null) { zero = UnsafeNativeMethods.GetActiveWindow(); } else { zero = Control.GetSafeHandle(owner); } } IntPtr userCookie = IntPtr.Zero; if (Application.UseVisualStyles) { if ((UnsafeNativeMethods.GetModuleHandle("shell32.dll") == IntPtr.Zero) && (UnsafeNativeMethods.LoadLibrary("shell32.dll") == IntPtr.Zero)) { throw new Win32Exception(Marshal.GetLastWin32Error(), System.Windows.Forms.SR.GetString("LoadDLLError", new object[] { "shell32.dll" })); } userCookie = UnsafeNativeMethods.ThemingScope.Activate(); } Application.BeginModalMessageLoop(); try { result = Win32ToDialogResult(SafeNativeMethods.MessageBox(new HandleRef(owner, zero), text, caption, type)); } finally { Application.EndModalMessageLoop(); UnsafeNativeMethods.ThemingScope.Deactivate(userCookie); } UnsafeNativeMethods.SendMessage(new HandleRef(owner, zero), 7, 0, 0); return(result); }