Exemplo n.º 1
0
 public void Scroll(int dx, int dy)
 {
     if (User32Native.ScrollWindowEx(Handle, dx, dy, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, SW_INVALIDATE) == -1)
     {
         throw Win32Exc.FromLastError();
     }
 }
Exemplo n.º 2
0
 public void Move(int x, int y, int w, int h)
 {
     if (!User32Native.MoveWindow(Handle, x, y, w, h, true))
     {
         throw Win32Exc.FromLastError();
     }
 }
Exemplo n.º 3
0
        public int GetProcessId(out int threadId)
        {
            threadId = User32Native.GetWindowThreadProcessId(Handle, out var processId);
            if (threadId == 0)
            {
                throw Win32Exc.FromLastError();
            }

            return(processId);
        }
Exemplo n.º 4
0
        public RECT GetRect()
        {
            var rect = default(RECT);

            if (!User32Native.GetWindowRect(Handle, ref rect))
            {
                throw Win32Exc.FromLastError();
            }

            return(rect);
        }
Exemplo n.º 5
0
        public void Disable()
        {
            if (!Enabled)
            {
                throw new InvalidOperationException();
            }

            if (!User32Native.UnregisterHotKey(Form.Handle, GlobalAtomId))
            {
                throw Win32Exc.FromLastError();
            }
        }
Exemplo n.º 6
0
        public static TimeSpan GetIdleTime()
        {
            LASTINPUTINFO lastInPut = new LASTINPUTINFO();

            lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
            User32Native.GetLastInputInfo(ref lastInPut);

            var      idleTicks = ((uint)Environment.TickCount - lastInPut.dwTime);
            TimeSpan timespent = TimeSpan.FromMilliseconds(idleTicks);

            return(timespent);
        }
Exemplo n.º 7
0
        public string GetText()
        {
            int length = User32Native.GetWindowTextLength(Handle);

            if (length == 0)
            {
                return(string.Empty);
            }

            var text = new StringBuilder(length * 2);

            User32Native.GetWindowText(Handle, text, text.Capacity);

            return(text.ToString());
        }
Exemplo n.º 8
0
        public static string GetWindowText(HWND window)
        {
            var length = User32Native.GetWindowTextLength(window) + 1;
            var sb     = new StringBuilder(length);

            User32Native.GetWindowText(window, sb, length);
            try
            {
                return(sb.ToString());
            }
            finally
            {
                sb.Clear();
            }
        }
Exemplo n.º 9
0
        public void Enable(Keys hotkey, Func <bool> callback)
        {
            callback.AssertNotNull(nameof(callback));

            if (Enabled)
            {
                throw new InvalidOperationException();
            }

            var modifiers = GetFsModifiers(hotkey, out var keysModifiers);
            var keys      = (int)hotkey & ~(int)keysModifiers;

            if (!User32Native.RegisterHotKey(Form.Handle, GlobalAtomId, (uint)modifiers, (uint)keys))
            {
                throw Win32Exc.FromLastError();
            }

            Callback = callback;
            Enabled  = true;
        }
Exemplo n.º 10
0
        public void Run()
        {
            var msg = new MSG();

            while (true)
            {
                if (!GetMessage(ref msg, IntPtr.Zero, 0, 0))
                {
                    break;
                }

                if (msg.message == WM_QUIT)
                {
                    break;
                }

                User32Native.TranslateMessage(ref msg);
                User32Native.DispatchMessage(ref msg);
            }
        }
Exemplo n.º 11
0
        public static void SimulateInput()
        {
            if (moved)
            {
                mouseX = -mouseX;
                mouseY = -mouseY;
            }

            var inputsList = new List <INPUT>();

            inputsList.Add(MoveMouse(mouseX, mouseY));
            inputsList.Add(AddKeyDown(VirtualKeyCode.F19));
            inputsList.Add(AddKeyUp(VirtualKeyCode.F19));

            var inputs = inputsList.ToArray();

            var successful = User32Native.SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));

            moved = !moved;
        }
Exemplo n.º 12
0
        public static INPUT AddKeyDown(VirtualKeyCode keyCode)
        {
            var down =
                new INPUT
            {
                Type = (UInt32)InputType.Keyboard,
                Data =
                {
                    Keyboard      =
                        new KEYBDINPUT
                    {
                        KeyCode   = (UInt16)keyCode,
                        Scan      = (UInt16)(User32Native.MapVirtualKey((UInt32)keyCode,           0) & 0xFFU),
                        Flags     = IsExtendedKey(keyCode) ? (UInt32)KeyboardFlag.ExtendedKey : 0,
                        Time      =                                                             0,
                        ExtraInfo = IntPtr.Zero
                    }
                }
            };

            return(down);
        }
