private Form1()
            {
                this.Size = new System.Drawing.Size(300, 300);
                this.Text = "Form1";

                nwl = new MyNativeWindowListener(this);
                nw  = new MyNativeWindow(this);
            }
Beispiel #2
0
 internal MyNotifyIcon()
 {
     window                    = new MyNativeWindow(this);
     iconData                  = new NOTIFYICONDATA();
     iconData.cbSize           = Marshal.SizeOf(typeof(NOTIFYICONDATA));
     iconData.hWnd             = window.Handle;
     iconData.uID              = 1;
     iconData.uCallbackMessage = WM_NOTIFYICONCALLBACK;
 }
Beispiel #3
0
        protected override void Dispose(bool disposing)
        {
            if (disposing && window != null)
            {
                visible = false;
                UpdateIcon();
                icon = null;
                window.Dispose();
                window = null;
            }

            base.Dispose(disposing);
        }
Beispiel #4
0
        public void DefWindProc_NoHandle()
        {
            MyNativeWindow nativeWindow = new MyNativeWindow();
            Message        message      = default;

            // This isn't supported per the docs, but we always have called DefWindowProc and shouldn't fail.
            // We assert as it isn't behavior we want to allow internally.
            using (new NoAssertContext())
            {
                nativeWindow.DefWndProc(ref message);
            }

            Assert.Empty(nativeWindow.Messages);
        }
Beispiel #5
0
        public void DefWindProc_AfterAssignHandle()
        {
            MyNativeWindow nativeWindow = new MyNativeWindow
            {
                MessageTypePredicate = (int msg) => msg == 123456
            };

            nativeWindow.CreateHandle(new CreateParams());

            Message message = Message.Create(IntPtr.Zero, 123456, IntPtr.Zero, IntPtr.Zero);

            // As we don't have a "Previous" the default window procedure gets called
            nativeWindow.DefWndProc(ref message);
            Assert.Empty(nativeWindow.Messages);

            MyNativeWindow nativeWindow2 = new MyNativeWindow
            {
                MessageTypePredicate = (int msg) => msg == 123456
            };

            // This will set the existing nativeWindow as Previous. When Previous NativeWindows
            // are registered for the same handle, a chain of calls will happen until the original
            // registered NativeWindow for a given handle is reached (i.e. no Previous). The
            // call chain is like this:
            //
            //   DefWndProc() -> PreviousWindow.CallBack() -> WndProc() -> DefWndProc()

            nativeWindow2.AssignHandle(nativeWindow.Handle);
            nativeWindow2.DefWndProc(ref message);
            Assert.Single(nativeWindow.Messages);
            Assert.Empty(nativeWindow2.Messages);

            // Check that the message continues to work back.

            MyNativeWindow nativeWindow3 = new MyNativeWindow
            {
                MessageTypePredicate = (int msg) => msg == 123456
            };

            nativeWindow3.AssignHandle(nativeWindow.Handle);
            nativeWindow3.DefWndProc(ref message);
            Assert.Equal(2, nativeWindow.Messages.Count);
            Assert.Single(nativeWindow2.Messages);
            Assert.Empty(nativeWindow3.Messages);
        }
Beispiel #6
0
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        var excelHwnd_IntPtr = new IntPtr(Globals.ThisAddIn.Application.Hwnd);

        MyNativeExcelWindow = new MyNativeWindow();
        MyNativeExcelWindow.AssignHandle(excelHwnd_IntPtr);
Beispiel #7
0
 internal MyNotifyIcon()
 {
     window = new MyNativeWindow(this);
     iconData = new NOTIFYICONDATA();
     iconData.cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
     iconData.hWnd = window.Handle;
     iconData.uID = 1;
     iconData.uCallbackMessage = WM_NOTIFYICONCALLBACK;
 }
Beispiel #8
0
        protected override void Dispose(bool disposing)
        {
            if (disposing && window != null)
            {
                visible = false;
                UpdateIcon();
                icon = null;
                window.Dispose();
                window = null;
            }

            base.Dispose(disposing);
        }
Beispiel #9
0
        // ------------------------------------------------------------------
        #endregion

        #region Private methods.
        // ------------------------------------------------------------------

        private int extendedFolderBrowserDialogBrowseCallbackProc(
            IntPtr hwnd,
            int msg,
            IntPtr lParam,
            IntPtr lpData)
        {
            switch (msg)
            {
            case Win32.BFFM_INITIALIZED:
                if (_selectedPath.Length != 0)
                {
                    Win32.SendMessage(
                        new HandleRef(null, hwnd),
                        Win32.BFFM_SETSELECTION,
                        1,
                        _selectedPath);

                    //centerToScreen(hwnd);

                    _nativeWindow = new MyNativeWindow(this);
                    _nativeWindow.AssignHandle(hwnd);
                }
                break;

            case Win32.BFFM_SELCHANGED:
            {
                //centerToScreen(hwnd);

                var ptr1 = lParam;
                if (ptr1 != IntPtr.Zero)
                {
                    // --

                    if (_selectedPath.Length != 0)
                    {
                        // Work-around bug_ in Windows 7 that the selected item is not visible.
                        // http://connect.microsoft.com/VisualStudio/feedback/details/518103/bffm-setselection-does-not-work-with-shbrowseforfolder-on-windows-7
                        // http://stackoverflow.com/questions/7014190/why-is-enumchildwindows-skipping-children
                        // http://www.ureader.com/msg/14842617.aspx
                        // http://stackoverflow.com/questions/3660556/how-to-select-an-item-in-a-treeview-using-win32-api

                        var childWindows = GetChildWindows(hwnd);

                        if (childWindows.Count > 0)
                        {
                            var hWndTree = childWindows[0];

                            var treeItem =
                                Win32.SendMessage(
                                    new HandleRef(null, hWndTree),
                                    Win32.TVM_GETNEXTITEM,
                                    Win32.TVGN_CARET,
                                    0);

                            Win32.SendMessage(
                                new HandleRef(null, hWndTree),
                                Win32.TVM_ENSUREVISIBLE,
                                0,
                                treeItem);
                        }
                    }

                    // --

                    var ptr2       = Marshal.AllocHGlobal(260 * Marshal.SystemDefaultCharSize);
                    var flag1      = Win32.Shell32.SHGetPathFromIDList(ptr1, ptr2);
                    var folderPath = Marshal.PtrToStringAuto(ptr2);
                    Marshal.FreeHGlobal(ptr2);

                    var enable = flag1;
                    if (flag1)
                    {
                        var args =
                            new SelectedFolderChangedEventArgs(
                                ptr1,
                                folderPath);

                        OnSelectedFolderChanged(args);

                        enable = args.EnableOK;
                    }

                    Win32.SendMessage(
                        new HandleRef(null, hwnd),
                        Win32.BFFM_ENABLEOK,
                        0,
                        enable ? 1 : 0);
                }
                break;
            }
            }
            return(0);
        }