예제 #1
0
        private static void Main()
        {
            const string szAppName = "OwnDraw";

            ModuleInstance  module   = ModuleInstance.GetModuleForType(typeof(Program));
            WindowClassInfo wndclass = new WindowClassInfo
            {
                Style           = ClassStyle.HorizontalRedraw | ClassStyle.VerticalRedraw,
                WindowProcedure = WindowProcedure,
                Instance        = module,
                Icon            = IconId.Application,
                Cursor          = CursorId.Arrow,
                Background      = StockBrush.White,
                ClassName       = szAppName
            };

            Windows.RegisterClass(ref wndclass);

            WindowHandle window = Windows.CreateWindow(
                szAppName,
                "Owner-Draw Button Demo",
                WindowStyles.OverlappedWindow,
                bounds: Windows.DefaultBounds,
                instance: module);

            window.ShowWindow(ShowWindowCommand.Normal);
            window.UpdateWindow();

            while (Windows.GetMessage(out WindowMessage message))
            {
                Windows.TranslateMessage(ref message);
                Windows.DispatchMessage(ref message);
            }
        }
예제 #2
0
        public void WindowCallback_SendMessage()
        {
            WindowClassInfo myClass = new WindowClassInfo
            {
                ClassName       = "WindowCallback_SendMessage",
                WindowProcedure = (window, message, wParam, lParam) =>
                {
                    return(42);
                }
            };

            Atom atom = Windows.RegisterClass(ref myClass);

            atom.IsValid.Should().BeTrue();

            try
            {
                WindowHandle window = Windows.CreateWindow(atom, style: WindowStyles.Minimize | WindowStyles.Diabled);
                window.IsInvalid.Should().BeFalse();

                try
                {
                    window.SendMessage(MessageType.Activate).Should().Be((LResult)42);
                }
                finally
                {
                    window.DestroyWindow();
                }
            }
            finally
            {
                Windows.UnregisterClass(atom);
            }
        }
예제 #3
0
        public void RegisterClass_UnregisterClassName()
        {
            WindowClassInfo myClass = new WindowClassInfo
            {
                ClassName       = "RegisterClass_UnregisterClassName",
                Style           = ClassStyle.HorizontalRedraw,
                WindowProcedure = CallDefaultProcedure
            };

            Atom atom = Windows.RegisterClass(ref myClass);

            atom.IsValid.Should().BeTrue();

            try
            {
                var info = Windows.GetClassInfo(Modules.GetModuleHandle(null), "RegisterClass_UnregisterClassName");
                info.ClassName.Should().Be("RegisterClass_UnregisterClassName");
                info.ClassAtom.Should().Be(Atom.Null);
                info.Style.Should().Be(ClassStyle.HorizontalRedraw);
            }
            finally
            {
                Windows.UnregisterClass("RegisterClass_UnregisterClassName", null);
                Action action =
                    () => Windows.GetClassInfo(Modules.GetModuleHandle(null), "RegisterClass_UnregisterClassName");
                action.Should().Throw <IOException>().And
                .HResult.Should().Be((int)WindowsError.ERROR_CLASS_DOES_NOT_EXIST.ToHResult());
            }
        }
