delphi读写剪贴板的一些参考

设置剪贴板数据:

先用GlobalAlloc在堆中分配空间,返回的Hmem句柄将作为SetClipboardData的第二个参数。
然后用GlobalLock把Hmem转为指针,再用delphi的strCopy把字符串写入。

取剪贴板数据:
获取剪贴板里的数据时,是不知道当前剪贴板里是否有数据的,也不知道剪贴板里的数据格式是什么。那么下面就来解决这两个问题,先使用函数IsClipboardFormatAvailable来获取剪贴板里的格式是否可以处理,接着使用函数OpenClipboard打开剪贴板,然后使用函数GetClipboardData来获取剪贴板数据

源代码如下:
-----------------------------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  *******
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  s1:string;
  hmem:HGLOBAL;
  pstr:PChar;
begin
  s1:=‘猪悟能在test剪贴板‘;
  hmem:=GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE,Length(s1)+4);
  pstr:=GlobalLock(hmem);
  StrCopy(pstr,PChar(s1));
  if OpenClipboard(0)then
  begin
     SetClipboardData(CF_TEXT,hmem);
     CloseClipboard;
     GlobalUnlock(hmem);
     GlobalFree(hmem);
     ShowMessage(‘字符串已经复制到剪贴板‘);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  hmem:THandle;
  pstr:PChar;
begin
  //检查剪贴板类容类型
  if IsClipboardFormatAvailable(CF_TEXT) then
  begin
    OpenClipboard(0);
    hmem:=GetClipboardData(CF_TEXT);
    pstr:=GlobalLock(hmem);
    Memo1.Text:=pstr;
    GlobalUnlock(hmem);
    CloseClipboard;
  end;
end;

end.

用#9查找tab符号:

ipos := pos(#9,sTmp)

时间: 2024-08-29 13:47:21

delphi读写剪贴板的一些参考的相关文章

delphi读写INI系统配置文件

delphi读写INI系统配置文件 一.调用delphi内建单元 uses System.IniFiles; 1.使用类TIniFile 2.类TIniFile的主要方法和函数: {$IFDEF MSWINDOWS}   { TIniFile - Encapsulates the Windows INI file interface (Get/SetPrivateProfileXXX functions) }   TIniFile = class(TCustomIniFile)   public

delphi 读写文本文件(函数比较全)

需要两个按钮和两个Richedit控件,采用默认名称即可. procedure TForm1.Button1Click(Sender: TObject);  //写文件 var wText: TextFile;begin  AssignFile(wText, 'ip.txt');  Rewrite(wText);//创建文件,或者使用ReSet打开文件  Writeln(wText, richedit1.text);  CloseFile(wText);end; procedure TForm1

Delphi读写UTF-8、Unicode格式文本文件

// UTF-8文件写入函数procedure SaveUTFFile(const FileName: string; S: string; WriteHeader: Boolean = True);var  MemStream: TMemoryStream;  HeaderStr: String;begin  if S = '' then Exit; MemStream := TMemoryStream.Create;  try    if WriteHeader then    begin 

通过XmlDocument读写Xml文档参考地址

/// <summary> /// 获取一个报表的参数 http://blog.csdn.net/hdhai9451/article/details/12170069 /// </summary> public static ReportAdapterSection GetReportAdapterSectionByID(string ReportID, ProfileRole RoleType, ReportTemplateType TemplateType) { ReportA

delphi 读写文本

将字符串写入txt文档,读取txt文档中的内容. //一次写字符串到文本文件,每次都会将原来的内容替换掉. procedure FilePutContents(f,s:String); // f为文件名,s为存的内容 var ss:TStrings; begin ss:=TStringList.Create; ss.Text:=s; ss.SaveToFile(f, TEncoding.UTF8); ss.Free; end; 下面为不替换原来的内容,追加写入文本: procedure File

未测试 Delphi读写UTF-8、Unicode格式文本文件

// UTF-8文件写入函数 procedure SaveUTFFile(const FileName: string; S: string; WriteHeader: Boolean = True); var MemStream: TMemoryStream; HeaderStr: String; begin if S = '' then Exit; MemStream := TMemoryStream.Create; try if WriteHeader then begin HeaderS

python读写剪贴板

#coding:utf-8 import os import time import win32api import win32con import win32clipboard as w import subprocess striepath="c:\\PROGRA~1\\INTERN~1\\iexplore.exe" def getText(): w.OpenClipboard() d = w.GetClipboardData(win32con.CF_TEXT) w.CloseCl

Delphi 读写Excel

两种方法,一是用ADO连接,问题是Excel文件内容要规则,二是用OLE打开,但操作就没有象操作数据库那么方便了. 一.用ADO连接:设置属性ConnetionString 选择 Microsoft Jet 4.0 OLE DB provider Select or enter a datasorce name -> 选择你要打开Excel文件 User name默认是Admin 密码默认为空,可以不用理会 Extended properties 设为:Excel 8.0 sql语句 selec

delphi 读写文件属性

uses comobj,activex; const IID_IPropertySetStorage:TGUID = '{0000013A-0000-0000-C000-000000000046}'; FMTID_SummaryInformation:TGUID='{F29F85E0-4FF9-1068-AB91-08002B27B3D9}'; function StgOpenStorageEx( pwcsName: POleStr; grfMode: dword;   stgfmt: DWOR