Пример #1
0
        private static IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            const string propertyName = "Sunburst.Win32UI.Control";

            if (msg == WindowMessages.WM_NCCREATE)
            {
                CREATESTRUCT createStruct = Marshal.PtrToStructure <CREATESTRUCT>(lParam);
                NativeMethods.SetProp(hWnd, propertyName, createStruct.lpCreateParams);
            }

            IntPtr instanceHandle = NativeMethods.GetProp(hWnd, propertyName);

            if (instanceHandle == IntPtr.Zero)
            {
                // If we haven't received WM_NCCREATE yet, there's not much we can do here.
                return(NativeMethods.DefWindowProc(hWnd, msg, wParam, lParam));
            }

            ControlNativeWindow instance = (ControlNativeWindow)GCHandle.FromIntPtr(instanceHandle).Target;

            Message m = new Message(hWnd, msg, wParam, lParam);

            instance.Owner.CallWndProc(ref m);

            if (msg == WindowMessages.WM_NCDESTROY)
            {
                GCHandle.FromIntPtr(instanceHandle).Free();
                NativeMethods.RemoveProp(hWnd, propertyName);
            }

            return(m.Result);
        }
Пример #2
0
        public static ControlNativeWindow SubclassWindow(Control owner, IntPtr hWnd, bool ownsHandle)
        {
            if (owner == null)
            {
                throw new ArgumentNullException(nameof(owner));
            }
            if (!NativeMethods.IsWindow(hWnd))
            {
                throw new ArgumentException("not a valid HWND", nameof(hWnd));
            }

            ControlNativeWindow nativeWindow = new ControlNativeWindow(owner, hWnd, ownsHandle);
            IntPtr wndProc = Marshal.GetFunctionPointerForDelegate((WNDPROC)WndProc);

            const int GWLP_WNDPROC = 0;

            nativeWindow.superclassWndProc = NativeMethods.GetWindowLongPtr(hWnd, GWLP_WNDPROC);

            GCHandle gcHandle = GCHandle.Alloc(nativeWindow);

            NativeMethods.SetProp(hWnd, "Sunburst.Win32UI.Control", GCHandle.ToIntPtr(gcHandle));

            NativeMethods.SetWindowLongPtr(hWnd, GWLP_WNDPROC, wndProc);
            return(nativeWindow);
        }
Пример #3
0
        public static ControlNativeWindow Create(Control owner, CreateParams createParams)
        {
            if (owner == null)
            {
                throw new ArgumentNullException(nameof(owner));
            }
            if (createParams == null)
            {
                throw new ArgumentNullException(nameof(createParams));
            }

            ControlNativeWindow nativeWindow = new ControlNativeWindow(owner);
            WindowClass         windowClass  = WindowClass.GetWindowClass(createParams.ClassName, createParams.ClassStyle);
            IntPtr wndProc       = Marshal.GetFunctionPointerForDelegate((WNDPROC)WndProc);
            string fullClassName = windowClass.Register(wndProc, out nativeWindow.superclassWndProc);

            GCHandle gcHandle = GCHandle.Alloc(nativeWindow);

            var frame = createParams.Frame;

            nativeWindow.Handle = NativeMethods.CreateWindowEx(createParams.ExtendedStyle, fullClassName,
                                                               createParams.Caption, createParams.Style, frame.left, frame.top, frame.Width, frame.Height,
                                                               createParams.ParentHandle, IntPtr.Zero, IntPtr.Zero, GCHandle.ToIntPtr(gcHandle));

            return(nativeWindow);
        }