代码之家  ›  专栏  ›  技术社区  ›  Eric Schoonover thSoft

转换Keith Hill的PowerShell获取剪贴板并将剪贴板设置为PSM1脚本

  •  11
  • Eric Schoonover thSoft  · 技术社区  · 15 年前

    我想转换KeithHill的C实现get clipboard,并将clipboard设置为.psm1文件形式的纯PowerShell。

    有没有一种方法可以像他在使用剪贴板时在其cmdlet中那样在PowerShell中旋转STA线程?

    The Blog Post
    The Code

    5 回复  |  直到 5 年前
        1
  •  15
  •   Ben McCormack    11 年前

    function Get-ClipBoard {
        Add-Type -AssemblyName System.Windows.Forms
        $tb = New-Object System.Windows.Forms.TextBox
        $tb.Multiline = $true
        $tb.Paste()
        $tb.Text
    }
    
    
    function Set-ClipBoard() {
        Param(
          [Parameter(ValueFromPipeline=$true)]
          [string] $text
        )
        Add-Type -AssemblyName System.Windows.Forms
        $tb = New-Object System.Windows.Forms.TextBox
        $tb.Multiline = $true
        $tb.Text = $text
        $tb.SelectAll()
        $tb.Copy()
    }
    
        2
  •  8
  •   mklement0    5 年前

    cross-edition, cross-platform module

    • Get-Clipboard Set-Clipboard

      • http://pscx.codeplex.com/
      • clip.exe

        • ... | clip.exe < Set-ClipboardText

          • $OutputEncoding = [System.Text.Encoding]::GetEncoding([System.Globalization.CultureInfo]::CurrentCulture.TextInfo.OEMCodePage)
          • $OutputEncoding = New-Object System.Text.UnicodeEncoding $false, $false # UTF-16 encoding *without BOM*
            • "I enjoyed Thomas Hübl's talk about 中文" | clip # should appear as is on the clipboard
        • $OutputEncoding https://github.com/PowerShell/PowerShell/issues/5763

          • (New-Object ...).psobject.BaseObject [...]:new()
          • powershell.exe -mta
          • -sta

    ClipboardText module Get-ClipboardText

    from the PowerShell Gallery sudo

    Install-Module ClipboardText
    

    the repo




    < |

        4
  •  1
  •   Eric Schoonover thSoft    15 年前

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    class OutClipboard {
      [STAThread]
      static void Main() {
        Clipboard.SetText(Console.In.ReadToEnd());
      }
    }
    

        5
  •  1
  •   Community gkalpak    7 年前