/// <summary>
 /// Sets the status text of the dialog box.
 /// </summary>
 /// <param name="hwnd">The hwnd of the dialog box.</param>
 /// <param name="statusText">The status text to set.</param>
 private static void SetStatusText(IntPtr hwnd, string statusText)
 {
     NativeMethods.SendMessage(hwnd, (uint)BrowseForFolderMessages.BFFM_SETSTATUSTEXTW, (IntPtr)1, statusText);
 }
        /// <summary>
        /// Callback for Windows.
        /// </summary>
        /// <param name="hwnd">Window handle of the browse dialog box.</param>
        /// <param name="uMsg">Dialog box event that generated the statusMessage.</param>
        /// <param name="lParam">Value whose meaning depends on the event specified in uMsg.</param>
        /// <param name="lpData">Application-defined value that was specified in the lParam member of the BROWSEINFO structure used in the call to SHBrowseForFolder.</param>
        /// <returns>Returns 0 except in the case of BFFM_VALIDATEFAILED. For that flag, returns 0 to dismiss the dialog or nonzero to keep the dialog displayed.</returns>
        //[CLSCompliant(false)]
        private int OnBrowseEvent(IntPtr hwnd, uint uMsg, IntPtr lParam, IntPtr lpData)
        {
            BrowseForFolderMessages messsage = (BrowseForFolderMessages)uMsg;

            switch (messsage)
            {
            case BrowseForFolderMessages.BFFM_INITIALIZED:
                // Dialog is being initialized, so set the initial parameters
                if (!String.IsNullOrEmpty(Caption))
                {
                    NativeMethods.SetWindowText(hwnd, Caption);
                }

                if (!String.IsNullOrEmpty(SelectedPath))
                {
                    NativeMethods.SendMessage(hwnd, (uint)BrowseForFolderMessages.BFFM_SETEXPANDED, (IntPtr)1, SelectedPath);
                    NativeMethods.SendMessage(hwnd, (uint)BrowseForFolderMessages.BFFM_SETSELECTIONW, (IntPtr)1, SelectedPath);
                }

                if (!String.IsNullOrEmpty(OkText))
                {
                    NativeMethods.SendMessage(hwnd, (uint)BrowseForFolderMessages.BFFM_SETOKTEXT, (IntPtr)0, OkText);
                }

                if (Initialized != null)
                {
                    EventHandler <FolderBrowserDialogInitializedEventArgs> h = Initialized;
                    h(this, new FolderBrowserDialogInitializedEventArgs(hwnd));
                }

                return(0);

            case BrowseForFolderMessages.BFFM_SELCHANGED:
                try
                {
                    StringBuilder sb = new StringBuilder(260);
                    if (lParam == IntPtr.Zero || 0 == NativeMethods.SHGetPathFromIDList(lParam, sb))
                    {
                        return(0);
                    }
                    SelectedPath = sb.ToString();
                }
                catch { return(0); }
                if (SelectedPathChanged != null)
                {
                    PropertyChangedEventHandler h = SelectedPathChanged;
                    h(this, new PropertyChangedEventArgs("SelectedPath"));
                }
                return(0);

            case BrowseForFolderMessages.BFFM_VALIDATEFAILEDA:
            case BrowseForFolderMessages.BFFM_VALIDATEFAILEDW:
                if (InvalidFolderSelected != null)
                {
                    string folderName;
                    if (messsage == BrowseForFolderMessages.BFFM_VALIDATEFAILEDA)
                    {
                        folderName = Marshal.PtrToStringAnsi(lParam);
                    }
                    else
                    {
                        folderName = Marshal.PtrToStringUni(lParam);
                    }

                    EventHandler <InvalidFolderEventArgs> h = InvalidFolderSelected;
                    InvalidFolderEventArgs e = new InvalidFolderEventArgs(folderName, true);
                    h(this, e);
                    return(e.DismissDialog ? 0 : 1);
                }
                return(0);

            default:
                return(0);
            }
        }
 /// <summary>
 /// Enables or disables the OK button in the dialog box.
 /// </summary>
 /// <param name="hwnd">The hwnd of the dialog box.</param>
 /// <param name="isEnabled">Whether or not the OK button should be enabled.</param>
 private static void EnableOk(IntPtr hwnd, bool isEnabled)
 {
     NativeMethods.SendMessage(hwnd, (uint)BrowseForFolderMessages.BFFM_ENABLEOK, (IntPtr)0, (IntPtr)(isEnabled ? 1 : 0));
 }
Example #4
0
 void EndUpdate()
 {
     NativeMethods.SendMessage(Handle, NativeMethods.WM_SETREDRAW, 1, 0);
 }
