none
WIN32 API DIalog в WPF RRS feed

  • Вопрос

  • Пытаюсь из dll загрузить Dialog и положить в Border, при этом изменив некоторые стили диалога.

    Вылетает, пишет что недопустимый дескриптор окна.

     

    Создаю объект и размещаю в Border следующим образом:

    DialogContol Dialog = new DialogContol((int)BorderResourceData.ActualWidth, (int)BorderResourceData.ActualHeight, PEDataInfo.ModuleLib, resID.ID);

    BorderResourceData.Child = Dialog;

     

     

    Сам класс Dialog:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Interop;
    using System.Runtime.InteropServices;
    using System.Windows;

    namespace test354.Classes
    {
        public class DialogContol : HwndHost
        {
            enum EnumWindowInfoType : int
            {
                GWL_WNDPROC    = (-4),
                GWL_HINSTANCE  = (-6),
                GWL_HWNDPARENT = (-8),
                GWL_STYLE      = (-16),
                GWL_EXSTYLE    = (-20),
                GWL_USERDATA   = (-21),
                GWL_ID         = (-12)
            }

            enum EnumExWindowStyle : uint
            {
                WS_EX_DLGMODALFRAME    = 0x00000001,
                WS_EX_NOPARENTNOTIFY   = 0x00000004,
                WS_EX_TOPMOST          = 0x00000008,
                WS_EX_ACCEPTFILES      = 0x00000010,
                WS_EX_TRANSPARENT      = 0x00000020,

                WS_EX_MDICHILD         = 0x00000040,
                WS_EX_TOOLWINDOW       = 0x00000080,
                WS_EX_WINDOWEDGE       = 0x00000100,
                WS_EX_CLIENTEDGE       = 0x00000200,
                WS_EX_CONTEXTHELP      = 0x00000400,

                WS_EX_RIGHT            = 0x00001000,
                WS_EX_LEFT             = 0x00000000,
                WS_EX_RTLREADING       = 0x00002000,
                WS_EX_LTRREADING       = 0x00000000,
                WS_EX_LEFTSCROLLBAR    = 0x00004000,
                WS_EX_RIGHTSCROLLBAR   = 0x00000000,

                WS_EX_CONTROLPARENT    = 0x00010000,
                WS_EX_STATICEDGE       = 0x00020000,
                WS_EX_APPWINDOW        = 0x00040000,
       
                WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE),
                WS_EX_PALETTEWINDOW    = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST),

                WS_EX_LAYERED          = 0x00080000,
                WS_EX_NOINHERITLAYOUT  = 0x00100000,
                WS_EX_LAYOUTRTL        = 0x00400000,
                WS_EX_COMPOSITED       = 0x02000000,
                WS_EX_NOACTIVATE       = 0x08000000

            }

            enum EnumWindowStyle : uint
            {
                WS_OVERLAPPED       = 0x00000000,
                WS_POPUP            = 0x80000000,
                WS_CHILD            = 0x40000000,
                WS_MINIMIZE         = 0x20000000,
                WS_VISIBLE          = 0x10000000,
                WS_DISABLED         = 0x08000000,
                WS_CLIPSIBLINGS     = 0x04000000,
                WS_CLIPCHILDREN     = 0x02000000,
                WS_MAXIMIZE         = 0x01000000,
                WS_CAPTION          = 0x00C00000,    /* WS_BORDER | WS_DLGFRAME  */
                WS_BORDER           = 0x00800000,
                WS_DLGFRAME         = 0x00400000,
                WS_VSCROLL          = 0x00200000,
                WS_HSCROLL          = 0x00100000,
                WS_SYSMENU          = 0x00080000,
                WS_THICKFRAME       = 0x00040000,
                WS_GROUP            = 0x00020000,
                WS_TABSTOP          = 0x00010000,

                WS_MINIMIZEBOX      = 0x00020000,
                WS_MAXIMIZEBOX      = 0x00010000,

                WS_TILED            = WS_OVERLAPPED,
                WS_ICONIC           = WS_MINIMIZE,
                WS_SIZEBOX          = WS_THICKFRAME,
                WS_TILEDWINDOW      = WS_OVERLAPPEDWINDOW,

                WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX),

                WS_POPUPWINDOW      = (WS_POPUP | WS_BORDER | WS_SYSMENU),

                WS_CHILDWINDOW      = (WS_CHILD)

            }

            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
            struct Rect
            {
                int left;
                int top;
                int right;
                int bottom;
            }


            [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            static extern IntPtr CreateDialogParam(IntPtr hInstance, [MarshalAs(UnmanagedType.LPWStr)] String lpTemplate, IntPtr hWndParent, IntPtr lpDialogFunc, IntPtr dwInitParam);

            [DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Auto, SetLastError = true)]
            static extern bool DestroyWindow(IntPtr hwnd);

            [DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Auto, SetLastError = true)]
            static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);

            [DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Auto, SetLastError = true)]
            static extern uint GetWindowLong(IntPtr hWnd, int nIndex);

            [DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Auto, SetLastError = true)]
            static extern bool GetWindowRect(IntPtr hWnd, out Rect lpRect);

            [DllImport("User32.dll")]
            static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
          
            [DllImport("User32.dll")]
            static extern int GetWindowTextLength(IntPtr hWnd);

            [DllImport("user32.dll")]
            public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

            [DllImport("user32.dll")]
            static extern int GetDlgCtrlID(IntPtr hwndCtl);

            [DllImport("user32.dll", SetLastError = true)]
            static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowsDelegate lpEnumFunc, IntPtr lParam);

            delegate bool EnumWindowsDelegate(IntPtr hWnd, IntPtr lParam);

          

            IntPtr hwndDialog;
            IntPtr hInstance;
            String DialogID;
            List<String> ListDlgInfo;
            int Width;
            int Height;


            public IntPtr hWndDialog
            {
                get
                {
                    return hwndDialog;
                }
            }

            public DialogContol(int Width, int Height, IntPtr hInstance, String DialogID)
            {
                this.Width = Width;
                this.Height = Height;
                this.hInstance = hInstance;
                this.DialogID = DialogID;
            }

            protected override HandleRef BuildWindowCore(HandleRef hwndParent)
            {
                try
                {
                    hwndDialog = IntPtr.Zero;
                  
                    hwndDialog = CreateDialogParam(hInstance, "#" + DialogID, hwndParent.Handle, IntPtr.Zero, IntPtr.Zero);

                    if (hwndDialog == IntPtr.Zero)
                    {
                        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    }

                    uint WindowStyle = GetWindowLong(hWndDialog, (int)EnumWindowInfoType.GWL_EXSTYLE);
                    uint NewWindowStyle = WindowStyle;

                    if ((WindowStyle & (uint)EnumWindowStyle.WS_DISABLED) > 0)
                    {
                        NewWindowStyle |= (uint)EnumWindowStyle.WS_DISABLED;
                    }

                    if ((WindowStyle & (uint)EnumWindowStyle.WS_CHILD) == 0)
                    {
                        NewWindowStyle |= (uint)EnumWindowStyle.WS_CHILD;
                    }

                    if ((WindowStyle & (uint)EnumWindowStyle.WS_POPUP) > 0)
                    {
                        NewWindowStyle |= (uint)EnumWindowStyle.WS_POPUP;
                    }

                    if (SetWindowLong(hwndDialog, (int)EnumWindowInfoType.GWL_STYLE, NewWindowStyle) == 0)
                    {
                        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    }

                   
                   
                    return new HandleRef(this, hWndDialog);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                return new HandleRef(this, IntPtr.Zero);
            }

            protected override void DestroyWindowCore(HandleRef hwnd)
            {
                DestroyWindow(hwnd.Handle);
            }

           
        }
    }

    • Перемещено Abolmasov Dmitry 19 января 2011 г. 12:39 (От:Настольные ПК)
    16 января 2011 г. 15:42

