protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); _hwndSoure = System.Windows.Interop.HwndSource.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle); _hwndSoure?.AddHook(HwndHook); }
protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); System.Windows.Interop.HwndSource hwndSource = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource; if (hwndSource != null) { hwndSource.AddHook(new System.Windows.Interop.HwndSourceHook(Hook)); } }
protected override void OnActivated(EventArgs e) { base.OnActivated(e); hwnd = new System.Windows.Interop.WindowInteropHelper(this); if (hwnd.Handle != null && hwnd.Handle != IntPtr.Zero) { nativeSource = System.Windows.Interop.HwndSource.FromHwnd(hwnd.Handle); nativeSource.AddHook(OnWindowMessage); } }
void SourceInitializedHandler(object sender, EventArgs e) { IntPtr handle = (new System.Windows.Interop.WindowInteropHelper(this)).Handle; System.Windows.Interop.HwndSource hwndSource = System.Windows.Interop.HwndSource.FromHwnd(handle); if (hwndSource != null) { hwndSource.AddHook(WindowProc); } }
private void mainWindow_SourceInitialized(object sender, EventArgs e) { IntPtr windowHandle = (new System.Windows.Interop.WindowInteropHelper(this)).Handle; System.Windows.Interop.HwndSource src = System.Windows.Interop.HwndSource.FromHwnd(windowHandle); src.AddHook(new System.Windows.Interop.HwndSourceHook(WndProc)); var spectroVM = (SpectrometerViewModel)gridSpectrometer.DataContext; spectroVM.MainWindowHandleForAvantesCallback = windowHandle; }
private void Window_SourceInitialized(object sender, EventArgs e) { log.DebugFormat("ContentControlToolBarView Window_SourceInitialized {0}", this.GetHashCode()); IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper((Window)sender).Handle; int value = GetWindowLong(hwnd, GWL_STYLE); SetWindowLong(hwnd, GWL_STYLE, (int)(value & ~WS_MAXIMIZEBOX)); System.Windows.Interop.HwndSource hwndSource = (System.Windows.Interop.HwndSource)System.Windows.Interop.HwndSource.FromVisual((Window)sender); hwndSource.AddHook(DragHook); }
private void MainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e) { try { System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle); source.AddHook(new System.Windows.Interop.HwndSourceHook(WndProc)); FreeEcho.FEHotKeyWPF.HotKeyProcessing.RegisterHotKey(source.Handle, true, false, true, false, 'A', 0); FreeEcho.FEHotKeyWPF.HotKeyProcessing.RegisterHotKey(source.Handle, false, true, false, true, 'B', 1); } catch { } }
private void InitSysMenu() { /// Get the Handle for the Forms System Menu IntPtr systemMenuHandle = GetSystemMenu(Handle, false); /// Create our new System Menu items just before the Close menu item InsertMenu(systemMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty); // <-- Add a menu seperator InsertMenu(systemMenuHandle, 6, MF_BYPOSITION, _AboutSysMenuID, "About"); InsertMenu(systemMenuHandle, 7, MF_BYPOSITION, _CloseSysMenuID, "Close"); // Attach our WndProc handler to this Window System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(Handle); source.AddHook(new System.Windows.Interop.HwndSourceHook(WndProc)); }
// Hotkey events must be assigned here, otherwise they don't work protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); // Get hardware handle to use for hotkeys var helper = new System.Windows.Interop.WindowInteropHelper(this); _source = System.Windows.Interop.HwndSource.FromHwnd(helper.Handle); _source.AddHook(HwndHook); // Register our selected hotkeys for (Int32 i = 0; i < AvailableLayers; ++i) { int id = hotKeyManager.Add(Constants.CTRL, System.Windows.Forms.Keys.D1 + i, this); hotKeyEventToKeyboardLayer[id] = i; } }
public void RegisterForPnpEvents() { Win32Api.DEV_BROADCAST_DEVICEINTERFACE devBroadcastDeviceInterface = new Win32Api.DEV_BROADCAST_DEVICEINTERFACE(); IntPtr devBroadcastDeviceInterfaceBuffer; IntPtr deviceNotificationHandle; System.Windows.Interop.HwndSource hwndSource = System.Windows.Interop.HwndSource.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle); hwndSource.AddHook(new System.Windows.Interop.HwndSourceHook(WndProc)); int size = Marshal.SizeOf(devBroadcastDeviceInterface); devBroadcastDeviceInterface.dbcc_size = size; devBroadcastDeviceInterface.dbcc_devicetype = Win32Api.DBT_DEVTYP_DEVICEINTERFACE; devBroadcastDeviceInterface.dbcc_reserved = 0; devBroadcastDeviceInterface.dbcc_classguid = DEVICE_INTERFACE_GUID_STDFU; devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true); deviceNotificationHandle = Win32Api.RegisterDeviceNotification(hwndSource.Handle, devBroadcastDeviceInterfaceBuffer, Win32Api.DEVICE_NOTIFY_WINDOW_HANDLE); Marshal.FreeHGlobal(devBroadcastDeviceInterfaceBuffer); }
public override void OnCreateParent(IntPtr hwndParent) { var parameters = new System.Windows.Interop.HwndSourceParameters("CS_Options"); parameters.PositionX = 0; parameters.PositionY = 0; parameters.Height = (int)m_page.Height; parameters.Width = (int)m_page.Width; parameters.ParentWindow = hwndParent; const int WS_CHILD = 0x40000000; const int WS_VISIBLE = 0x10000000; parameters.WindowStyle = WS_VISIBLE | WS_CHILD; m_source = new System.Windows.Interop.HwndSource(parameters); m_source.SizeToContent = System.Windows.SizeToContent.WidthAndHeight; m_source.AddHook(ChildHwndSourceHook); if (m_source.CompositionTarget != null) { m_source.CompositionTarget.BackgroundColor = System.Windows.SystemColors.WindowBrush.Color; } m_source.RootVisual = m_page; }
/// <summary> /// 安装热键处理挂钩 /// </summary> /// <param name="window">The window.</param> /// <returns> /// <c>true</c>:安装成功<br/> /// <c>false</c>:安装失败 /// </returns> /// <value>消息源</value> /// <remarks></remarks> public static bool InstallHotKeyHook(Window window) { //判断是否已经安装了挂钩 if (isInstalled) { return(true); } //判断组件是否有效 if (null == window) { //如果无效,则直接返回 return(false); } //获得窗体的句柄 System.Windows.Interop.WindowInteropHelper helper = new System.Windows.Interop.WindowInteropHelper(window); //判断窗体句柄是否有效 if (IntPtr.Zero == helper.Handle) { //如果句柄无效,则直接返回 return(false); } //获得消息源 System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(helper.Handle); //判断消息源是否有效 if (null == source) { //如果消息源无效,则直接返回 return(false); } //挂接事件 source.AddHook(HotKeyHook); //写入局部变量 main = window; //返回安装成功 isInstalled = true; return(true); }
void MainWindow_NoResizeWindowOnDoubleClick(object sender, EventArgs e) { System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle); source.AddHook(new System.Windows.Interop.HwndSourceHook(WndProc)); }
public void Window_SourceInitialized(object sender, EventArgs e) { System.Windows.Interop.WindowInteropHelper helper = new System.Windows.Interop.WindowInteropHelper(window); System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(helper.Handle); source.AddHook(WndProc); }
private bool InitWindow() { if (hwnd?.Handle != null && hwnd?.Handle != IntPtr.Zero) { return(true); } else { hwnd = new System.Windows.Interop.WindowInteropHelper(this); if (hwnd.Handle != null && hwnd.Handle != IntPtr.Zero) { nativeSource = System.Windows.Interop.HwndSource.FromHwnd(hwnd.Handle); nativeSource.AddHook(OnWindowMessage); var dpi = Screen.GetDpiForWindow(hwnd); var settings = PeerCastStation.Core.PeerCastApplication.Current.Settings.Get <WPFSettings>(); var rect = new Rect( IsFinite(settings.WindowLeft) ? settings.WindowLeft : (this.Left * dpi / 96.0), IsFinite(settings.WindowTop) ? settings.WindowTop : (this.Top * dpi / 96.0), IsFinite(settings.WindowWidth) ? settings.WindowWidth : (this.Width * dpi / 96.0), IsFinite(settings.WindowHeight) ? settings.WindowHeight : (this.Height * dpi / 96.0) ); var screens = Screen.GetAllScreen(); if (!screens.Any(s => s.PhysicalWorkingArea.Contains(rect))) { var targetScreen = screens.OrderByDescending(s => { var r = Rect.Intersect(rect, s.PhysicalWorkingArea); return(r.IsEmpty ? 0.0 : r.Width *r.Height); }).First(); var targetArea = targetScreen.PhysicalWorkingArea; if (rect.Width > targetArea.Width) { rect = new Rect(rect.Left, rect.Top, targetArea.Width, rect.Height); } if (rect.Height > targetArea.Height) { rect = new Rect(rect.Left, rect.Top, rect.Width, targetArea.Height); } if (rect.Top < targetArea.Top) { rect = new Rect(rect.Left, targetArea.Top, rect.Width, rect.Height); } if (rect.Top >= targetArea.Bottom) { rect = new Rect(rect.Left, targetArea.Bottom - rect.Height / 2, rect.Width, rect.Height); } if (rect.Left <= targetArea.Left - rect.Right) { rect = new Rect(targetArea.Left - rect.Width / 2, rect.Top, rect.Width, rect.Height); } if (rect.Left >= targetArea.Right) { rect = new Rect(targetArea.Right - rect.Width / 2, rect.Top, rect.Width, rect.Height); } } dpi = Screen.GetDpiForWindow(this); this.Top = rect.Top * 96.0 / dpi; dpi = Screen.GetDpiForWindow(this); this.Left = rect.Left * 96.0 / dpi; dpi = Screen.GetDpiForWindow(this); this.Width = rect.Width * 96.0 / dpi; dpi = Screen.GetDpiForWindow(this); this.Height = rect.Height * 96.0 / dpi; return(true); } else { return(false); } } }
protected override void OnActivated(EventArgs e) { base.OnActivated(e); hwnd = new System.Windows.Interop.WindowInteropHelper(this); if (hwnd.Handle!=null && hwnd.Handle!=IntPtr.Zero) { nativeSource = System.Windows.Interop.HwndSource.FromHwnd(hwnd.Handle); nativeSource.AddHook(OnWindowMessage); } }
public override void OnCreateParent(IntPtr hwndParent) { var parameters = new System.Windows.Interop.HwndSourceParameters("CS_Options"); parameters.PositionX = 0; parameters.PositionY = 0; parameters.Height = (int)m_page.Height; parameters.Width = (int)m_page.Width; parameters.ParentWindow = hwndParent; const int WS_CHILD = 0x40000000; const int WS_VISIBLE = 0x10000000; parameters.WindowStyle = WS_VISIBLE | WS_CHILD; m_source = new System.Windows.Interop.HwndSource(parameters); m_source.SizeToContent = System.Windows.SizeToContent.WidthAndHeight; m_source.AddHook(new System.Windows.Interop.HwndSourceHook(ChildHwndSourceHook)); m_source.CompositionTarget.BackgroundColor = System.Windows.SystemColors.WindowBrush.Color; m_source.RootVisual = m_page; }
private void AddHook() { _HwndSource = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource; if (_HwndSource != null) { _HwndSource.AddHook(TaskDialogHelpers.CloseButtonHook); } }
void Overlay_Loaded(object sender, System.Windows.RoutedEventArgs e) { _overlaySource = (System.Windows.Interop.HwndSource)System.Windows.PresentationSource.FromVisual(_overlay); int exStyle = (int)GetWindowLong(_overlaySource.Handle, GWL_EXSTYLE); exStyle |= WS_EX_TOOLWINDOW; SetWindowLong(_overlaySource.Handle, GWL_EXSTYLE, (IntPtr)exStyle); SetWindowPos(_overlaySource.Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); SetWindowPos(Handle, _overlaySource.Handle, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); var m = _overlaySource.CompositionTarget.TransformFromDevice; _wpfScale.X = m.M11; _wpfScale.Y = m.M22; _overlay.Left = (Left + _chromeTopLeft.Width) * _wpfScale.X; _overlay.Top = (Top + _chromeTopLeft.Height) * _wpfScale.Y; _overlay.Width = ClientSize.Width * _wpfScale.X; _overlay.Height = ClientSize.Height * _wpfScale.Y; _overlaySource.AddHook(Overlay_WndProc); }