Example #5
0
        private void UpdateImageList()
        {
            IntPtr handle = IntPtr.Zero;

            if (this.Style != CommandBarStyle.Menu)
            {
                Size size = new Size(8, 8);
                for (int i = 0; i < items.Count; i++)
                {
                    Image image = items[i].Image;
                    if (image != null)
                    {
                        if (image.Width > size.Width)
                        {
                            size.Width = image.Width;
                        }
                        if (image.Height > size.Height)
                        {
                            size.Height = image.Height;
                        }
                    }
                }

                Image[] images = new Image[items.Count];
                for (int i = 0; i < items.Count; i++)
                {
                    Image image = items[i].Image;
                    images[i] = (image != null) ? image : new Bitmap(size.Width, size.Height);
                }

                if (this.imageList == null)
                {
                    this.imageList            = new ImageList();
                    this.imageList.ImageSize  = size;
                    this.imageList.ColorDepth = ColorDepth.Depth32Bit;

                    for (int i = 0; i < images.Length; i++)
                    {
                        this.imageList.Images.Add(images[i]);
                    }
                }
                else if (this.imageList.Images.Count == images.Length)
                {
                    for (int i = 0; i < images.Length; i++)
                    {
                        this.imageList.Images[i] = images[i];
                    }
                }
                else
                {
                    this.imageList.Images.Clear();
                    this.imageList.ImageSize = size;

                    for (int i = 0; i < images.Length; i++)
                    {
                        this.imageList.Images.Add(images[i]);
                    }
                }

                handle = this.imageList.Handle;
            }

            NativeMethods.SendMessage(this.Handle, NativeMethods.TB_SETIMAGELIST, 0, handle);
        }
Example #6
0
 private void BeginUpdate()
 {
     NativeMethods.SendMessage(Handle, NativeMethods.WM_SETREDRAW, 0, 0);
 }
Example #7
0
        private void TrackDropDown(int index)
        {
            while (index >= 0)
            {
                this.trackNextItem = -1;

                this.BeginUpdate();

                CommandBarMenu menu = this.items[index] as CommandBarMenu;
                if (menu != null)
                {
                    menu.PerformDropDown(EventArgs.Empty);
                    this.contextMenu.Items.Clear();
                    this.contextMenu.Items.AddRange(menu.Items);                     // = menu.Items;
                    this.contextMenu.Mnemonics = true;
                }
                else
                {
                    this.contextMenu.Items.Clear();                     // .Items = new CommandBarItemCollection();
                    this.contextMenu.Mnemonics = true;
                }

                // Item state
                NativeMethods.SendMessage(this.Handle, NativeMethods.TB_PRESSBUTTON, index, -1);

                // Trick to get the first menu item selected
                NativeMethods.PostMessage(this.Handle, NativeMethods.WM_KEYDOWN, (int)Keys.Down, 1);
                NativeMethods.PostMessage(this.Handle, NativeMethods.WM_KEYUP, (int)Keys.Down, 1);

                this.SetState(State.HotTracking, index);

                // Hook
                NativeMethods.HookProc hookProc = new NativeMethods.HookProc(DropDownHook);
                GCHandle hookProcHandle         = GCHandle.Alloc(hookProc);
                this.hookHandle = NativeMethods.SetWindowsHookEx(NativeMethods.WH_MSGFILTER, hookProc, IntPtr.Zero, NativeMethods.GetCurrentThreadId());
                if (this.hookHandle == IntPtr.Zero)
                {
                    throw new SecurityException();
                }

                // Ask for position
                NativeMethods.RECT rect = new NativeMethods.RECT();
                NativeMethods.SendMessage(Handle, NativeMethods.TB_GETRECT, index, ref rect);
                Point position = new Point(rect.left, rect.bottom);

                this.EndUpdate();
                this.Update();

                this.contextMenu.Show(this, position);

                // Unhook
                NativeMethods.UnhookWindowsHookEx(hookHandle);
                hookProcHandle.Free();
                this.hookHandle = IntPtr.Zero;

                // Item state
                NativeMethods.SendMessage(Handle, NativeMethods.TB_PRESSBUTTON, index, 0);
                this.SetState(trackEscapePressed ? State.Hot : State.None, index);

                index = trackNextItem;
            }
        }
Example #8
0
 private void SetHotItem(int index)
 {
     NativeMethods.SendMessage(Handle, NativeMethods.TB_SETHOTITEM, index, 0);
 }
Example #9
0
 private int GetHotItem()
 {
     return(NativeMethods.SendMessage(Handle, NativeMethods.TB_GETHOTITEM, 0, 0));
 }
Example #10
0
        private bool IsValid(int index)
        {
            int count = NativeMethods.SendMessage(Handle, NativeMethods.TB_BUTTONCOUNT, 0, 0);

            return((index >= 0) && (index < count));
        }