Ответы

Все ответы

  • Попробую зайти с другой стороны. Как мне получить стили окна для диалога?? и установить их!

    Если GetWindowLong всегда возвращает 1

    а при SetWindowLong недопустимый дескриптор окна

    что я делаю не правильно???

     

    protected override HandleRef BuildWindowCore(HandleRef hwndParent)
    {
                try
                {
                    hwndDialog = IntPtr.Zero;

                   //hInstance - дескриптор модуля модуля где хранится Dialog

                   hwndDialog = CreateDialogParam(hInstance, "#" + DialogID, hwndParent.Handle, IntPtr.Zero, IntPtr.Zero);

                    if (hwndDialog == IntPtr.Zero)
                    {
                        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    }

                    uint WindowStyle = GetWindowLong(hWndDialog, (int)EnumWindowInfoType.GWL_EXSTYLE);
                    uint NewWindowStyle = WindowStyle;

                    if ((WindowStyle & (uint)EnumWindowStyle.WS_DISABLED) > 0)
                    {
                        NewWindowStyle |= (uint)EnumWindowStyle.WS_DISABLED;
                    }

                    if ((WindowStyle & (uint)EnumWindowStyle.WS_CHILD) == 0)
                    {
                        NewWindowStyle |= (uint)EnumWindowStyle.WS_CHILD;
                    }

                    if ((WindowStyle & (uint)EnumWindowStyle.WS_POPUP) > 0)
                    {
                        NewWindowStyle |= (uint)EnumWindowStyle.WS_POPUP;
                    }

                    if (SetWindowLong(hwndDialog, (int)EnumWindowInfoType.GWL_STYLE, NewWindowStyle) == 0)
                    {
                        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    }

                   
                   
                    return new HandleRef(this, hWndDialog);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                return new HandleRef(this, IntPtr.Zero);
    }

    • Помечено в качестве ответа VeryHappyCactus 18 января 2011 г. 15:39
    • Снята пометка об ответе VeryHappyCactus 18 января 2011 г. 15:39
    16 января 2011 г. 20:33
  • Все, тему можно закрывать, уже разобрался!
    18 января 2011 г. 15:39
  • Все, разобрался, тему можно закрывать!
    • Помечено в качестве ответа VeryHappyCactus 18 января 2011 г. 15:40
    18 января 2011 г. 15:40