/// <summary> /// The create window. /// </summary> private void CreateWindow() { var instanceHandle = Kernel32Methods.GetModuleHandle(IntPtr.Zero); _windowProc = WindowProc; var wc = new WindowClassEx { Size = (uint)Marshal.SizeOf <WindowClassEx>(), ClassName = "chromelywindow", CursorHandle = User32Helpers.LoadCursor(IntPtr.Zero, SystemCursor.IDC_ARROW), IconHandle = GetIconHandle(), Styles = WindowClassStyles.CS_HREDRAW | WindowClassStyles.CS_VREDRAW, BackgroundBrushHandle = new IntPtr((int)StockObject.WHITE_BRUSH), WindowProc = _windowProc, InstanceHandle = instanceHandle }; var resReg = User32Methods.RegisterClassEx(ref wc); if (resReg == 0) { Log.Error("chromelywindow registration failed"); return; } var styles = GetWindowStyles(_hostConfig.HostPlacement.State); var placement = _hostConfig.HostPlacement; NativeMethods.RECT rect; rect.Left = placement.Left; rect.Top = placement.Top; rect.Right = placement.Left + placement.Width; rect.Bottom = placement.Top + placement.Height; NativeMethods.AdjustWindowRectEx(ref rect, styles.Item1, false, styles.Item2); var hwnd = User32Methods.CreateWindowEx( styles.Item2, wc.ClassName, _hostConfig.HostPlacement.Frameless ? string.Empty : _hostConfig.HostTitle, styles.Item1, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, IntPtr.Zero, IntPtr.Zero, instanceHandle, IntPtr.Zero); if (hwnd == IntPtr.Zero) { Log.Error("chromelywindow creation failed"); return; } User32Methods.ShowWindow(Handle, styles.Item3); User32Methods.UpdateWindow(Handle); }
// This is the initializer WindowProc for the class. It is only called until the NCCREATE message // is arrived, at which point the WindowInstanceInitializerProc is called, which again is only // executed once. It performs nifty trick to replace the WindowProc of the instance, by chaining // WndProc and swapping out base classes to be able to do it without any extra overhead of the // traditional Win32 WndProc methods. private unsafe IntPtr ClassInitializerProc(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam) { try { Debug.WriteLine("[ClassInitializerProc]: " + hwnd); WindowProc winInstanceInitializerProc = null; if (msg == (int)WM.NCCREATE) { var wParamForNcCreate = Marshal.GetFunctionPointerForDelegate(this.m_windowProc); var createStruct = *(CreateStruct *)lParam.ToPointer(); var instancePtr = createStruct.CreateParams; if (instancePtr != IntPtr.Zero) { var winInstance = (WindowCore)GCHandle.FromIntPtr(instancePtr).Target; if (winInstance != null) { winInstanceInitializerProc = winInstance.WindowInstanceInitializerProc; wParam = wParamForNcCreate; } } } return(winInstanceInitializerProc?.Invoke(hwnd, msg, wParam, lParam) ?? this.m_windowProc(hwnd, msg, wParam, lParam)); } catch (Exception ex) { if (!WindowCore.HandleException(ex, null)) { throw; } return(IntPtr.Zero); } }
public WindowFactory(string existingClassName, string targetClassName, IntPtr srcInstanceHandle, IntPtr targetInstanceHandle, ClassInfoMutator mutator) { WindowClassExBlittable classInfo; if (!User32Methods.GetClassInfoEx(srcInstanceHandle, existingClassName, out classInfo)) { throw new Exception("Class is not registered - " + existingClassName); } var className = targetClassName ?? Guid.NewGuid().ToString(); var ciClassName = Marshal.SystemDefaultCharSize == 1 ? Marshal.StringToHGlobalAnsi(className) : Marshal.StringToHGlobalUni(className); classInfo.Size = (uint)Marshal.SizeOf <WindowClassExBlittable>(); classInfo.ClassName = ciClassName; classInfo.InstanceHandle = targetInstanceHandle; try { mutator?.Invoke(ref classInfo); this.ClassName = className; this.InstanceHandle = classInfo.InstanceHandle; this.m_windowProc = Marshal.GetDelegateForFunctionPointer <WindowProc>(classInfo.WindowProc); classInfo.WindowProc = Marshal.GetFunctionPointerForDelegate <WindowProc>(this.ClassInitializerProc); this.RegisterClass(ref classInfo); } finally { Marshal.FreeHGlobal(ciClassName); } }
private void RegisterWindowClass() { _wndProcDelegate = WndProc; var wc = new WindowClassEx { Styles = WindowClassStyles.CS_HREDRAW | WindowClassStyles.CS_VREDRAW | WindowClassStyles.CS_OWNDC, WindowProc = _wndProcDelegate, InstanceHandle = Kernel32.GetModuleHandle(null), CursorHandle = User32.LoadCursor(IntPtr.Zero, (IntPtr)SystemCursor.IDC_ARROW), ClassName = CLASS_NAME }; wc.Size = (uint)Marshal.SizeOf(wc); // Load user icon - if any. wc.IconHandle = User32.LoadImage(Kernel32.GetModuleHandle(null), "#32512", ResourceImageType.IMAGE_ICON, 0, 0, LoadResourceFlags.LR_DEFAULTSIZE | LoadResourceFlags.LR_SHARED); if (wc.IconHandle == IntPtr.Zero) { Kernel32.SetLastError(0); // None loaded - load default. wc.IconHandle = User32.LoadImage(IntPtr.Zero, (IntPtr)SystemIcon.IDI_APPLICATION, ResourceImageType.IMAGE_ICON, 0, 0, LoadResourceFlags.LR_DEFAULTSIZE | LoadResourceFlags.LR_SHARED); } ushort windowClass = User32.RegisterClassEx(ref wc); if (windowClass == 0) { CheckError("Win32: Failed to register window class.", true); } CheckError("Win32: Could not register class."); }
public WndProcOverride(IntPtr wndHandle, string wndClassName) { handle = wndHandle; className = wndClassName; newWndProc = new WindowProc(OverridenWndProc); originalWndProc = User32Methods.SetWindowLongPtr(handle, -4, Marshal.GetFunctionPointerForDelegate(newWndProc)); }
public WindowFactory(string name, WindowClassStyles styles, IntPtr hInstance, IntPtr hIcon, IntPtr hCursor, IntPtr hBgBrush, WindowProc wndProc) { var cache = Cache.Instance; var className = name ?? Guid.NewGuid().ToString(); this.ClassName = className; this.InstanceHandle = hInstance; this.m_windowProc = wndProc ?? DefWindowProc; this.m_classInitializerProcRef = this.ClassInitializerProc; var classInfo = new WindowClassEx { Size = cache.WindowClassExSize, ClassName = className, CursorHandle = hCursor, IconHandle = hIcon, Styles = styles, BackgroundBrushHandle = hBgBrush, WindowProc = this.m_classInitializerProcRef, InstanceHandle = hInstance }; this.RegisterClass(ref classInfo); }
void INativeConnectable.ConnectWindowProc(IntPtr baseWindowProcPtr) { this.m_baseWindowProcPtr = baseWindowProcPtr == IntPtr.Zero ? this.GetParam(WindowLongFlags.GWLP_WNDPROC) : baseWindowProcPtr; this.m_instanceWindowProc = this.WindowProc; this.SetParam(WindowLongFlags.GWLP_WNDPROC, Marshal.GetFunctionPointerForDelegate(this.m_instanceWindowProc)); this.OnSourceConnected(); }
public WndProcOverride(IntPtr wndHandle, string wndClassName, ChromiumWebBrowser mBrowser, IntPtr parentHandle) { browser = mBrowser; mainHandle = parentHandle; handle = wndHandle; className = wndClassName; newWndProc = new WindowProc(OverridenWndProc); originalWndProc = User32Methods.SetWindowLongPtr(handle, -4, Marshal.GetFunctionPointerForDelegate(newWndProc)); }
public void Init(string title, int width, int height, bool vsync, bool fullscreen) { if (m_Info != null) { throw new InvalidOperationException("application already initialized"); } m_Info = new ApplicationInfo { Title = title, Width = width, Height = height, VSync = vsync, FullScreen = fullscreen }; IntPtr hInstance = Kernel32Methods.GetModuleHandle(IntPtr.Zero); m_WindowProc = WindowProc; var wc = new WindowClassEx { Size = (uint)Marshal.SizeOf <WindowClassEx>(), ClassName = "MainWindow", CursorHandle = User32Helpers.LoadCursor(IntPtr.Zero, SystemCursor.IDC_ARROW), IconHandle = User32Helpers.LoadIcon(IntPtr.Zero, SystemIcon.IDI_APPLICATION), Styles = WindowClassStyles.CS_HREDRAW | WindowClassStyles.CS_VREDRAW | WindowClassStyles.CS_OWNDC, BackgroundBrushHandle = new IntPtr((int)StockObject.WHITE_BRUSH), WindowProc = m_WindowProc, InstanceHandle = hInstance }; if (User32Methods.RegisterClassEx(ref wc) == 0) { throw new ExternalException("window registration failed"); } NetCoreEx.Geometry.Rectangle size = new NetCoreEx.Geometry.Rectangle(0, 0, width, height); User32Methods.AdjustWindowRectEx(ref size, WindowStyles.WS_OVERLAPPEDWINDOW | WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS, false, WindowExStyles.WS_EX_APPWINDOW | WindowExStyles.WS_EX_WINDOWEDGE); m_hWnd = User32Methods.CreateWindowEx(WindowExStyles.WS_EX_APPWINDOW | WindowExStyles.WS_EX_WINDOWEDGE, wc.ClassName, title, WindowStyles.WS_OVERLAPPEDWINDOW | WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS, (int)CreateWindowFlags.CW_USEDEFAULT, (int)CreateWindowFlags.CW_USEDEFAULT, size.Right + (-size.Left), size.Bottom + (-size.Top), IntPtr.Zero, IntPtr.Zero, hInstance, IntPtr.Zero); if (m_hWnd == IntPtr.Zero) { throw new ExternalException("window creation failed"); } User32Methods.ShowWindow(m_hWnd, ShowWindowCommands.SW_SHOWNORMAL); User32Methods.UpdateWindow(m_hWnd); Context.Instance.Init(m_hWnd, m_Info); Renderer.Instance.Init(); Script.LuaEngine.Instance.Init(); }
public static IntPtr SetWindowLongPtr(IntPtr hWnd, WindowLongFlags nIndex, WindowProc dwNewLong) { if (IntPtr.Size == 8) { return(SetWindowLongPtr64(hWnd, nIndex, dwNewLong)); } else { return(new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong))); }
public WinInputBase() { WindowProc = WindowProcedure; InputThread = new Thread(ProcessEvents); InputThread.SetApartmentState(ApartmentState.STA); InputThread.IsBackground = true; InputThread.Start(); InputReady.WaitOne(); }
public void Disable() { if (_newCallback == null) { return; } Kernel32.SetWindowLongPtr(Handle, GWL_WNDPROC, _oldCallback); _newCallback = null; IsEnabled = false; }
private ChromeWidgetMessageInterceptor(IntPtr window, IntPtr childHandle, IFramelessOptions framelessOptions, Action <Message> forwardAction) { _parentHandle = window; _childHandle = childHandle; _childWndProc = WndProc; _framelessOptions = framelessOptions; _childWndProcHandle = Marshal.GetFunctionPointerForDelegate(_childWndProc); _oldWindProc = NativeMethods.SetWindowLongPtr(_childHandle, (int)WindowLongFlags.GWLP_WNDPROC, _childWndProcHandle); NativeMethods.SetWindowLongPtr(_childHandle, (int)WindowLongFlags.GWLP_USERDATA, _oldWindProc); _forwardAction = forwardAction; }
public void Initialize() { try { WindowProc.KillProcess("iexplore"); ie = new SHDocVw.InternetExplorer(); } catch (Exception ex) { SharpLog.Error("WebProcInit", ex.ToString()); } }
public override User32WindowClass CreateWindowClass() { CallbackDelegate = new WindowProc(this.Callback); User32WindowClass aClass = new User32WindowClass(gWindowClassName, (User32.CS_HREDRAW | User32.CS_VREDRAW | User32.CS_GLOBALCLASS | User32.CS_OWNDC), CallbackDelegate); return aClass; }
public WindowFactory(ref WindowClassEx classEx) { this.ClassName = classEx.ClassName; this.InstanceHandle = classEx.InstanceHandle; this.m_windowProc = classEx.WindowProc ?? DefWindowProc; this.m_classInitializerProcRef = this.ClassInitializerProc; // Leave the reference untouched. So, use a copy for the modified registration. var classExClone = classEx; classExClone.WindowProc = this.m_classInitializerProcRef; this.RegisterClass(ref classExClone); }
//Window create public AVCustomWindow(string windowTitle, int windowWidth, int windowHeight, bool windowVisible) { try { //Set window variables this.windowWidth = windowWidth; this.windowHeight = windowHeight; //Create window procedure windowProc = WindowProcessMessage; windowProcPointer = Marshal.GetFunctionPointerForDelegate(windowProc); //Create window class WNDCLASSEX windowClassEx = new WNDCLASSEX { cbSize = WNDCLASSEX.classSize, style = 0, lpfnWndProc = windowProcPointer, cbClsExtra = 0, cbWndExtra = 0, hInstance = IntPtr.Zero, hIcon = IntPtr.Zero, hCursor = IntPtr.Zero, hbrBackground = IntPtr.Zero, lpszMenuName = windowTitle, lpszClassName = Environment.TickCount.ToString(), hIconSm = IntPtr.Zero }; //Register window class RegisterClassEx(ref windowClassEx); //Create window WindowStyles windowStyles = WindowStyles.WS_OVERLAPPEDWINDOW; WindowStylesEx windowStylesEx = WindowStylesEx.WS_EX_LEFT; windowParent = CreateWindowEx(windowStylesEx, windowClassEx.lpszClassName, windowClassEx.lpszMenuName, windowStyles, 0, 0, this.windowHeight, this.windowWidth, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); //Show window if (windowVisible) { ShowWindow(windowParent, WindowShowCommand.Show); } Debug.WriteLine("Created custom window: " + windowParent); } catch (Exception ex) { Debug.WriteLine("Failed to create custom window: " + ex.Message); } }
public void Enable() { _newCallback = OnWndProc; _oldCallback = Kernel32.SetWindowLongPtr(Handle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(_newCallback)); if (_oldCallback == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } IsEnabled = true; }
public void Close() { try { if (ie != null) { //ie.Quit(); WindowProc.KillProcess("iexplore"); } } catch (Exception ex) { SharpLog.Error("WebProcClose", ex.ToString()); } }
internal static void InitializeWindowClass() { string MenuName; if (string.IsNullOrEmpty(_className)) { _className = WindowHelper.GenerateRandomClass(); MenuName = WindowHelper.GenerateRandomTitle(); _windowProc = OverlayWindow.WindowProcedure; _windowProcAddress = Marshal.GetFunctionPointerForDelegate(_windowProc); } else { return; } while (true) { var wndClassEx = new WindowClassEx { Size = WindowClassEx.NativeSize(), Style = 0, WindowProc = _windowProcAddress, ClsExtra = 0, WindowExtra = 0, Instance = IntPtr.Zero, Icon = IntPtr.Zero, Curser = IntPtr.Zero, Background = IntPtr.Zero, MenuName = MenuName, ClassName = _className, IconSm = IntPtr.Zero }; if (User32.RegisterClassEx(ref wndClassEx) != 0) { break; } else { // already taken name? _className = WindowHelper.GenerateRandomClass(); } } }
// Public methods /////////////////////// public static void Start() { if (thread != null) { return; } thread = Thread.Run("event loop", () => { var moduleHandle = GetModuleHandle(IntPtr.Zero); wndProcDelegate = OnWndProc; var wndClass = WndClassEx.New(); wndClass.lpszClassName = CLASS_NAME; wndClass.lpfnWndProc = wndProcDelegate; wndClass.hInstance = moduleHandle; var classAtom = RegisterClassExW(ref wndClass); if (classAtom == IntPtr.Zero) { var lastError = Marshal.GetLastWin32Error(); Log.Error($"Could not register window class (code {lastError})"); return; } handle = CreateWindowExW( 0, classAtom, String.Empty, 0, 0, 0, 0, 0, HWND_MESSAGE, IntPtr.Zero, moduleHandle, IntPtr.Zero ); if (handle == IntPtr.Zero) { var lastError = Marshal.GetLastWin32Error(); Log.Error($"Could create window with class {classAtom} (code {lastError})"); return; } System.Windows.Forms.Application.Run(); }); }
/// <summary>Initializes a new instance of the <see cref="WindowClass"/> class and registers the class name.</summary> /// <param name="className"> /// <para> /// A string that specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, /// or any of the predefined control-class names. /// </para> /// <para> /// The maximum length for <c>lpszClassName</c> is 256. If <c>lpszClassName</c> is greater than the maximum length, the /// RegisterClassEx function will fail. /// </para> /// </param> /// <param name="hInst">A handle to the instance that contains the window procedure for the class.</param> /// <param name="wndProc"> /// A pointer to the window procedure. You must use the CallWindowProc function to call the window procedure. For more information, /// see WindowProc. /// </param> /// <param name="styles">The class style(s). This member can be any combination of the Class Styles.</param> /// <param name="hIcon"> /// A handle to the class icon. This member must be a handle to an icon resource. If this member is <c>NULL</c>, the system provides /// a default icon. /// </param> /// <param name="hSmIcon"> /// A handle to a small icon that is associated with the window class. If this member is <c>NULL</c>, the system searches the icon /// resource specified by the <c>hIcon</c> member for an icon of the appropriate size to use as the small icon. /// </param> /// <param name="hCursor"> /// A handle to the class cursor. This member must be a handle to a cursor resource. If this member is <c>NULL</c>, an application /// must explicitly set the cursor shape whenever the mouse moves into the application's window. /// </param> /// <param name="hbrBkgd"> /// A handle to the class background brush. This member can be a handle to the brush to be used for painting the background, or it /// can be a color value. A color value must be one of the following standard system colors (the value 1 must be added to the chosen color). /// <para> /// The system automatically deletes class background brushes when the class is unregistered by using <see cref="UnregisterClass"/>. /// An application should not delete these brushes. /// </para> /// <para> /// When this member is <c>NULL</c>, an application must paint its own background whenever it is requested to paint in its client /// area. To determine whether the background must be painted, an application can either process the WM_ERASEBKGND message or test /// the <c>fErase</c> member of the PAINTSTRUCT structure filled by the BeginPaint function. /// </para> /// </param> /// <param name="menuName"> /// A string that specifies the resource name of the class menu, as the name appears in the resource file. If you use an integer to /// identify the menu, use the MAKEINTRESOURCE macro. If this member is <c>NULL</c>, windows belonging to this class have no default menu. /// </param> /// <param name="extraBytes"> /// The number of extra bytes to allocate following the window-class structure. The system initializes the bytes to zero. /// </param> /// <param name="extraWinBytes"> /// The number of extra bytes to allocate following the window instance. The system initializes the bytes to zero. If an application /// uses <c>WNDCLASSEX</c> to register a dialog box created by using the <c>CLASS</c> directive in the resource file, it must set /// this member to <c>DLGWINDOWEXTRA</c>. /// </param> public WindowClass(string className, HINSTANCE hInst, WindowProc wndProc, WindowClassStyles styles = 0, HICON hIcon = default, HICON hSmIcon = default, HCURSOR hCursor = default, HBRUSH hbrBkgd = default, string menuName = null, int extraBytes = 0, int extraWinBytes = 0) { // TODO: Find way to hold on to wndProc ref wc = new WNDCLASSEX { cbSize = (uint)Marshal.SizeOf(typeof(WNDCLASSEX)), lpfnWndProc = wndProc, hInstance = hInst, lpszClassName = className, style = styles, hIcon = hIcon, hIconSm = hSmIcon, hCursor = hCursor, hbrBackground = hbrBkgd, lpszMenuName = menuName, cbClsExtra = extraBytes, cbWndExtra = extraWinBytes, }; Atom = Win32Error.ThrowLastErrorIfNull(Macros.MAKEINTATOM(RegisterClassEx(wc))); }
/// <summary> /// Releases all resources used by this OverlayWindow. /// </summary> /// <param name="disposing">A Boolean value indicating whether this is called from the destructor.</param> protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (_isInitialized) { DestroyWindow(); } SizeChanged = null; PositionChanged = null; VisibilityChanged = null; _windowThread = null; _windowProc = null; _title = null; _className = null; disposedValue = true; } }
public NativeWindow(bool isDesignMode) { if (isDesignMode) { return; } ClassName = $"{nameof(NotifyIcon)}-{DateTime.Now.Ticks}"; _messageReceiver = OnMessageReceived; _creationMessage = Native.RegisterWindowMessageW("TaskbarCreated"); var windowClass = new WindowClassEx() { cbSize = (uint)Marshal.SizeOf(typeof(WindowClassEx)), lpszClassName = ClassName, lpfnWndProc = _messageReceiver }; Native.RegisterClassExW(ref windowClass); Handle = Native.CreateWindowExW(0, ClassName, string.Empty, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); }
public WinapiHostFactory(string name, WindowClassStyles styles, IntPtr hInstance, IntPtr hIcon, IntPtr hCursor, IntPtr hBgBrush, WindowProc wndProc) : base(name, styles, hInstance, hIcon, hCursor, hBgBrush, wndProc) { }
/// <summary> /// The create window. /// </summary> private void CreateWindow() { var instanceHandle = Kernel32Methods.GetModuleHandle(IntPtr.Zero); _windowProc = WindowProc; var wc = new WindowClassEx { Size = (uint)Marshal.SizeOf <WindowClassEx>(), ClassName = "chromelywindow", CursorHandle = User32Helpers.LoadCursor(IntPtr.Zero, SystemCursor.IDC_ARROW), IconHandle = GetIconHandle(), Styles = WindowClassStyles.CS_HREDRAW | WindowClassStyles.CS_VREDRAW, BackgroundBrushHandle = new IntPtr((int)StockObject.WHITE_BRUSH), WindowProc = _windowProc, InstanceHandle = instanceHandle }; var resReg = User32Methods.RegisterClassEx(ref wc); if (resReg == 0) { Log.Error("chromelywindow registration failed"); return; } var styles = GetWindowStyles(_hostConfig.HostPlacement.State); var placement = _hostConfig.HostPlacement; NativeMethods.RECT rect; rect.Left = placement.Left; rect.Top = placement.Top; rect.Right = placement.Left + placement.Width; rect.Bottom = placement.Top + placement.Height; NativeMethods.AdjustWindowRectEx(ref rect, styles.Item1, false, styles.Item2); var hwnd = User32Methods.CreateWindowEx( styles.Item2, wc.ClassName, _hostConfig.HostPlacement.Frameless ? string.Empty : _hostConfig.HostTitle, styles.Item1, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, IntPtr.Zero, IntPtr.Zero, instanceHandle, IntPtr.Zero); if (hwnd == IntPtr.Zero) { Log.Error("chromelywindow creation failed"); return; } if (_hostConfig.HostPlacement.KioskMode) { //// Set new window style and size. var windowHDC = User32Methods.GetDC(Handle); var fullscreenWidth = Gdi32Methods.GetDeviceCaps(windowHDC, (int)DeviceCapsParams.HORZRES); var fullscreenHeight = Gdi32Methods.GetDeviceCaps(windowHDC, (int)DeviceCapsParams.VERTRES); User32Methods.ReleaseDC(Handle, windowHDC); User32Methods.SetWindowLongPtr(Handle, (int)WindowLongFlags.GWL_STYLE, (IntPtr)styles.Item1); User32Methods.SetWindowLongPtr(Handle, (int)WindowLongFlags.GWL_EXSTYLE, (IntPtr)styles.Item2); User32Methods.SetWindowPos(Handle, (IntPtr)HwndZOrder.HWND_TOP, 0, 0, fullscreenWidth, fullscreenHeight, WindowPositionFlags.SWP_NOZORDER | WindowPositionFlags.SWP_FRAMECHANGED); User32Methods.ShowWindow(Handle, ShowWindowCommands.SW_MAXIMIZE); try { this._hookID = NativeMethods.SetHook(_hookCallback); } catch { DetachKeyboardHook(); } } else { User32Methods.ShowWindow(Handle, styles.Item3); } User32Methods.UpdateWindow(Handle); User32Methods.RegisterHotKey(IntPtr.Zero, 1, KeyModifierFlags.MOD_CONTROL, VirtualKey.L); }
void SubClass() { IntPtr hWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; NewWindowProc = new WindowProc(MyWindowProc); DefWindowProc = SetWindowLongPtr(hWnd, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(NewWindowProc)); }
public WindowsApplicationHost(Game game) : base(game) { // TODO: Add options for EnableHighResolution if (PlatformDetection.IsWindows10Version1703OrGreater()) { if (SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) || SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE)) { Log.Debug("Win32", "Enabled per monitor DPI awareness."); } } else if (PlatformDetection.IsWindows8Point1OrGreater()) { if (SetProcessDpiAwareness(ProcessDpiAwareness.PerMonitorDpiAware) == 0) { Log.Debug("Enabled per monitor DPI awareness."); } else { Log.Error("Failed to set process DPI awareness"); } } else { SetProcessDPIAware(); } //EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, // (IntPtr monitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr data) => // { // var monitorInfo = new MonitorInfo // { // Size = (uint)Marshal.SizeOf<MonitorInfo>() // }; // if (!GetMonitorInfo(monitor, ref monitorInfo)) // throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); // Rectangle bounds = monitorInfo.MonitorRect; // RECT workingArea = monitorInfo.WorkRect; // return true; // }, IntPtr.Zero); // Register main class. _wndProc = ProcessWindowMessage; var wndClassEx = new WindowClassEx { Size = (uint)Unsafe.SizeOf <WindowClassEx>(), Styles = WindowClassStyles.CS_HREDRAW | WindowClassStyles.CS_VREDRAW | WindowClassStyles.CS_OWNDC, WindowProc = _wndProc, InstanceHandle = HInstance, CursorHandle = LoadCursor(IntPtr.Zero, SystemCursor.IDC_ARROW), BackgroundBrushHandle = IntPtr.Zero, IconHandle = IntPtr.Zero, ClassName = WndClassName, }; var atom = RegisterClassEx(ref wndClassEx); if (atom == 0) { throw new InvalidOperationException( $"Failed to register window class. Error: {Marshal.GetLastWin32Error()}" ); } MainView = new Win32Window(this, "Vortice", 800, 600); }
static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, WindowLongFlags nIndex, WindowProc dwNewLong);
static extern int SetWindowLong32(IntPtr hWnd, WindowLongFlags nIndex, WindowProc dwNewLong);
public static IntPtr CreateWindow(Func <Message, IntPtr> processMessage, string className, string title = null) { if (HandleWindow != IntPtr.Zero) { return(HandleWindow); } WindowProc = new WindowProc((handleWindow, wm, wParam, lParam) => { IntPtr result = IntPtr.Zero; if (processMessage != null) { var message = new Message { handle = handleWindow, message = wm, wParam = wParam, lParam = lParam, };; result = processMessage(message); } if (result == IntPtr.Zero) { return(Win32.DefWindowProc(handleWindow, wm, wParam, lParam)); } else { return(result); } }); WindowClassEx = new WindowClassEx() { size = Marshal.SizeOf(typeof(WindowClassEx)), style = CS.HREDRAW | CS.VREDRAW, windowProc = Marshal.GetFunctionPointerForDelegate(WindowProc), classExtra = 0, windowExtra = 0, handleInstance = IntPtr.Zero, handleIcon = IntPtr.Zero, handleCursor = IntPtr.Zero, handlebrBackground = IntPtr.Zero, menuName = null, className = className, handleIconSm = IntPtr.Zero, }; Tracable.LogLine("windowClassEx {0}", WindowClassEx.ToString()); var registeredClassEx = Win32.RegisterClassEx(ref WindowClassEx); Tracable.LogLine("registeredClassEx {0}", registeredClassEx.ToString()); if (registeredClassEx == 0) { var error = Win32.GetLastError(); Tracable.LogLine("error {0}", error.ToRepr()); Win32.UnregisterClass(className, IntPtr.Zero); return(IntPtr.Zero); } HandleWindow = Win32.CreateWindowEx ( WS_EX.NONE, registeredClassEx, title ?? WindowClassEx.className, WS.NONE, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, WindowClassEx.handleInstance, IntPtr.Zero ); Tracable.LogLine("handle {0}", HandleWindow.ToRepr()); if (HandleWindow == null) { var error = Win32.GetLastError(); Tracable.LogLine("error {0}", error.ToRepr()); } return(HandleWindow); }
public Hook() { m_windowProc = new WindowProc(WndProc); }
public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, WindowProc newProc);
public static extern IntPtr CallWindowProc(WindowProc lpPrevWndFunc, IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
public static IntPtr SetWindowProc(IntPtr hWnd, WindowProc aProc) { IntPtr oldProc = SetWindowLongPtr(hWnd, User32.GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(aProc)); return oldProc; }