Exemplo n.º 1
0
 /// <include file='doc\SendKeys.uex' path='docs/doc[@for="SendKeys.InstallHook"]/*' />
 /// <devdoc>
 ///     install the hook.  quite easy
 /// </devdoc>
 private static void InstallHook()
 {
     if (hhook == IntPtr.Zero)
     {
         hook     = new NativeMethods.HookProc(new SendKeysHookProc().Callback);
         stopHook = false;
         hhook    = UnsafeNativeMethods.SetWindowsHookEx(NativeMethods.WH_JOURNALPLAYBACK,
                                                         hook,
                                                         new HandleRef(null, UnsafeNativeMethods.GetModuleHandle(null)),
                                                         0);
         if (hhook == IntPtr.Zero)
         {
             throw new System.Security.SecurityException(SR.SendKeysHookFailed);
         }
     }
 }
Exemplo n.º 2
0
        /// <include file='doc\FontDialog.uex' path='docs/doc[@for="FontDialog.RunDialog"]/*' />
        /// <internalonly/>
        /// <devdoc>
        ///    <para>
        ///       The actual implementation of running the dialog. Inheriting classes
        ///       should override this if they want to add more functionality, and call
        ///       base.runDialog() if necessary
        ///
        ///    </para>
        /// </devdoc>
        protected override bool RunDialog(IntPtr hWndOwner)
        {
            NativeMethods.WndProc    hookProcPtr = new NativeMethods.WndProc(this.HookProc);
            NativeMethods.CHOOSEFONT cf          = new NativeMethods.CHOOSEFONT();
            IntPtr screenDC = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef);

            NativeMethods.LOGFONT lf = new NativeMethods.LOGFONT();

            Graphics graphics = Graphics.FromHdcInternal(screenDC);

            try {
                Font.ToLogFont(lf, graphics);
            }
            finally {
                graphics.Dispose();
            }
            UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, screenDC));

            IntPtr logFontPtr = IntPtr.Zero;

            try {
                logFontPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(NativeMethods.LOGFONT)));
                Marshal.StructureToPtr(lf, logFontPtr, false);

                cf.lStructSize = Marshal.SizeOf(typeof(NativeMethods.CHOOSEFONT));
                cf.hwndOwner   = hWndOwner;
                cf.hDC         = IntPtr.Zero;
                cf.lpLogFont   = logFontPtr;
                cf.Flags       = Options | NativeMethods.CF_INITTOLOGFONTSTRUCT | NativeMethods.CF_ENABLEHOOK;
                if (minSize > 0 || maxSize > 0)
                {
                    cf.Flags |= NativeMethods.CF_LIMITSIZE;
                }

                //if ShowColor=true then try to draw the sample text in color,
                //if ShowEffects=false then we will draw the sample text in standard control text color regardless.
                //(limitation of windows control)
                //
                if (ShowColor || ShowEffects)
                {
                    cf.rgbColors = ColorTranslator.ToWin32(color);
                }
                else
                {
                    cf.rgbColors = ColorTranslator.ToWin32(SystemColors.ControlText);
                }

                cf.lpfnHook  = hookProcPtr;
                cf.hInstance = UnsafeNativeMethods.GetModuleHandle(null);
                cf.nSizeMin  = minSize;
                if (maxSize == 0)
                {
                    cf.nSizeMax = int.MaxValue;
                }
                else
                {
                    cf.nSizeMax = maxSize;
                }
                Debug.Assert(cf.nSizeMin <= cf.nSizeMax, "min and max font sizes are the wrong way around");
                if (!SafeNativeMethods.ChooseFont(cf))
                {
                    return(false);
                }


                NativeMethods.LOGFONT lfReturned = null;
                lfReturned = (NativeMethods.LOGFONT)UnsafeNativeMethods.PtrToStructure(logFontPtr, typeof(NativeMethods.LOGFONT));

                if (lfReturned.lfFaceName != null && lfReturned.lfFaceName.Length > 0)
                {
                    lf = lfReturned;
                    UpdateFont(lf);
                    UpdateColor(cf.rgbColors);
                }

                return(true);
            }
            finally {
                if (logFontPtr != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(logFontPtr);
                }
            }
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        private static void TestHook()
        {
            hookSupported = false;
            try
            {
                NativeMethods.HookProc hookProc = new NativeMethods.HookProc(EmptyHookCallback);
                IntPtr hookHandle = UnsafeNativeMethods.SetWindowsHookEx(NativeMethods.WH_JOURNALPLAYBACK,
                                                                         hookProc,
                                                                         new HandleRef(null, UnsafeNativeMethods.GetModuleHandle(null)),
                                                                         0);

                hookSupported = (hookHandle != IntPtr.Zero);

                if (hookHandle != IntPtr.Zero)
                {
                    UnsafeNativeMethods.UnhookWindowsHookEx(new HandleRef(null, hookHandle));
                }
            }
            catch {} // ignore any exceptions to keep existing SendKeys behavior
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
 private static void TestHook()
 {
     hookSupported = false;
     try
     {
         System.Windows.Forms.NativeMethods.HookProc pfnhook = new System.Windows.Forms.NativeMethods.HookProc(SendKeys.EmptyHookCallback);
         IntPtr handle = UnsafeNativeMethods.SetWindowsHookEx(1, pfnhook, new HandleRef(null, UnsafeNativeMethods.GetModuleHandle(null)), 0);
         hookSupported = new bool?(handle != IntPtr.Zero);
         if (handle != IntPtr.Zero)
         {
             UnsafeNativeMethods.UnhookWindowsHookEx(new HandleRef(null, handle));
         }
     }
     catch
     {
     }
 }
Exemplo n.º 7
0
 private static void InstallHook()
 {
     if (hhook == IntPtr.Zero)
     {
         SendKeysHookProc proc1 = new SendKeysHookProc();
         hook     = new System.Windows.Forms.NativeMethods.HookProc(proc1.Callback);
         stopHook = false;
         hhook    = UnsafeNativeMethods.SetWindowsHookEx(1, hook, new HandleRef(null, UnsafeNativeMethods.GetModuleHandle(null)), 0);
         if (hhook == IntPtr.Zero)
         {
             throw new SecurityException(System.Windows.Forms.SR.GetString("SendKeysHookFailed"));
         }
     }
 }