private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return(CallNextHookEx(_hHook, nCode, wParam, lParam)); } CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); IntPtr hook = _hHook; if (msg.message == (int)CbtHookAction.HCBT_ACTIVATE) { try { CenterWindow(msg.hwnd); } finally { UnhookWindowsHookEx(_hHook); _hHook = IntPtr.Zero; } } return(CallNextHookEx(hook, nCode, wParam, lParam)); }
private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return(CallNextHookEx(hHook, nCode, wParam, lParam)); } CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); IntPtr hook = hHook; if (hookCaption != null && msg.message == WM_INITDIALOG) { int nLength = GetWindowTextLength(msg.hwnd); StringBuilder text = new StringBuilder(nLength + 1); GetWindowText(msg.hwnd, text, text.Capacity); if (hookCaption == text.ToString()) { hookCaption = null; SetTimer(msg.hwnd, (UIntPtr)TimerID, hookTimeout, hookTimer); UnhookWindowsHookEx(hHook); hHook = IntPtr.Zero; } } return(CallNextHookEx(hook, nCode, wParam, lParam)); }
private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return(CallNextHookEx(hHook, nCode, wParam, lParam)); } CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); IntPtr hook = hHook; if (msg.message == WM_INITDIALOG) { int nLength = GetWindowTextLength(msg.hwnd); StringBuilder className = new StringBuilder(10); GetClassName(msg.hwnd, className, className.Capacity); if (className.ToString() == "#32770") { nButton = 0; EnumChildWindows(msg.hwnd, enumProc, IntPtr.Zero); if (nButton == 1) { IntPtr hButton = GetDlgItem(msg.hwnd, MBCancel); if (hButton != IntPtr.Zero) { SetWindowText(hButton, OK); } } } } return(CallNextHookEx(hook, nCode, wParam, lParam)); }
private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return(CallNextHookEx(hHook, nCode, wParam, lParam)); } CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); IntPtr hook = hHook; if (WM_INITDIALOG == msg.message) { StringBuilder className = new StringBuilder(10); GetClassName(msg.hwnd, className, className.Capacity); if (className.ToString() == "#32770") { buttonCount = 0; EnumChildWindows(msg.hwnd, enumProc, IntPtr.Zero); if (1 == buttonCount) { IntPtr hButton = GetDlgItem(msg.hwnd, IDCANCEL); if (IntPtr.Zero != hButton) { SetWindowText(hButton, OK); } } } } return(CallNextHookEx(hook, nCode, wParam, lParam)); }
private static IntPtr MessageBoxHookProc(Int32 nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return(CenteredDialogBox.CallNextHookEx(windowHook, nCode, wParam, lParam)); } CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); IntPtr hook = windowHook; if (msg.Message == (Int32)CbtHookAction.HCBT_ACTIVATE) { try { CenteredDialogBox.CenterWindow(msg.Hwnd); } finally { CenteredDialogBox.UnhookWindowsHookEx(windowHook); windowHook = IntPtr.Zero; } } return(CenteredDialogBox.CallNextHookEx(hook, nCode, wParam, lParam)); }
private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return(CallNextHookEx(_hHook, nCode, wParam, lParam)); } CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); IntPtr hook = _hHook; if (msg.message == (int)CbtHookAction.HCBT_ACTIVATE) { try { if (_ico != null) { SendMessage(msg.hwnd, WM_SETICON, ICON_SMALL, _ico.Handle); } CenterWindow(msg.hwnd); SubClass(msg.hwnd); } catch { /* If it bombs, we just quietly let it use the plain default MessageBox */ } finally { UnhookWindowsHookEx(_hHook); } } return(CallNextHookEx(hook, nCode, wParam, lParam)); }
private static Int32 WH_CALLWNDPROCRET_PROC(Int32 iCode, IntPtr pWParam, IntPtr pLParam) { if (iCode < 0) { return(CallNextHookEx(ClosedPopupMsg._pWH_CALLWNDPROCRET, iCode, pWParam, pLParam)); } CWPRETSTRUCT cwp = (CWPRETSTRUCT)Marshal.PtrToStructure(pLParam, typeof(CWPRETSTRUCT)); if (cwp.message == WM_INITDIALOG) { Int32 iLength = GetWindowTextLength(cwp.hwnd); foreach (IntPtr pChildOfDialog in ClosedPopupMsg.listChildWindows(cwp.hwnd)) { iLength = GetWindowTextLength(pChildOfDialog); if (iLength > 0) { Int32 ctrlId = GetDlgCtrlID(pChildOfDialog); SendMessage(cwp.hwnd, WM_COMMAND, new IntPtr(ctrlId), pChildOfDialog); } } } return(CallNextHookEx(ClosedPopupMsg._pWH_CALLWNDPROCRET, iCode, pWParam, pLParam)); }
private static IntPtr ActiveHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return(CallNextHookEx(_hActiveHook, nCode, wParam, lParam)); } CWPRETSTRUCT m = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); switch (m.message) { case WM_SETFOCUS: _focused = true; DebugWrite("WM_SETFOCUS(0x{0:X8}) Activitated={1} Focused={2}", m.wParam, _active, _focused); break; case WM_KILLFOCUS: _focused = false; DebugWrite("WM_KILLFOCUS(0x{0:X8}) Activitated={1} Focused={2}", m.wParam, _active, _focused); break; case WM_ACTIVATEAPP: case WM_ACTIVATE: _active = ((m.wParam.ToInt32() & 0xFFFF) != 0); DebugWrite("WM_ACTIVATE Activitated={0} Focused={1}", _active, _focused); break; } return(CallNextHookEx(_hActiveHook, nCode, wParam, lParam)); }
internal CallWndProcRetHookEventArgs(IntPtr wparam, IntPtr lparam) { sentFromCurrentProcess = wparam != IntPtr.Zero; CWPRETSTRUCT cwpr = (CWPRETSTRUCT)Marshal.PtrToStructure(lparam, typeof(CWPRETSTRUCT)); message = Message.Create(cwpr.hwnd, cwpr.message, cwpr.wparam, cwpr.lparam); message.Result = cwpr.result; }
private IntPtr MessageHook(int code, IntPtr wParam, IntPtr lParam) { CWPRETSTRUCT m = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); WindowMessage msg = (WindowMessage)m.message; switch (msg) { case WindowMessage.Create: ApplyStyles(m.hWnd); break; } return(UnsafeNativeMethods.CallNextHookEx(Hook, code, wParam, lParam)); }
private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return(CallNextHookEx(hHook, nCode, wParam, lParam)); } CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); IntPtr hook = hHook; if (msg.message == WM_INITDIALOG) { int nLength = GetWindowTextLength(msg.hwnd); StringBuilder className = new StringBuilder(10); StringBuilder wndText = new StringBuilder(100); GetClassName(msg.hwnd, className, className.Capacity); GetWindowText(msg.hwnd, wndText, wndText.Capacity); if (className.ToString() == "#32770" && (wndText.ToString() == Application.ProductName || wndText.ToString() == Application.ProductName + " " || wndText.ToString() == Application.ProductName + " ")) // one or two space for Babu.Windows.Forms.MessageBox { nButton = 0; EnumChildWindows(msg.hwnd, enumProc, IntPtr.Zero); if (nButton == 1) { IntPtr hButton = GetDlgItem(msg.hwnd, MBCancel); if (hButton != IntPtr.Zero) { SetWindowText(hButton, OK); } } // adjust yes button size and position if (YesXDelta + NoXDelta != 0 || YesXDelta != 0) { AdjustDlgItemXPosition(msg.hwnd, MBYes, YesXDelta + NoXDelta, YesXDelta); } if (NoXDelta != 0) { AdjustDlgItemXPosition(msg.hwnd, MBNo, NoXDelta, NoXDelta); } Reset(); // reset button text after using, fix bug #7726 } } return(CallNextHookEx(hook, nCode, wParam, lParam)); }
private static void ApplyTheme(CWPRETSTRUCT msg) { try { if (msg.GetMessage() != WindowMessage.Create) { return; } if (!(Control.FromHandle(msg.hWnd) is Form form)) { return; } Theme.ApplyTheme(form); } catch { } }
private static IntPtr WaitMessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return(CallNextHookEx(_hWaitHook, nCode, wParam, lParam)); } CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); IntPtr hook = _hWaitHook; if (msg.message == WM_INITDIALOG) { if (GetWindowText(msg.hwnd).StartsWith(_messageboxTitle)) { //System.Diagnostics.Debug.WriteLine("[Pushing MessageBox Dialog Button]\n"); SendDlgItemMessage(msg.hwnd, _buttonId, BM_CLICK, IntPtr.Zero, IntPtr.Zero); UnregisterWaitToPush(); } } return(CallNextHookEx(hook, nCode, wParam, lParam)); }
private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return(CallNextHookEx(MessageBoxEx.hHook, nCode, wParam, lParam)); } CWPRETSTRUCT cwpretstruct = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); IntPtr hHook = MessageBoxEx.hHook; if ((hookCaption != null) && (cwpretstruct.message == 0x110)) { StringBuilder text = new StringBuilder(GetWindowTextLength(cwpretstruct.hwnd) + 1); GetWindowText(cwpretstruct.hwnd, text, text.Capacity); if (hookCaption == text.ToString()) { hookCaption = null; SetTimer(cwpretstruct.hwnd, (UIntPtr)0x2a, hookTimeout, hookTimer); UnhookWindowsHookEx(MessageBoxEx.hHook); MessageBoxEx.hHook = IntPtr.Zero; } } return(CallNextHookEx(hHook, nCode, wParam, lParam)); }
// Hook proceedure called by the OS when a message has been processed by the target Window private static Int32 WH_CALLWNDPROCRET_PROC(Int32 iCode, IntPtr pWParam, IntPtr pLParam) { if (iCode < 0) { return(CallNextHookEx(WindowsInterop._pWH_CALLWNDPROCRET, iCode, pWParam, pLParam)); } CWPRETSTRUCT cwp = (CWPRETSTRUCT) Marshal.PtrToStructure(pLParam, typeof(CWPRETSTRUCT)); if (cwp.message == WM_INITDIALOG) { // A dialog was initialised, find out what sort it was via it's Caption text Int32 iLength = GetWindowTextLength(cwp.hwnd); StringBuilder sb = new StringBuilder(iLength + 1); GetWindowText(cwp.hwnd, sb, sb.Capacity); if (StringConstants.DialogCaptionSecurityAlert.Equals(sb.ToString(), StringComparison.InvariantCultureIgnoreCase)) { // A "Security Alert" dialog was initialised, now need // // a) To know what type it is - e.g. is it an SSL related one or a switching b/w // secure/non-secure modes etc one - and, // // b) A handle to the 'Yes' button so a user-click can be simulated on it // Boolean blnIsSslDialog = true; // assumed true for now IntPtr pYesButtonHwnd = IntPtr.Zero; // Check out further properties of the dialog foreach (IntPtr pChildOfDialog in WindowsInterop.listChildWindows(cwp.hwnd)) { // Go through all of the child controls on the dialog and see what they reveal via their text iLength = GetWindowTextLength(pChildOfDialog); if (iLength > 0) { StringBuilder sbProbe = new StringBuilder(iLength + 1); GetWindowText(pChildOfDialog, sbProbe, sbProbe.Capacity); if (StringConstants.DialogTextSecureToNonSecureWarning.Equals(sbProbe.ToString(), StringComparison.InvariantCultureIgnoreCase) || StringConstants.DialogTextNonSecureToSecureWarning.Equals(sbProbe.ToString(), StringComparison.InvariantCultureIgnoreCase)) { // Ok, the text says something about toggling secure/non-secure or vice-versa so, blnIsSslDialog = false; } if (StringConstants.ButtonTextYes.Equals(sbProbe.ToString(), StringComparison.InvariantCultureIgnoreCase)) { // Hey, this one says 'Yes', so cache a pointer to it pYesButtonHwnd = pChildOfDialog; // NB: Could also confim it is a button we're pointing at, at this point, and // not a label or something by using the Win32 API's GetClassName() function (see below for e.g.), // but in this case it is already known that a button is the only thing saying 'Yes' // on this particular dialog } } } // Ok, some sort of "Security Alert" dialog was initialised, fire a message // to anyone who's listening i.e. In this case, ask the event receiver if // they want to ignore it if (SecurityAlertDialogWillBeShown != null) { if (SecurityAlertDialogWillBeShown(blnIsSslDialog) && pYesButtonHwnd != IntPtr.Zero) { // Alert the dialog's message-pump that the 'Yes' button was 'Pressed' Int32 ctrlId = GetDlgCtrlID(pYesButtonHwnd); SendMessage(cwp.hwnd, WM_COMMAND, new IntPtr(ctrlId), pYesButtonHwnd); // Block any further processing of the WM_INITDIALOG message by anyone else // This is important: by doing this, the dialog never shows up on the screen return(1); } } } else if (sb.ToString().StartsWith(StringConstants.DialogCaptionConnectTo)) { // 'Connect to ...' style dialog shown IntPtr pComboHwnd = IntPtr.Zero; IntPtr pEditHwnd = IntPtr.Zero; IntPtr pOkButtonHwnd = IntPtr.Zero; foreach (IntPtr pChildOfDialog in WindowsInterop.listChildWindows(cwp.hwnd)) { // Go through all of the child controls on the dialog and see what they reveal via their type/text StringBuilder sbProbe = new StringBuilder(255); if (GetClassName(pChildOfDialog, sbProbe, sbProbe.Capacity) != 0 && !String.IsNullOrEmpty(sbProbe.ToString())) { if (sbProbe.ToString().Equals("SysCredential")) { // This control is actually a set of controls called a "SysCredential" // (as determined via Spy++), so cache it's child controls that are of // type "ComboBoxEx32" and "Edit" foreach (IntPtr pChildOfSysCredential in WindowsInterop.listChildWindows(pChildOfDialog)) { StringBuilder sbProbe2 = new StringBuilder(255); if (GetClassName(pChildOfSysCredential, sbProbe2, sbProbe2.Capacity) != 0 && !String.IsNullOrEmpty(sbProbe2.ToString())) { if (StringConstants.WindowTypeCombo.Equals(sbProbe2.ToString(), StringComparison.InvariantCultureIgnoreCase)) { // Hey, here's the Combo pComboHwnd = pChildOfSysCredential; } if (StringConstants.WindowTypeEdit.Equals(sbProbe2.ToString(), StringComparison.InvariantCultureIgnoreCase)) { // Hey, here's *an* Edit box. // // This will happen several times as there is an Edit box at the bottom of // the "ComboBoxEx" heirarchy as well, but luckily the last one encountered // is actually the required one pEditHwnd = pChildOfSysCredential; } } } } if (StringConstants.WindowTypeButton.Equals(sbProbe.ToString(), StringComparison.InvariantCultureIgnoreCase)) { // This control is a Button, does it have text, if so what does it say iLength = GetWindowTextLength(pChildOfDialog); if (iLength > 0) { StringBuilder sbText = new StringBuilder(iLength + 1); GetWindowText(pChildOfDialog, sbText, sbText.Capacity); if (StringConstants.ButtonTextOk.Equals(sbText.ToString(), StringComparison.InvariantCultureIgnoreCase)) { // Hey, this one says 'OK', so cache a pointer to it pOkButtonHwnd = pChildOfDialog; } } } } } // Ok, a "Connect to" dialog was initialiased, fire a message to anyone who's interested // i.e. Ask the event receiver if they want to automate the dialog by providing // a username and password with which to populate the dialog if (ConnectToDialogWillBeShown != null) { String sUsername = null; String sPassword = null; if (ConnectToDialogWillBeShown(ref sUsername, ref sPassword) && sUsername != null && sPassword != null && pOkButtonHwnd != IntPtr.Zero && pComboHwnd != IntPtr.Zero && pEditHwnd != IntPtr.Zero) { // Yep, they do // Put the username and password into the boxes SetWindowText(pComboHwnd, sUsername); SetWindowText(pEditHwnd, sPassword); // Alert the dialog's message-pump that the 'OK' button was 'Pressed' Int32 ctrlId = GetDlgCtrlID(pOkButtonHwnd); SendMessage(cwp.hwnd, WM_COMMAND, new IntPtr(ctrlId), pOkButtonHwnd); // Block further processing of the WM_INITDIALOG message by anyone else // This is important: by doing this, the dialog never shows up on the screen return(1); } } } } // Call the next hook in the chain return(CallNextHookEx(WindowsInterop._pWH_CALLWNDPROCRET, iCode, pWParam, pLParam)); }
private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return(SafeNativeMethods.CallNextHookEx(_hHook, nCode, wParam, lParam)); } CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); IntPtr hook = _hHook; switch (msg.message) { case (int)Win32HookAction.HCBT_ACTIVATE: try { if (_owner != null) { Rectangle cRect = new Rectangle(0, 0, 0, 0); if (SafeNativeMethods.GetWindowRect(msg.hwnd, ref cRect)) { int width = cRect.Width - cRect.X; int height = cRect.Height - cRect.Y; Rectangle pRect = new Rectangle(0, 0, 0, 0); if (SafeNativeMethods.GetWindowRect(_owner.Handle, ref pRect)) { Point ptCenter = new Point(pRect.X, pRect.Y); ptCenter.X += (pRect.Width - pRect.X) / 2; ptCenter.Y += ((pRect.Height - pRect.Y) / 2) - 10; Point ptStart = new Point(ptCenter.X, ptCenter.Y); ptStart.X -= width / 2; if (ptStart.X < 0) { ptStart.X = 0; } ptStart.Y -= height / 2; if (ptStart.Y < 0) { ptStart.Y = 0; } SafeNativeMethods.MoveWindow(msg.hwnd, ptStart.X, ptStart.Y, width, height, false); if (MoveCursorToMsgBoxAtOwner) { SetCursorPos(msg.hwnd, new Point(width / 2, (height / 2) + 24)); } } } } } catch (Exception ex) { Log.Debug(ex); } if (!ButtonText.OverrideEnabled) { return(MessageBoxUnhookProc()); } break; case (int)Win32HookAction.WM_INITDIALOG: if (ButtonText.OverrideEnabled) { try { int nLength = SafeNativeMethods.GetWindowTextLength(msg.hwnd); StringBuilder className = new StringBuilder(10); SafeNativeMethods.GetClassName(msg.hwnd, className, className.Capacity); if (className.ToString() == "#32770") { nButton = 0; SafeNativeMethods.EnumChildWindows(msg.hwnd, _enumProc, IntPtr.Zero); if (nButton == 1) { IntPtr hButton = SafeNativeMethods.GetDlgItem(msg.hwnd, 2); if (hButton != IntPtr.Zero) { SafeNativeMethods.SetWindowText(hButton, ButtonText.OK); } } } } catch (Exception ex) { Log.Debug(ex); } } return(MessageBoxUnhookProc()); } return(SafeNativeMethods.CallNextHookEx(hook, nCode, wParam, lParam)); }