예제 #4
0
        public Win32Window()
        {
            instance = GetModuleHandle(null);
            enabled  = true;

            var classStyle = WindowClassStyle.HorizontalRedraw | WindowClassStyle.VerticalRedraw | WindowClassStyle.PrivateDeviceContext;

            windowCount++;
            var className = "Window" + windowCount;

            MainWindowProcedureDelegate = MainWindowProcedure;

            var info = new WindowClassInfo()
            {
                Size      = (uint)Marshal.SizeOf <WindowClassInfo>(),
                Style     = classStyle,
                Procedure = MainWindowProcedureDelegate,
                Instance  = instance,
                Cursor    = LoadCursor(IntPtr.Zero, new IntPtr(32512)),
                ClassName = className
            };

            var atom = RegisterClass(ref info);

            if (atom == 0)
            {
                throw new MulionException($"Unable to create window class ({GetErrorMessage(GetLastError())})");
            }

            RegenerateStyle();

            handle = CreateWindow(0, className, title, windowStyle, DefaultWindowPosition, DefaultWindowPosition, 0, 0, IntPtr.Zero, IntPtr.Zero, instance, IntPtr.Zero);

            if (handle == IntPtr.Zero)
            {
                throw new MulionException($"Unable to create window ({GetErrorMessage(GetLastError())})");
            }

            var topLeft = new WinPoint();

            if (!ClientToScreen(handle, ref topLeft))
            {
                throw new MulionException($"Unable to retrieve client are for window ({GetErrorMessage(GetLastError())})");
            }

            var bottomRight = (WinPoint)(Point)Bounds.Size;

            if (!ClientToScreen(handle, ref bottomRight))
            {
                throw new MulionException($"Unable to retrieve client are for window ({GetErrorMessage(GetLastError())})");
            }

            bounds = new Rectangle((Point)topLeft, (Size)(Point)bottomRight - (Size)(Point)topLeft);
        }
예제 #5
0
        static void Main()
        {
            // Hack for launching as a .NET Core Windows Application
            WInterop.Console.Console.TryFreeConsole();

            const string szAppName = "RandRect";

            ModuleInstance  module   = ModuleInstance.GetModuleForType(typeof(Program));
            WindowClassInfo wndclass = new WindowClassInfo
            {
                Style           = ClassStyle.HorizontalRedraw | ClassStyle.VerticalRedraw,
                WindowProcedure = WindowProcedure,
                Instance        = module,
                Icon            = IconId.Application,
                Cursor          = CursorId.Arrow,
                Background      = StockBrush.White,
                ClassName       = szAppName
            };

            Windows.RegisterClass(ref wndclass);

            WindowHandle window = Windows.CreateWindow(
                szAppName,
                "Random Rectangles",
                WindowStyles.OverlappedWindow,
                bounds: Windows.DefaultBounds,
                instance: module);

            window.ShowWindow(ShowWindowCommand.Normal);
            window.UpdateWindow();

            while (true)
            {
                if (Windows.PeekMessage(out WindowMessage message, options: PeekMessageOptions.Remove))
                {
                    if (message.Type == MessageType.Quit)
                    {
                        break;
                    }
                    Windows.TranslateMessage(ref message);
                    Windows.DispatchMessage(ref message);
                }

                // We're crazy fast 20 years past the source sample,
                // sleeping to make this a bit more interesting.
                Thread.Sleep(100);
                DrawRectangle(window);
            }
        }
예제 #6
0
        public void WindowCallback_Subclass()
        {
            int             value   = 42;
            WindowClassInfo myClass = new WindowClassInfo
            {
                ClassName       = "WindowCallback_Subclass",
                WindowProcedure = (window, message, wParam, lParam) =>
                {
                    return(value);
                }
            };

            Atom atom = Windows.RegisterClass(ref myClass);

            atom.IsValid.Should().BeTrue();

            try
            {
                WindowHandle window = Windows.CreateWindow(atom, style: WindowStyles.Minimize | WindowStyles.Diabled);
                window.IsInvalid.Should().BeFalse();

                try
                {
                    Windows.SendMessage(window, MessageType.Activate, 0, 0).Should().Be((LResult)42);

                    WNDPROC         previous = default;
                    WindowProcedure subClass = (w, m, wParam, lParam) =>
                    {
                        return(Windows.CallWindowProcedure(previous, w, m, wParam, lParam));
                    };

                    value    = 1999;
                    previous = Windows.SetWindowProcedure(window, subClass);
                    Windows.SendMessage(window, MessageType.Activate, 0, 0).Should().Be((LResult)1999);
                    GC.KeepAlive(subClass);
                }
                finally
                {
                    Windows.DestroyWindow(window);
                }
            }
            finally
            {
                Windows.UnregisterClass(atom, null);
            }
        }
