/// <summary>
        /// The hook procedure for window messages generated by the FileOpenDialog
        /// </summary>
        /// <param name="hWnd">the handle of the window at which this message is targeted</param>
        /// <param name="msg">the message identifier</param>
        /// <param name="wParam">message-specific parameter data</param>
        /// <param name="lParam">mess-specific parameter data</param>
        /// <returns></returns>
        public IntPtr MyHookProc(IntPtr hWnd, UInt32 msg, Int32 wParam, Int32 lParam)
        {
            try
            {
                if (hWnd == IntPtr.Zero)
                {
                    return(IntPtr.Zero);
                }

                switch (msg)
                {
                // We're not interested in every possible message; just return a NULL for those we don't care about
                default:
                {
                    return(IntPtr.Zero);
                }

                // WM_INITDIALOG - at this point the OpenFileDialog exists, so we pull the user-supplied control
                // into the FileOpenDialog now, using the SetParent API.
                case WM.INITDIALOG:
                {
                    _hWndParent = User32.GetParent(hWnd);

                    //setting a bool for whether the OS is RTL (not the installed language of WLW)
                    IsRTL = (User32.GetWindowLong(_hWndParent, User32.GWL_EXSTYLE) & User32.WS_EX_LAYOUTRTL) > 0;

                    //account for large title bar, borders for adjusting control locations
                    TITLEBARINFO titleBarInfo = new TITLEBARINFO();
                    titleBarInfo.cbSize = (uint)Marshal.SizeOf(titleBarInfo);
                    if (!User32.GetTitleBarInfo(_hWndParent, ref titleBarInfo))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                    WINDOWINFO info = new WINDOWINFO();
                    info.cbSize = (uint)Marshal.SizeOf(info);
                    if (!User32.GetWindowInfo(_hWndParent, ref info))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                    _extraWindowHeight = (titleBarInfo.rcTitleBar.bottom - titleBarInfo.rcTitleBar.top) + 2 * (int)info.cyWindowBorders;
                    _extraWindowWidth  = 2 * (int)info.cxWindowBorders;

                    Rectangle rcClient = new Rectangle(0, 0, 0, 0);
                    // Get client rectangle of dialog
                    RECT rcTemp = new RECT();
                    User32.GetWindowRect(_hWndParent, ref rcTemp);
                    rcClient.X      = rcTemp.left;
                    rcClient.Y      = rcTemp.top;
                    rcClient.Width  = rcTemp.Width;
                    rcClient.Height = rcTemp.Height + TABS_HEIGHT + BUTTONS_HEIGHT;
                    //make the dialog box bigger
                    User32.MoveWindow(_hWndParent, rcClient.Left, rcClient.Top, rcClient.Width, rcClient.Height, true);
                    //move all the controls down
                    AdjustControlLocations();
                    //top tab control
                    mainTabControl = new LightweightControlContainerControl();
                    User32.SetParent(mainTabControl.Handle, _hWndParent);
                    mainTabControl.Location = new Point(0, 0);
                    mainTabControl.Anchor   = AnchorStyles.Left | AnchorStyles.Right;
                    mainTabControl.Size     = new Size(rcClient.Width, TABS_HEIGHT);

                    tabs = new TabLightweightControl();
                    tabs.ColorizeBorder = false;
                    tabs.VirtualBounds  = new Rectangle(0, 0, rcClient.Width, TABS_HEIGHT);
                    tabs.LightweightControlContainerControl = mainTabControl;
                    tabs.DrawSideAndBottomTabPageBorders    = false;

                    InsertImageTabControl tabFromFile = new InsertImageTabControl();
                    tabFromFile.TabText   = Res.Get(StringId.InsertImageInsertFromFile);
                    tabFromFile.TabBitmap = ResourceHelper.LoadAssemblyResourceBitmap("ImageInsertion.Images.TabInsertFromFile.png");
                    tabFromFile.BackColor = SystemColors.Control;
                    User32.SetParent(tabFromFile.Handle, _hWndParent);
                    tabs.SetTab(0, tabFromFile);

                    mainTabControl.BackColor = tabFromFile.ApplicationStyle.InactiveTabTopColor;

                    //now, add tabs for the other controls
                    int i = 1;
                    foreach (InsertImageSource imageSource in imageSources)
                    {
                        InsertImageTabControl tab = new InsertImageTabControl();
                        tab.TabText   = imageSource.TabName;
                        tab.TabBitmap = imageSource.TabBitmap;
                        tab.BackColor = SystemColors.Control;
                        tabs.SetTab(i++, tab);
                    }

                    tabs.SelectedTabNumberChanged += new EventHandler(tabs_SelectedTabNumberChanged);

                    //set the keyboard hook for tab switching
                    tabKeyboardHook = new TabbingHookProc(tabs);
                    tabKeyboardHook.Install(_hWndParent);

                    //add other image source panels
                    _panelImage             = new Panel();
                    _panelImage.Location    = new Point(0, mainTabControl.Size.Height);
                    _panelImage.BorderStyle = BorderStyle.None;
                    _panelImage.Anchor      = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
                    _panelImage.Size        = new Size(rcClient.Width, rcClient.Height - TABS_HEIGHT - BUTTONS_HEIGHT - _extraWindowHeight);
                    _panelImage.Visible     = false;

                    User32.SetParent(_panelImage.Handle, _hWndParent);

                    //initalize the other sources
                    foreach (InsertImageSource source in imageSources)
                    {
                        source.Init(_panelImage.Width, _panelImage.Height);
                        Control c = source.ImageSelectionControls;
                        DisplayHelper.Scale(c);
                        foreach (Control childControl in c.Controls)
                        {
                            DisplayHelper.Scale(childControl);
                        }
                    }
                    DisplayHelper.Scale(_panelImage);

                    //special cancel button
                    _buttonPanel             = new Panel();
                    _buttonPanel.Location    = new Point(rcClient.Width - (int)(0.5 * _extraWindowWidth) - _buttonPanel.Width, _panelImage.Bounds.Bottom);
                    _buttonPanel.Size        = new Size(75, 23);
                    _buttonPanel.BorderStyle = BorderStyle.None;

                    _cancelButton           = new Button();
                    _cancelButton.TextAlign = ContentAlignment.MiddleCenter;
                    if (BidiHelper.IsRightToLeft)
                    {
                        _cancelButton.RightToLeft = RightToLeft.Yes;
                    }
                    _cancelButton.Text      = Res.Get(StringId.CancelButton);
                    _cancelButton.FlatStyle = FlatStyle.System;
                    _cancelButton.Location  = new Point(0, 0);
                    _cancelButton.Size      = new Size(75, 23);
                    _cancelButton.Click    += new EventHandler(_cancelButton_Click);

                    _buttonPanel.Controls.Add(_cancelButton);

                    User32.SetParent(_buttonPanel.Handle, _hWndParent);

                    int    origWidth = _cancelButton.Width;
                    string tmp       = _cancelButton.Text;
                    _cancelButton.Text = Res.Get(StringId.InsertImageButton);
                    int newWidth = Math.Max(origWidth, DisplayHelper.MeasureButton(_cancelButton));
                    _cancelButton.Text = tmp;
                    newWidth           = Math.Max(newWidth, DisplayHelper.MeasureButton(_cancelButton));

                    _buttonPanel.Width = _cancelButton.Width = newWidth;
                    int deltaX = newWidth - origWidth;
                    _buttonPanel.Left -= deltaX;

                    //fixing up button text and tab order
                    IntPtr hWndOpenButton = User32.GetDlgItem(_hWndParent, _OPEN_BUTTON_ID);
                    User32.SetWindowText(hWndOpenButton, Res.Get(StringId.InsertImageButton));

                    mainTabControl.InitFocusManager();
                    mainTabControl.AddFocusableControls(tabs.GetAccessibleControls());
                    foreach (InsertImageSource tabPage in imageSources)
                    {
                        mainTabControl.AddFocusableControl(tabPage.ImageSelectionControls);
                    }

                    state = STATE.FILE;

                    return(IntPtr.Zero);
                }

                case WM.SIZE:
                {
                    ManipulatePanels();
                    return(IntPtr.Zero);
                }

                // WM_NOTIFY - we're only interested in the CDN_SELCHANGE notification message:
                // we grab the currently-selected filename and copy it into the buffer
                case WM.NOTIFY:
                {
                    IntPtr   ipNotify = new IntPtr(lParam);
                    OfNotify ofNot    = (OfNotify)Marshal.PtrToStructure(ipNotify, typeof(OfNotify));
                    Int16    code     = (short)ofNot.hdr.code;
                    if (code == CommonDlgNotification.SelChange)
                    {
                        UpdateChosenImage(false);
                        //CheckOptions(false);
                    }
                    else if (code == CommonDlgNotification.InitDone)
                    {
                        listener = new CommandListener(_hWndParent, this, (int)User32.GetDlgCtrlID(_cancelButton.Handle));
                    }
                    else if (code == CommonDlgNotification.FileOk)
                    {
                        // update the image path (need to do this if the user selected
                        // a file by simpliy typing in the filepath text box)
                        UpdateChosenImage(true);

                        // ok to insert
                        _insertFile = true;
                    }
                    else if ((code == CommonDlgNotification.FolderChange) && (state == STATE.WEB))
                    {
                        // If the user hits the OK button while there is no valid selection
                        // within the File panel, the file dialog sends a CommonDlgNotification.FolderChange
                        // We use this combined with other relevant state to trigger the closing
                        // of the Image dialog
                        HitOpen();
                    }
                    return(IntPtr.Zero);
                }
                }
            }
            catch (Exception ex)
            {
                UnexpectedErrorMessage.Show(ex);
                return(new IntPtr(1));
            }
            finally
            {
                GC.KeepAlive(this);
            }
        }
        /// <summary>
        /// The hook procedure for window messages generated by the FileOpenDialog
        /// </summary>
        /// <param name="hWnd">the handle of the window at which this message is targeted</param>
        /// <param name="msg">the message identifier</param>
        /// <param name="wParam">message-specific parameter data</param>
        /// <param name="lParam">mess-specific parameter data</param>
        /// <returns></returns>
        public IntPtr MyHookProc(IntPtr hWnd, UInt32 msg, Int32 wParam, Int32 lParam)
        {
            try
            {
                if (hWnd == IntPtr.Zero)
                    return IntPtr.Zero;

                switch (msg)
                {
                    // We're not interested in every possible message; just return a NULL for those we don't care about
                    default:
                        {
                            return IntPtr.Zero;
                        }
                    // WM_INITDIALOG - at this point the OpenFileDialog exists, so we pull the user-supplied control
                    // into the FileOpenDialog now, using the SetParent API.
                    case WM.INITDIALOG:
                        {

                            _hWndParent = User32.GetParent(hWnd);

                            //setting a bool for whether the OS is RTL (not the installed language of WLW)
                            IsRTL = (User32.GetWindowLong(_hWndParent, User32.GWL_EXSTYLE) & User32.WS_EX_LAYOUTRTL) > 0;

                            //account for large title bar, borders for adjusting control locations
                            TITLEBARINFO titleBarInfo = new TITLEBARINFO();
                            titleBarInfo.cbSize = (uint)Marshal.SizeOf(titleBarInfo);
                            if (!User32.GetTitleBarInfo(_hWndParent, ref titleBarInfo))
                            {
                                throw new Win32Exception(Marshal.GetLastWin32Error());
                            }

                            WINDOWINFO info = new WINDOWINFO();
                            info.cbSize = (uint)Marshal.SizeOf(info);
                            if (!User32.GetWindowInfo(_hWndParent, ref info))
                            {
                                throw new Win32Exception(Marshal.GetLastWin32Error());
                            }

                            _extraWindowHeight = (titleBarInfo.rcTitleBar.bottom - titleBarInfo.rcTitleBar.top) + 2 * (int)info.cyWindowBorders;
                            _extraWindowWidth = 2 * (int)info.cxWindowBorders;

                            Rectangle rcClient = new Rectangle(0, 0, 0, 0);
                            // Get client rectangle of dialog
                            RECT rcTemp = new RECT();
                            User32.GetWindowRect(_hWndParent, ref rcTemp);
                            rcClient.X = rcTemp.left;
                            rcClient.Y = rcTemp.top;
                            rcClient.Width = rcTemp.Width;
                            rcClient.Height = rcTemp.Height + TABS_HEIGHT + BUTTONS_HEIGHT;
                            //make the dialog box bigger
                            User32.MoveWindow(_hWndParent, rcClient.Left, rcClient.Top, rcClient.Width, rcClient.Height, true);
                            //move all the controls down
                            AdjustControlLocations();
                            //top tab control
                            mainTabControl = new LightweightControlContainerControl();
                            User32.SetParent(mainTabControl.Handle, _hWndParent);
                            mainTabControl.Location = new Point(0, 0);
                            mainTabControl.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                            mainTabControl.Size = new Size(rcClient.Width, TABS_HEIGHT);

                            tabs = new TabLightweightControl();
                            tabs.ColorizeBorder = false;
                            tabs.VirtualBounds = new Rectangle(0, 0, rcClient.Width, TABS_HEIGHT);
                            tabs.LightweightControlContainerControl = mainTabControl;
                            tabs.DrawSideAndBottomTabPageBorders = false;

                            InsertImageTabControl tabFromFile = new InsertImageTabControl();
                            tabFromFile.TabText = Res.Get(StringId.InsertImageInsertFromFile);
                            tabFromFile.TabBitmap = ResourceHelper.LoadAssemblyResourceBitmap("ImageInsertion.Images.TabInsertFromFile.png");
                            tabFromFile.BackColor = SystemColors.Control;
                            User32.SetParent(tabFromFile.Handle, _hWndParent);
                            tabs.SetTab(0, tabFromFile);

                            mainTabControl.BackColor = tabFromFile.ApplicationStyle.InactiveTabTopColor;

                            //now, add tabs for the other controls
                            int i = 1;
                            foreach (InsertImageSource imageSource in imageSources)
                            {
                                InsertImageTabControl tab = new InsertImageTabControl();
                                tab.TabText = imageSource.TabName;
                                tab.TabBitmap = imageSource.TabBitmap;
                                tab.BackColor = SystemColors.Control;
                                tabs.SetTab(i++, tab);
                            }

                            tabs.SelectedTabNumberChanged += new EventHandler(tabs_SelectedTabNumberChanged);

                            //set the keyboard hook for tab switching
                            tabKeyboardHook = new TabbingHookProc(tabs);
                            tabKeyboardHook.Install(_hWndParent);

                            //add other image source panels
                            _panelImage = new Panel();
                            _panelImage.Location = new Point(0, mainTabControl.Size.Height);
                            _panelImage.BorderStyle = BorderStyle.None;
                            _panelImage.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
                            _panelImage.Size = new Size(rcClient.Width, rcClient.Height - TABS_HEIGHT - BUTTONS_HEIGHT - _extraWindowHeight);
                            _panelImage.Visible = false;

                            User32.SetParent(_panelImage.Handle, _hWndParent);

                            //initalize the other sources
                            foreach (InsertImageSource source in imageSources)
                            {
                                source.Init(_panelImage.Width, _panelImage.Height);
                                Control c = source.ImageSelectionControls;
                                DisplayHelper.Scale(c);
                                foreach (Control childControl in c.Controls)
                                    DisplayHelper.Scale(childControl);
                            }
                            DisplayHelper.Scale(_panelImage);

                            //special cancel button
                            _buttonPanel = new Panel();
                            _buttonPanel.Location = new Point(rcClient.Width - (int)(0.5 * _extraWindowWidth) - _buttonPanel.Width, _panelImage.Bounds.Bottom);
                            _buttonPanel.Size = new Size(75, 23);
                            _buttonPanel.BorderStyle = BorderStyle.None;

                            _cancelButton = new Button();
                            _cancelButton.TextAlign = ContentAlignment.MiddleCenter;
                            if (BidiHelper.IsRightToLeft)
                                _cancelButton.RightToLeft = RightToLeft.Yes;
                            _cancelButton.Text = Res.Get(StringId.CancelButton);
                            _cancelButton.FlatStyle = FlatStyle.System;
                            _cancelButton.Location = new Point(0, 0);
                            _cancelButton.Size = new Size(75, 23);
                            _cancelButton.Click += new EventHandler(_cancelButton_Click);

                            _buttonPanel.Controls.Add(_cancelButton);

                            User32.SetParent(_buttonPanel.Handle, _hWndParent);

                            int origWidth = _cancelButton.Width;
                            string tmp = _cancelButton.Text;
                            _cancelButton.Text = Res.Get(StringId.InsertImageButton);
                            int newWidth = Math.Max(origWidth, DisplayHelper.MeasureButton(_cancelButton));
                            _cancelButton.Text = tmp;
                            newWidth = Math.Max(newWidth, DisplayHelper.MeasureButton(_cancelButton));

                            _buttonPanel.Width = _cancelButton.Width = newWidth;
                            int deltaX = newWidth - origWidth;
                            _buttonPanel.Left -= deltaX;

                            //fixing up button text and tab order
                            IntPtr hWndOpenButton = User32.GetDlgItem(_hWndParent, _OPEN_BUTTON_ID);
                            User32.SetWindowText(hWndOpenButton, Res.Get(StringId.InsertImageButton));

                            mainTabControl.InitFocusManager();
                            mainTabControl.AddFocusableControls(tabs.GetAccessibleControls());
                            foreach (InsertImageSource tabPage in imageSources)
                                mainTabControl.AddFocusableControl(tabPage.ImageSelectionControls);

                            state = STATE.FILE;

                            return IntPtr.Zero;
                        }

                    case WM.SIZE:
                        {
                            ManipulatePanels();
                            return IntPtr.Zero;
                        }
                    // WM_NOTIFY - we're only interested in the CDN_SELCHANGE notification message:
                    // we grab the currently-selected filename and copy it into the buffer
                    case WM.NOTIFY:
                        {
                            IntPtr ipNotify = new IntPtr(lParam);
                            OfNotify ofNot = (OfNotify)Marshal.PtrToStructure(ipNotify, typeof(OfNotify));
                            Int16 code = (short)ofNot.hdr.code;
                            if (code == CommonDlgNotification.SelChange)
                            {
                                UpdateChosenImage(false);
                                //CheckOptions(false);
                            }
                            else if (code == CommonDlgNotification.InitDone)
                            {
                                listener = new CommandListener(_hWndParent, this, (int)User32.GetDlgCtrlID(_cancelButton.Handle));
                            }
                            else if (code == CommonDlgNotification.FileOk)
                            {
                                // update the image path (need to do this if the user selected
                                // a file by simpliy typing in the filepath text box)
                                UpdateChosenImage(true);

                                // ok to insert
                                _insertFile = true;
                            }
                            else if ((code == CommonDlgNotification.FolderChange) && (state == STATE.WEB))
                            {
                                // If the user hits the OK button while there is no valid selection
                                // within the File panel, the file dialog sends a CommonDlgNotification.FolderChange
                                // We use this combined with other relevant state to trigger the closing
                                // of the Image dialog
                                HitOpen();
                            }
                            return IntPtr.Zero;
                        }
                }
            }
            catch (Exception ex)
            {
                UnexpectedErrorMessage.Show(ex);
                return new IntPtr(1);
            }
            finally
            {
                GC.KeepAlive(this);
            }
        }