private void RetrieveCursorMetadata(DesktopFrame frame) { var pointerInfo = new PointerInfo(); // A non-zero mouse update timestamp indicates that there is a mouse position update and optionally a shape change //if (frameInfo.LastMouseUpdateTime == 0) // return; var updatePosition = _frameInfo.PointerPosition.Visible || (pointerInfo.WhoUpdatedPositionLast == _whichOutputDevice); // If two outputs both say they have a visible, only update if new update has newer timestamp if (_frameInfo.PointerPosition.Visible && pointerInfo.Visible && (pointerInfo.WhoUpdatedPositionLast != _whichOutputDevice) && (pointerInfo.LastTimeStamp > _frameInfo.LastMouseUpdateTime)) { updatePosition = false; } // Update position if (updatePosition) { pointerInfo.Position = new SharpDX.Point(_frameInfo.PointerPosition.Position.X, _frameInfo.PointerPosition.Position.Y); pointerInfo.WhoUpdatedPositionLast = _whichOutputDevice; pointerInfo.LastTimeStamp = _frameInfo.LastMouseUpdateTime; pointerInfo.Visible = _frameInfo.PointerPosition.Visible; } // No new shape if (_frameInfo.PointerShapeBufferSize != 0) { if (_frameInfo.PointerShapeBufferSize > pointerInfo.BufferSize) { pointerInfo.PtrShapeBuffer = new byte[_frameInfo.PointerShapeBufferSize]; pointerInfo.BufferSize = _frameInfo.PointerShapeBufferSize; } try { unsafe { fixed(byte *ptrShapeBufferPtr = pointerInfo.PtrShapeBuffer) { _deskDupl.GetFramePointerShape(_frameInfo.PointerShapeBufferSize, (IntPtr)ptrShapeBufferPtr, out pointerInfo.BufferSize, out pointerInfo.ShapeInfo); var bitmap = new Bitmap(pointerInfo.ShapeInfo.Width, pointerInfo.ShapeInfo.Height, PixelFormat.Format32bppArgb); var boundsRect = new System.Drawing.Rectangle(0, 0, pointerInfo.ShapeInfo.Width, pointerInfo.ShapeInfo.Height); var mapDest = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat); var sourcePtr = (IntPtr)ptrShapeBufferPtr; var destPtr = mapDest.Scan0; for (var y = 0; y < pointerInfo.ShapeInfo.Height; y++) { // Copy a single line Utilities.CopyMemory(destPtr, sourcePtr, pointerInfo.ShapeInfo.Width * 4); // Advance pointers sourcePtr = IntPtr.Add(sourcePtr, pointerInfo.ShapeInfo.Pitch); destPtr = IntPtr.Add(destPtr, mapDest.Stride); } // Release bitmap lock bitmap.UnlockBits(mapDest); frame.CursorBitmap = bitmap; } } } catch (SharpDXException ex) { if (ex.ResultCode.Failure) { throw new DesktopDuplicationException("Failed to get frame pointer shape."); } } } //frame.CursorVisible = pointerInfo.Visible; frame.CursorLocation = new System.Drawing.Point(pointerInfo.Position.X, pointerInfo.Position.Y); }
private void RetrieveCursorMetadata(DesktopFrame frame) { var pointerInfo = new PointerInfo(); // A non-zero mouse update timestamp indicates that there is a mouse position update and optionally a shape change if (frameInfo.LastMouseUpdateTime == 0) return; bool updatePosition = true; // Make sure we don't update pointer position wrongly // If pointer is invisible, make sure we did not get an update from another output that the last time that said pointer // was visible, if so, don't set it to invisible or update. if (!frameInfo.PointerPosition.Visible && (pointerInfo.WhoUpdatedPositionLast != this.mWhichOutputDevice)) updatePosition = false; // If two outputs both say they have a visible, only update if new update has newer timestamp if (frameInfo.PointerPosition.Visible && pointerInfo.Visible && (pointerInfo.WhoUpdatedPositionLast != this.mWhichOutputDevice) && (pointerInfo.LastTimeStamp > frameInfo.LastMouseUpdateTime)) updatePosition = false; // Update position if (updatePosition) { pointerInfo.Position = new SharpDX.Point(frameInfo.PointerPosition.Position.X, frameInfo.PointerPosition.Position.Y); pointerInfo.WhoUpdatedPositionLast = mWhichOutputDevice; pointerInfo.LastTimeStamp = frameInfo.LastMouseUpdateTime; pointerInfo.Visible = frameInfo.PointerPosition.Visible; } // No new shape if (frameInfo.PointerShapeBufferSize == 0) return; if (frameInfo.PointerShapeBufferSize > pointerInfo.BufferSize) { pointerInfo.PtrShapeBuffer = new byte[frameInfo.PointerShapeBufferSize]; pointerInfo.BufferSize = frameInfo.PointerShapeBufferSize; } try { unsafe { fixed (byte* ptrShapeBufferPtr = pointerInfo.PtrShapeBuffer) { mDeskDupl.GetFramePointerShape(frameInfo.PointerShapeBufferSize, (IntPtr)ptrShapeBufferPtr, out pointerInfo.BufferSize, out pointerInfo.ShapeInfo); } } } catch (SharpDXException ex) { if (ex.ResultCode.Failure) { throw new DesktopDuplicationException("Failed to get frame pointer shape."); } } frame.CursorVisible = pointerInfo.Visible; frame.CursorLocation = new System.Drawing.Point(pointerInfo.Position.X, pointerInfo.Position.Y); //Console.WriteLine(frame.CursorLocation.X+":"+frame.CursorLocation.Y); }
private void RetrieveCursorMetadata(DesktopFrame frame) { var pointerInfo = new PointerInfo(); // A non-zero mouse update timestamp indicates that there is a mouse position update and optionally a shape change if (frameInfo.LastMouseUpdateTime == 0) { return; } bool updatePosition = true; // Make sure we don't update pointer position wrongly // If pointer is invisible, make sure we did not get an update from another output that the last time that said pointer // was visible, if so, don't set it to invisible or update. if (!frameInfo.PointerPosition.Visible && (pointerInfo.WhoUpdatedPositionLast != this.mWhichOutputDevice)) { updatePosition = false; } // If two outputs both say they have a visible, only update if new update has newer timestamp if (frameInfo.PointerPosition.Visible && pointerInfo.Visible && (pointerInfo.WhoUpdatedPositionLast != this.mWhichOutputDevice) && (pointerInfo.LastTimeStamp > frameInfo.LastMouseUpdateTime)) { updatePosition = false; } // Update position if (updatePosition) { pointerInfo.Position = new SharpDX.Point(frameInfo.PointerPosition.Position.X, frameInfo.PointerPosition.Position.Y); pointerInfo.WhoUpdatedPositionLast = mWhichOutputDevice; pointerInfo.LastTimeStamp = frameInfo.LastMouseUpdateTime; pointerInfo.Visible = frameInfo.PointerPosition.Visible; } // No new shape if (frameInfo.PointerShapeBufferSize == 0) { return; } if (frameInfo.PointerShapeBufferSize > pointerInfo.BufferSize) { pointerInfo.PtrShapeBuffer = new byte[frameInfo.PointerShapeBufferSize]; pointerInfo.BufferSize = frameInfo.PointerShapeBufferSize; } try { unsafe { fixed(byte *ptrShapeBufferPtr = pointerInfo.PtrShapeBuffer) { mDeskDupl.GetFramePointerShape(frameInfo.PointerShapeBufferSize, (IntPtr)ptrShapeBufferPtr, out pointerInfo.BufferSize, out pointerInfo.ShapeInfo); } } } catch (SharpDXException ex) { if (ex.ResultCode.Failure) { throw new DesktopDuplicationException("Failed to get frame pointer shape."); } } frame.CursorVisible = pointerInfo.Visible; frame.CursorLocation = new System.Drawing.Point(pointerInfo.Position.X, pointerInfo.Position.Y); //Console.WriteLine(frame.CursorLocation.X+":"+frame.CursorLocation.Y); }