Exemplo n.º 1
0
        /// <include file='doc\CommonDialog.uex' path='docs/doc[@for="CommonDialog.ShowDialog1"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Runs a common dialog box, parented to the given IWin32Window.
        ///    </para>
        /// </devdoc>
        public DialogResult ShowDialog(IWin32Window owner)
        {
            IntSecurity.SafeSubWindows.Demand();

            if (owner == null || owner.Handle == IntPtr.Zero)
            {
                return(ShowDialog());
            }

            if (!SystemInformation.UserInteractive)
            {
                throw new InvalidOperationException(SR.GetString(SR.CantShowModalOnNonInteractive));
            }

            if (helpMsg == 0)
            {
                helpMsg = SafeNativeMethods.RegisterWindowMessage("commdlg_help");
            }

            IntPtr hwndOwner = owner.Handle;

            defOwnerWndProc = UnsafeNativeMethods.GetWindowLong(new HandleRef(owner, 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(owner, 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(owner, hwndOwner)))
                {
                    UnsafeNativeMethods.SetWindowLong(new HandleRef(owner, hwndOwner), NativeMethods.GWL_WNDPROC, new HandleRef(null, defOwnerWndProc));
                }
            }

            return(result);
        }
Exemplo n.º 2
0
 // Disposes all top-level Controls on this thread
 internal void Dispose()
 {
     for (int i = 0; i < _windowCount; i++)
     {
         IntPtr hWnd = _windows[i];
         if (UnsafeNativeMethods.IsWindow(new HandleRef(null, hWnd)))
         {
             Control c = Control.FromHandle(hWnd);
             if (c != null)
             {
                 c.Dispose();
             }
         }
     }
 }
Exemplo n.º 3
0
            // 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));
                    }
                }
            }
Exemplo n.º 4
0
            // stop the timer and optionally destroy the HWND.
            //
            public void StopTimer(bool destroyHwnd, IntPtr hWnd)
            {
                if (hWnd == IntPtr.Zero)
                {
                    hWnd = Handle;
                }

                // Fire a message across threads to destroy the timer and HWND on the thread that created it.
                //
                if (GetInvokeRequired(hWnd))
                {
                    UnsafeNativeMethods.PostMessage(new HandleRef(this, hWnd), NativeMethods.WM_CLOSE, 0, 0);
                    return;
                }

                // Locking 'this' here is ok since this is an internal class.
                lock (this) {
                    if (_stoppingTimer || hWnd == IntPtr.Zero || !UnsafeNativeMethods.IsWindow(new HandleRef(this, hWnd)))
                    {
                        return;
                    }

                    if (_timerID != 0)
                    {
                        try {
                            _stoppingTimer = true;
                            SafeNativeMethods.KillTimer(new HandleRef(this, hWnd), _timerID);
                        }
                        finally {
                            _timerID       = 0;
                            _stoppingTimer = false;
                        }
                    }

                    if (destroyHwnd)
                    {
                        base.DestroyHandle();
                    }
                }
            }
Exemplo n.º 5
0
 public void StopTimer(bool destroyHwnd, IntPtr hWnd)
 {
     if (hWnd == IntPtr.Zero)
     {
         hWnd = base.Handle;
     }
     if (this.GetInvokeRequired(hWnd))
     {
         UnsafeNativeMethods.PostMessage(new HandleRef(this, hWnd), 0x10, 0, 0);
     }
     else
     {
         lock (this)
         {
             if ((!this._stoppingTimer && (hWnd != IntPtr.Zero)) && UnsafeNativeMethods.IsWindow(new HandleRef(this, hWnd)))
             {
                 if (this._timerID != 0)
                 {
                     try
                     {
                         this._stoppingTimer = true;
                         SafeNativeMethods.KillTimer(new HandleRef(this, hWnd), this._timerID);
                     }
                     finally
                     {
                         this._timerID       = 0;
                         this._stoppingTimer = false;
                     }
                 }
                 if (destroyHwnd)
                 {
                     base.DestroyHandle();
                 }
             }
         }
     }
 }