private static void RaiseErrorOrActivateFirstInstance() { if (ChoGlobalApplicationSettings.Me.ApplicationBehaviourSettings.ActivateFirstInstance) { if (ApplicationMode == ChoApplicationMode.Console || ApplicationMode == ChoApplicationMode.Windows) { string appProcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; if (!appProcessName.IsNullOrWhiteSpace()) { Process[] RunningProcesses = Process.GetProcessesByName(appProcessName); if (RunningProcesses.Length != 1) { ChoUser32.ShowWindowAsync(RunningProcesses[0].MainWindowHandle, (int)SHOWWINDOW.SW_SHOWMINIMIZED); ChoUser32.ShowWindowAsync(RunningProcesses[0].MainWindowHandle, (int)SHOWWINDOW.SW_RESTORE); } } } Environment.Exit(ChoExitCodes.ActivateFirstInstanceNExit); } else { OnFatalApplicationException(100, "Already another instance of this application running."); } }
public virtual void Subscribe() { if (_hooksHandle != 0) { return; } lock (_padLock) { // install Mouse hook only if it is not installed and must be installed if (_hooksHandle == 0) { _hooksProc = HooksProc; //install hook _hooksHandle = ChoUser32.SetWindowsHookEx( (int)_hookType, _hooksProc, IntPtr.Zero, //Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); //If SetWindowsHookEx fails. if (_hooksHandle == 0) { ChoWin32Exception.CheckNThrowException(); } } } }
public static ChoKeyEventArgs New(KEYBOARDHOOKSTRUCT keyboardHookStruct) { // Is Control being held down? bool control = ((ChoUser32.GetKeyState(ChoUser32.VK_LCONTROL) & 0x80) != 0) || ((ChoUser32.GetKeyState(ChoUser32.VK_RCONTROL) & 0x80) != 0); // Is Shift being held down? bool shift = ((ChoUser32.GetKeyState(ChoUser32.VK_LSHIFT) & 0x80) != 0) || ((ChoUser32.GetKeyState(ChoUser32.VK_RSHIFT) & 0x80) != 0); // Is Alt being held down? bool alt = ((ChoUser32.GetKeyState(ChoUser32.VK_LALT) & 0x80) != 0) || ((ChoUser32.GetKeyState(ChoUser32.VK_RALT) & 0x80) != 0); // Is CapsLock on? bool capslock = (ChoUser32.GetKeyState(ChoUser32.VK_CAPITAL) != 0); ChoKeyEventArgs e = new ChoKeyEventArgs( (Keys)( keyboardHookStruct.VKCode | (control ? (int)Keys.Control : 0) | (shift ? (int)Keys.Shift : 0) | (alt ? (int)Keys.Alt : 0) )); return(e); }
public static void ShowConsoleWindow() { if (ConsoleWindowHandle != IntPtr.Zero) { ChoUser32.ShowWindow(ConsoleWindowHandle, (int)SHOWWINDOW.SW_SHOWNORMAL); //1 = SW_HIDE } }
static ChoWindowsManager() { try { ConsoleWindowHandle = ChoKernel32.GetConsoleWindow(); MainWindowHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; if (MainWindowHandle != IntPtr.Zero) { _origExWindowStyle = ChoUser32.GetWindowLong(MainWindowHandle, (int)GwlIndexEnum.GWL_EXSTYLE); } if (Environment.UserInteractive) { if (ConsoleWindowHandle != IntPtr.Zero) { ApplicationMode = ChoApplicationMode.Console; } else { ApplicationMode = ChoApplicationMode.Windows; } } else { ApplicationMode = ChoApplicationMode.Service; if (HttpContext.Current != null) { ApplicationMode = ChoApplicationMode.Web; } } } catch { } }
/// <summary> /// If the process has a console attached to it, it will be detached and no longer visible. Writing to the System.Console is still possible, but no output will be shown. /// </summary> public static void Hide() { if (HasWindow) { ChoUser32.ShowWindow(MainWindowHandle, (int)SHOWWINDOW.SW_HIDE); //1 = SW_HIDE } }
public static void ShowInTaskbar(bool show) { if (HasWindow) { if (!show) { if (_show) { _show = false; Hide(); ChoUser32.SetWindowLong(MainWindowHandle, (int)GwlIndexEnum.GWL_EXSTYLE, (int)((WindowStyles)_origExWindowStyle | WindowStyles.WS_EX_TOOLWINDOW)); ChoUser32.ShowWindow(MainWindowHandle, (int)SHOWWINDOW.SW_SHOWNA); } } else { if (!_show) { _show = true; Hide(); ChoUser32.SetWindowLong(MainWindowHandle, (int)GwlIndexEnum.GWL_EXSTYLE, (int)_origExWindowStyle); ChoUser32.ShowWindow(MainWindowHandle, (int)SHOWWINDOW.SW_SHOWNA); } } } }
/// <summary> /// Creates a new console instance if the process is not attached to a console already. /// </summary> public static void Show() { if (HasWindow) { ChoUser32.ShowWindow(MainWindowHandle, (int)SHOWWINDOW.SW_SHOWNORMAL); //1 = SW_SHOWNORMA } }
public static void BringWindowToTop() { if (HasWindow) { ChoUser32.SetForegroundWindow(MainWindowHandle); } }
/// <summary> /// Creates a new console instance if the process is not attached to a console already. /// </summary> public static void Show() { if (HasWindow) { ChoUser32.ShowWindow(MainWindowHandle, (int)SHOWWINDOW.SW_SHOWNORMAL); //1 = SW_SHOWNORMA ChoWindowsManager.SetTop(); } }
public static void SetTop(IntPtr?handle = null) { if (handle == null) { handle = MainWindowHandle; } ChoUser32.SetWindowPos(handle.Value, (IntPtr)SpecialWindowHandles.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_SHOWWINDOW); }
public static void SetUACShield(this Button @this, bool showShield = true) { if (!ChoWindowsIdentity.IsAdministrator() && ChoEnvironment.AtLeastVista()) { //Note: make sure the button FlatStyle = FlatStyle.System @this.FlatStyle = FlatStyle.System; // BCM_SETSHIELD = 0x0000160C ChoUser32.SendMessage(@this.Handle, ChoUser32.BCM_SETSHIELD, 0, showShield ? 0xFFFFFFFF : 0); } }
public static void AlwaysOnTop(bool set) { if (HasWindow) { if (set) { ChoUser32.SetWindowPos(MainWindowHandle, (IntPtr)SpecialWindowHandles.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_SHOWWINDOW); } else { ChoUser32.SetWindowPos(MainWindowHandle, (IntPtr)SpecialWindowHandles.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_SHOWWINDOW); } } }
protected virtual int HooksProc(int code, int wParam, IntPtr lParam) { if (HooksCallbackProc(code, wParam, lParam)) { return(-1); } int hooksHandle = _hooksHandle; if (hooksHandle != 0) { return(ChoUser32.CallNextHookEx(hooksHandle, code, wParam, lParam)); } else { return(-1); } }
public static ChoKeyPressEventArgs New(KEYBOARDHOOKSTRUCT keyboardHookStruct) { // Is Control being held down? bool control = ((ChoUser32.GetKeyState(ChoUser32.VK_LCONTROL) & 0x80) != 0) || ((ChoUser32.GetKeyState(ChoUser32.VK_RCONTROL) & 0x80) != 0); // Is Shift being held down? bool shift = ((ChoUser32.GetKeyState(ChoUser32.VK_LSHIFT) & 0x80) != 0) || ((ChoUser32.GetKeyState(ChoUser32.VK_RSHIFT) & 0x80) != 0); // Is Alt being held down? bool alt = ((ChoUser32.GetKeyState(ChoUser32.VK_LALT) & 0x80) != 0) || ((ChoUser32.GetKeyState(ChoUser32.VK_RALT) & 0x80) != 0); // Is CapsLock on? bool capslock = (ChoUser32.GetKeyState(ChoUser32.VK_CAPITAL) != 0); byte[] keyState = new byte[256]; byte[] inBuffer = new byte[2]; ChoUser32.GetKeyboardState(keyState); if (ChoUser32.ToAscii(keyboardHookStruct.VKCode, keyboardHookStruct.ScanCode, keyState, inBuffer, keyboardHookStruct.Flags) == 1) { char key = (char)inBuffer[0]; if ((capslock ^ shift) && Char.IsLetter(key)) { key = Char.ToUpper(key); } return(new ChoKeyPressEventArgs(key)); } return(null); }
public virtual void Unsubscribe() { if (_hooksHandle == 0) { return; } lock (_padLock) { // install Mouse hook only if it is not installed and must be installed if (_hooksHandle != 0) { //install hook int result = ChoUser32.UnhookWindowsHookEx(_hooksHandle); _hooksHandle = 0; //If SetWindowsHookEx fails. if (result == 0) { ChoWin32Exception.CheckNThrowException(); } } } }