Пример #1
0
        /// <summary>
        /// Size or fill-in a RECONVERTSTRING structure that contains the selection (with padding)
        /// </summary>
        public static IntPtr ReconvertString(IntPtr lParam, SnapshotSpan selection)
        {
            SnapshotSpan selectionContext = GetSelectionContext(selection);
            int          sizeofRCS        = Marshal.SizeOf(typeof(NativeMethods.RECONVERTSTRING));

            if (lParam != IntPtr.Zero)
            {
                NativeMethods.RECONVERTSTRING reconvertString = (NativeMethods.RECONVERTSTRING)(Marshal.PtrToStructure(lParam, typeof(NativeMethods.RECONVERTSTRING)));

                if (selection.Length >= ((reconvertString.dwSize - sizeofRCS) / 2))
                {
                    //We didn't get space for all the characters we requested.
                    return(IntPtr.Zero);
                }

                Marshal.WriteInt32((IntPtr)((long)lParam + (long)Marshal.OffsetOf(typeof(NativeMethods.RECONVERTSTRING), "dwStrLen")), selectionContext.Length);
                Marshal.WriteInt32((IntPtr)((long)lParam + (long)Marshal.OffsetOf(typeof(NativeMethods.RECONVERTSTRING), "dwStrOffset")), sizeofRCS);

                Marshal.WriteInt32((IntPtr)((long)lParam + (long)Marshal.OffsetOf(typeof(NativeMethods.RECONVERTSTRING), "dwCompStrLen")), selection.Length);
                Marshal.WriteInt32((IntPtr)((long)lParam + (long)Marshal.OffsetOf(typeof(NativeMethods.RECONVERTSTRING), "dwCompStrOffset")), (selection.Start.Position - selectionContext.Start.Position) * 2);

                Marshal.WriteInt32((IntPtr)((long)lParam + (long)Marshal.OffsetOf(typeof(NativeMethods.RECONVERTSTRING), "dwTargetStrLen")), selection.Length);
                Marshal.WriteInt32((IntPtr)((long)lParam + (long)Marshal.OffsetOf(typeof(NativeMethods.RECONVERTSTRING), "dwTargetStrOffset")), (selection.Start.Position - selectionContext.Start.Position) * 2);

                Marshal.Copy(selection.Snapshot.GetText(selectionContext).ToCharArray(), 0, (IntPtr)((long)lParam + (long)sizeofRCS), selectionContext.Length);
                Marshal.WriteInt16((IntPtr)((long)lParam + (long)(sizeofRCS + (selectionContext.Length * 2))), 0);
            }

            return(new IntPtr(sizeofRCS + ((selectionContext.Length + 1) * 2)));
        }
Пример #2
0
        /// <summary>
        /// Generate a selection from a RECONVERTSTRING block.
        /// </summary>
        public static SnapshotSpan ConfirmReconvertString(IntPtr lParam, SnapshotSpan selection)
        {
            if (lParam != IntPtr.Zero)
            {
                SnapshotSpan selectionContext = GetSelectionContext(selection);

                NativeMethods.RECONVERTSTRING reconvertString = (NativeMethods.RECONVERTSTRING)(Marshal.PtrToStructure(lParam, typeof(NativeMethods.RECONVERTSTRING)));

                return(new SnapshotSpan(selectionContext.Start + (reconvertString.dwCompStrOffset / 2), reconvertString.dwCompStrLen));
            }

            return(new SnapshotSpan(selection.Snapshot, 0, 0));
        }