Exemplo n.º 13
0
        public static INPUT AddKeyUp(VirtualKeyCode keyCode)
        {
            var up =
                new INPUT
            {
                Type = (UInt32)InputType.Keyboard,
                Data =
                {
                    Keyboard      =
                        new KEYBDINPUT
                    {
                        KeyCode = (UInt16)keyCode,
                        Scan    = (UInt16)(User32Native.MapVirtualKey((UInt32)keyCode, 0) & 0xFFU),
                        Flags   = (UInt32)(IsExtendedKey(keyCode)
                                    ? KeyboardFlag.KeyUp | KeyboardFlag.ExtendedKey
                                    : KeyboardFlag.KeyUp),
                        Time      =                                                   0,
                        ExtraInfo = IntPtr.Zero
                    }
                }
            };

            return(up);
        }
Exemplo n.º 14
0
        protected IntPtr FilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (DataSource.SourceId.Id == 0)
            {
                handled = false;
                return(IntPtr.Zero);
            }

            int pos = User32Native.GetMessagePos();

            WindowsMessage message = new WindowsMessage();

            message.hwnd    = hwnd;
            message.message = msg;
            message.wParam  = wParam;
            message.lParam  = lParam;
            message.time    = User32Native.GetMessageTime();
            message.x       = (short)pos;
            message.y       = (short)(pos >> 16);

            Marshal.StructureToPtr(message, _eventMessage.EventPtr, false);
            _eventMessage.Message = 0;

            TwainResult result = Twain32Native.DsEvent(
                ApplicationId,
                DataSource.SourceId,
                DataGroup.Control,
                DataArgumentType.Event,
                Message.ProcessEvent,
                ref _eventMessage);

            if (result == TwainResult.NotDSEvent)
            {
                handled = false;
                return(IntPtr.Zero);
            }

            switch (_eventMessage.Message)
            {
            case Message.XFerReady:
                IList <IntPtr> imagePointers = TransferPictures();

                foreach (IntPtr image in imagePointers)
                {
                    using (var renderer = new BitmapRenderer(image))
                    {
                        Images.Add(renderer.RenderToBitmap());
                    }
                }

                EndingScan();
                DataSource.Close();

                ScanningComplete(this, EventArgs.Empty);
                break;

            case Message.CloseDS:
                EndingScan();
                DataSource.Close();
                break;

            case Message.CloseDSOK:
                EndingScan();
                DataSource.Close();
                break;

            case Message.DeviceEvent:
                break;
            }

            handled = true;
            return(IntPtr.Zero);
        }
Exemplo n.º 15
0
        protected IntPtr FilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (DataSource.SourceId.Id == 0)
            {
                handled = false;
                return(IntPtr.Zero);
            }

            int pos = User32Native.GetMessagePos();

            WindowsMessage message = new WindowsMessage();

            message.hwnd    = hwnd;
            message.message = msg;
            message.wParam  = wParam;
            message.lParam  = lParam;
            message.time    = User32Native.GetMessageTime();
            message.x       = (short)pos;
            message.y       = (short)(pos >> 16);

            Marshal.StructureToPtr(message, _eventMessage.EventPtr, false);
            _eventMessage.Message = 0;

            TwainResult result = Twain32Native.DsEvent(
                ApplicationId,
                DataSource.SourceId,
                DataGroup.Control,
                DataArgumentType.Event,
                Message.ProcessEvent,
                ref _eventMessage);

            if (result == TwainResult.NotDSEvent)
            {
                handled = false;
                return(IntPtr.Zero);
            }

            switch (_eventMessage.Message)
            {
            case Message.XFerReady:
                Exception exception = null;
                try
                {
                    TransferPictures();
                }
                catch (Exception e)
                {
                    exception = e;
                }
                CloseDsAndCompleteScanning(exception);
                break;

            case Message.CloseDS:
            case Message.CloseDSOK:
            case Message.CloseDSReq:
                CloseDsAndCompleteScanning(null);
                break;

            case Message.DeviceEvent:
                break;

            default:
                break;
            }

            handled = true;
            return(IntPtr.Zero);
        }
Exemplo n.º 16
0
 public bool PostMessage(uint message, IntPtr wParam, IntPtr lParam)
 {
     return(User32Native.PostMessage(Handle, message, wParam, lParam) != 0);
 }
Exemplo n.º 17
0
 private bool GetMessage(ref MSG msg, IntPtr hwnd, int minMessage, int maxMessage)
 {
     return(User32Native.GetMessage(ref msg, hwnd.ToInt32(), minMessage, maxMessage));
 }
Exemplo n.º 18
0
 public IntPtr SetRedraw(bool redraw)
 {
     return(User32Native.SendMessage(Handle, WM_SETREDRAW, redraw ? (IntPtr)1 : (IntPtr)0, (IntPtr)0));
 }