예제 #7
0
        public void RegisterClass_GetSetClassLong()
        {
            // Some docs claim that 40 is the max, but that isn't true (at least in recent OSes)
            // https://msdn.microsoft.com/en-us/library/windows/desktop/ms633574.aspx
            WindowClassInfo myClass = new WindowClassInfo
            {
                ClassName       = "RegisterClass_GetSetClassLong",
                Style           = ClassStyle.HorizontalRedraw,
                WindowProcedure = CallDefaultProcedure,
                ClassExtraBytes = 80
            };

            Atom atom = Windows.RegisterClass(ref myClass);

            atom.IsValid.Should().BeTrue();

            try
            {
                WindowHandle window = Windows.CreateWindow(
                    atom,
                    "RegisterClass_GetSetClassLong_Window",
                    WindowStyles.Diabled | WindowStyles.Minimize);

                window.IsInvalid.Should().BeFalse();

                try
                {
                    var info = Windows.GetClassInfo(Modules.GetModuleHandle(null), atom);
                    info.ClassExtraBytes.Should().Be(80);

                    IntPtr result = Windows.SetClassLong(window, (ClassLong)72, (IntPtr)0x0000BEEF);
                    result.Should().Be(IntPtr.Zero);

                    window.GetClassLong((ClassLong)72).Should().Be((IntPtr)0x0000BEEF);
                }
                finally
                {
                    window.DestroyWindow();
                }
            }
            finally
            {
                Windows.UnregisterClass(atom, null);
            }
        }
예제 #8
0
        public void RegisterClass_UnregisterActiveWindow()
        {
            WindowClassInfo myClass = new WindowClassInfo
            {
                ClassName       = "RegisterClass_UnregisterActiveWindow",
                WindowProcedure = CallDefaultProcedure,
            };

            Atom atom = Windows.RegisterClass(ref myClass);

            atom.IsValid.Should().BeTrue();

            try
            {
                WindowHandle window = Windows.CreateWindow(
                    atom,
                    "RegisterClass_UnregisterActiveWindow",
                    WindowStyles.Diabled | WindowStyles.Minimize);

                window.IsInvalid.Should().BeFalse();

                try
                {
                    Action action = () => Windows.UnregisterClass(atom, null);
                    action.Should().Throw <IOException>().And
                    .HResult.Should().Be((int)WindowsError.ERROR_CLASS_HAS_WINDOWS.ToHResult());
                }
                finally
                {
                    window.DestroyWindow();
                }
            }
            finally
            {
                Windows.UnregisterClass(atom, null);
            }
        }
예제 #9
0
        static void Main()
        {
            // Hack for launching as a .NET Core Windows Application
            WInterop.Console.Console.TryFreeConsole();

            const string szAppName = "HelloWin";

            ModuleInstance  module   = ModuleInstance.GetModuleForType(typeof(Program));
            WindowClassInfo wndclass = new WindowClassInfo
            {
                Style           = ClassStyle.HorizontalRedraw | ClassStyle.VerticalRedraw,
                WindowProcedure = WindowProcedure,
                Instance        = module,
                Icon            = IconId.Application,
                Cursor          = CursorId.Arrow,
                Background      = StockBrush.White,
                ClassName       = szAppName
            };

            Windows.RegisterClass(ref wndclass);

            WindowHandle window = Windows.CreateWindow(
                szAppName,
                "The Hello Program",
                WindowStyles.OverlappedWindow,
                instance: module,
                bounds: Windows.DefaultBounds);

            window.ShowWindow(ShowWindowCommand.Normal);
            window.UpdateWindow();

            while (Windows.GetMessage(out WindowMessage message))
            {
                Windows.TranslateMessage(ref message);
                Windows.DispatchMessage(ref message);
            }
        }
예제 #10
0
 public static extern ushort RegisterClass(ref WindowClassInfo classInfo);