![]() |
TIPS集 |
カスタム検索
|
| お断り: | ここに掲載しているサンプルコードはだいぶ古いものもあり、最新のOSや開発ツールでは動作しないものもあるかもしれません。 あらかじめ、ご了承ください。 |
procedure …… var ErrorCode: Integer; Buff: array[0..255] of Char; begin : ErrorCode := GetLastError; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, ErrorCode, LANG_SYSTEM_DEFAULT, Buff, 255, nil); Application.MessageBox(Buff, 'Error', IDOK) end;
procedure …… var ErrorCode: Integer; Buff: array[0..255] of Char; Str: array[0..1] of PChar; begin : ErrorCode := GetLastError; Str[0] := '引数1'; Str[1] := '引数2'; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_ARGUMENT_ARRAY, nil, ErrorCode, LANG_SYSTEM_DEFAULT, Buff, 255, @Str); Application.MessageBox(Buff, 'Error', IDOK) end;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Menus, ShellAPI;
const
MY_NOTIFYICON = WM_USER + 102;
type
TNotifyWindow = class(TWinControl)
private
procedure MyNotifyIcon(var Msg: TMessage); message MY_NOTIFYICON;
public
end;
TForm1 = class(TForm)
Button1: TButton;
PopupMenu1: TPopupMenu;
MenuRestore: TMenuItem;
procedure Button1Click(Sender: TObject);
procedure MenuRestoreClick(Sender: TObject);
private
public
end;
var
Form1: TForm1;
FNotifyWindow: TNotifyWindow;
NtIconData: TNotifyIconData;
implementation
{$R *.DFM}
// ウィンドウをタスクトレイに収容する
procedure TForm1.Button1Click(Sender: TObject);
begin
FNotifyWindow := TNotifyWindow.Create(Self);
FNotifyWindow.Parent := TWinControl(Self);
with NtIconData do begin
cbSize := SizeOf(TNOTIFYICONDATA);
Wnd := FNotifyWindow.Handle;
uCallBackMessage := MY_NOTIFYICON;
uId := 1;
uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
hIcon := Application.Icon.Handle;
StrPCopy(szTip, Caption);
end;
Shell_NotifyIcon(NIM_ADD, Addr(NtIconData));
Form1.Visible := False;
end;
// タスクトレイでのイベント処理
procedure TNotifyWindow.MyNotifyIcon(var Msg: TMessage);
var Pos: TPoint;
begin
GetCursorPos(Pos);
case Msg.lParam of
WM_LBUTTONDOWN:
begin
Form1.PopupMenu1.Popup(Pos.X, Pos.Y);
end;
end;
end;
{タスクトレイからはずす}
procedure TForm1.MenuRestoreClick(Sender: TObject);
begin
if FNotifyWindow <> nil then begin
Shell_NotifyIcon(NIM_DELETE, Addr(NtIconData));
FNotifyWindow.Free;
FNotifyWindow := nil;
end;
Form1.Visible := True;
end;
end.
TabUnits := 16; SendMessage(Memo.Handle, EM_SETTABSTOPS, 1, LPARAM(@TabUnits));
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ShellAPI;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private 宣言 }
procedure WMDropFile(var Msg: TWMDropFiles); message WM_DROPFILES;
public
{ Public 宣言 }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle, True); {Drag & Dropを受入可能にする}
end;
procedure TForm1.WMDropFile(var Msg: TWMDropFiles);
var FileName: array[0..255] of Char;
Cnt, K: Integer;
begin
Cnt := DragQueryFile(Msg.Drop, -1, FileName, SizeOf(FileName));
for K := 0 to Cnt - 1 do begin
DragQueryFile(Msg.Drop, K, FileName, SizeOf(FileName));
{FileNameにdropされたファイル名が入っているので、ここで何らかの処理をする。たとえば次の行}
Application.MessageBox(FileName, 'Dropped File', IDOK);
end;
DragFinish(Msg.Drop);
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ActiveX;
type
TForm1 = class(TForm)
CreateButton: TButton;
ReadButton: TButton;
Memo1: TMemo;
procedure CreateButtonClick(Sender: TObject);
procedure ReadButtonClick(Sender: TObject);
procedure ReadStorage(Stg: IStorage; Level: Integer);
private
{ Private 宣言 }
public
{ Public 宣言 }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.CreateButtonClick(Sender: TObject);
var WStr: array[0..255] of WChar;
StgRoot, Stg: IStorage;
Stm: IStream;
Ret: HRESULT;
Bytes: Integer;
Txt: String;
begin
OleInitialize(nil);
StgCreateDocFile(StringToWideChar('Sample', WStr, 255),
STGM_READWRITE or STGM_CREATE or
STGM_DIRECT or STGM_SHARE_EXCLUSIVE, 0, StgRoot);
if StgRoot = nil then exit;
Ret := StgRoot.CreateStorage(StringToWideChar('Storage1', WStr, 255),
STGM_WRITE or STGM_CREATE or
STGM_DIRECT or STGM_SHARE_EXCLUSIVE, 0, 0, Stg);
if Ret = S_OK then begin
Ret := Stg.CreateStream(StringToWideChar('Stream1', WStr, 255),
STGM_WRITE or STGM_CREATE or
STGM_DIRECT or STGM_SHARE_EXCLUSIVE, 0, 0, Stm);
if Ret = S_OK then begin
Txt := 'Sample Data';
Stm.Write(PChar(Txt), Length(Txt), @Bytes);
end;
end;
OleUninitialize;
end;
procedure TForm1.ReadButtonClick(Sender: TObject);
var WStr: array[0..255] of WChar;
StgRoot: IStorage;
Ret: HRESULT;
begin
OleInitialize(nil);
Ret := StgOpenStorage(StringToWideChar('Sample', WStr, 255),
nil, STGM_READWRITE or STGM_DIRECT or STGM_SHARE_EXCLUSIVE,
nil, 0, StgRoot);
if Ret = S_OK then ReadStorage(StgRoot, 0);
OleUninitialize;
end;
procedure TForm1.ReadStorage(Stg: IStorage; Level: Integer);
var ChildStg: IStorage;
Stm: IStream;
Enum: IENumStatStg;
StatStg: TStatStg;
Ret: HRESULT;
Count, Bytes, Len: Integer;
Buff: Pointer;
procedure Display(Level: Integer; Txt: String);
begin
Memo1.Lines.Add(StringOfChar(' ', Level * 4) + Txt);
end;
begin
Ret := Stg.EnumElements(0, nil, 0, Enum);
if Ret <> S_OK then exit;
Buff := AllocMem(256);
while True do begin
Ret := Enum.Next(1, StatStg, @Count);
if Ret <> S_OK then break;
case StatStg.dwType of
STGTY_STREAM:
begin
Ret := Stg.OpenStream(StatStg.pwcsName, nil,
STGM_READ or STGM_DIRECT or STGM_SHARE_EXCLUSIVE,
0, Stm);
if Ret = S_OK then begin
Bytes := Round(StatStg.cbSize);
Display(Level, WideCharToString(StatStg.pwcsName));
Stm.Read(Buff, Bytes, @Len);
Display(Level + 1, PChar(Buff));
end;
end;
STGTY_STORAGE:
begin
Ret := Stg.OpenStorage(StatStg.pwcsName, nil,
STGM_READ or STGM_DIRECT or STGM_SHARE_EXCLUSIVE,
nil, 0, ChildStg);
if Ret = S_OK then begin
Display(Level, WideCharToString(StatStg.pwcsName));
ReadStorage(ChildStg, Level + 1);
end;
end;
else continue;
end;
end;
FreeMem(Buff);
end;
end.
SetCursorPos(x, y); mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
keybd_event(VK_SNAPSHOT, 0, 0, 0); keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0);
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
PLVItem = ^TLVItem;
TLVItem = packed record
mask: UINT;
iItem: Integer;
iSubItem: Integer;
state: UINT;
stateMask: UINT;
pszText: PChar;
cchTextMax: Integer;
iImage: Integer;
lParam: LPARAM;
end;
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private 宣言 }
public
{ Public 宣言 }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
const
LVM_FIRST = $1000;
LVM_GETITEM = LVM_FIRST + 5;
LVM_GETNEXTITEM = LVM_FIRST + 12;
LVIF_TEXT = 1;
LVNI_ALL = 0;
LVNI_FOCUSED = 1;
LVNI_SELECTED = 2;
procedure TForm1.Button1Click(Sender: TObject);
var hWin: HWND;
hFileMap, ItemNo: Integer;
pItem: PLVItem;
Txt: String;
begin
hWin := FindWindow('ExploreWClass', nil);
if hWin = 0 then Txt := 'エクスプローラが起動されていません'
else begin
hWin := FindWindowEx(hWin, 0, 'SHELLDLL_DefView', nil);
hWin := FindWindowEx(hWin, 0, 'SysListView32', nil);
ItemNo := SendMessage(hWin, LVM_GETNEXTITEM, -1, LVNI_ALL or LVNI_FOCUSED);
hFileMap := CreateFileMapping(-1, nil, PAGE_READWRITE, 0, $1000, 'MyMap');
if hFileMap <> 0 then begin
pItem := MapViewOfFile(hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
with pItem^ do begin
mask := LVIF_TEXT;
iSubItem := 0;
iItem := ItemNo;
pszText := Ptr(Integer(pItem) + Sizeof(TLVItem));
cchTextMax := 255;
end;
end;
SendMessage(hWin, LVM_GETITEM, 0, Integer(pItem));
Edit1.Text := pItem^.pszText;
end;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMessageList = class(TList)
public
destructor Destroy; override;
end;
TForm1 = class(TForm)
RecBtn: TButton;
PlayBtn: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure RecBtnClick(Sender: TObject);
procedure PlayBtnClick(Sender: TObject);
procedure WatchMessage(var Msg: TMsg; var Handled: Boolean);
private
{ Private 宣言 }
public
{ Public 宣言 }
end;
function RecordProc(nCode: Integer; wParam: WPARAM; pEvnt: PEventMsg): LRESULT; stdcall;
function PlayProc(nCode: Integer; wParam: WPARAM; pEvnt: PEventMsg): LRESULT; stdcall;
procedure StopPlay;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
MessageList: TMessageList;
JournalHook: Integer;
Idx: Integer;
procedure TForm1.FormCreate(Sender: TObject);
begin
MessageList := TMessageList.Create;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
MessageList.Free;
end;
{記録開始}
procedure TForm1.RecBtnClick(Sender: TObject);
begin
JournalHook := SetWindowsHookEx(WH_JOURNALRECORD, @RecordProc, hInstance, 0);
end;
{記録中、イベント毎に呼び出される}
function RecordProc(nCode: Integer; wParam: WPARAM; pEvnt: PEventMsg): LRESULT; stdcall;
var pMes: PEventMsg;
begin
if nCode < 0 then
Result := CallNextHookEx(JournalHook, nCode, wParam, Longint(pEvnt))
else begin
Result := 0;
if nCode = HC_ACTION then begin
case pEvnt^.Message of
WM_SYSKEYDOWN: ;
WM_KEYDOWN: ;
WM_SYSKEYUP: ;
WM_KEYUP: ;
WM_MOUSEMOVE: ;
WM_LBUTTONDOWN: ;
WM_LBUTTONUP: ;
WM_RBUTTONDOWN: ;
WM_RBUTTONUP: ;
end;
New(pMes);
pMes^ := pEvnt^;
MessageList.Add(pMes);
end;
end;
end;
{再生開始}
procedure TForm1.PlayBtnClick(Sender: TObject);
begin
Idx := 0;
JournalHook := SetWindowsHookEx(WH_JOURNALPLAYBACK, @PlayProc, hInstance, 0);
Application.OnMessage := Form1.WatchMessage;
while JournalHook <> 0 do Application.ProcessMessages;
end;
{再生中に定期的に呼び出される}
function PlayProc(nCode: Integer; wParam: WPARAM; pEvnt: PEventMsg): LRESULT; stdcall;
begin
if nCode < 0 then
Result := CallNextHookEx(JournalHook, nCode, wParam, Longint(pEvnt))
else begin
Result := 0;
if nCode = HC_SKIP then begin
if Idx < MessageList.Count - 1 then inc(Idx)
else StopPlay;
end
else if nCode = HC_GETNEXT then begin
pEvnt^ := PEventMsg(MessageList[Idx])^;
case pEvnt^.message of
WM_KEYDOWN,
WM_KEYUP:
begin
pEvnt^.paramH := Hi(pEvnt^.paramL); // ******
pEvnt^.paramL := Lo(pEvnt^.paramL); // ******
end;
end;
Result := 0;
end;
end;
end;
{記録、再生停止}
procedure StopPlay;
begin
if JournalHook <> 0 then begin
UnhookWindowsHookEx(JournalHook);
JournalHook := 0;
Application.OnMessage := nil;
end;
end;
{CTRL-ESC, Alt+CTRL+DEL のいずれかのキーを押して記録または
再生を中止した}
procedure TForm1.WatchMessage(var Msg: TMsg; var Handled: Boolean);
begin
if Msg.Message = wm_CancelJournal then begin
JournalHook := 0;
Application.OnMessage := nil;
end;
end;
destructor TMessageList.Destroy;
var K: Integer;
begin
for K := 0 to Count - 1 do Dispose(PEventMsg(Items[K]));
inherited Destroy;
end;
end.
// uses節にShellAPIを追加すること
procedure TForm1.Button1Click(Sender: TObject);
var shRec : TSHFileOpStruct;
pathNames: String;
begin
pathNames := ExpandFileName('Sample1') + #0 + ExpandFileName('Sample2') + #0#0;
with shRec do begin
wnd := Handle;
wFunc := FO_DELETE;
pFrom := PChar(pathNames);
pTo := nil;
fFlags := FOF_ALLOWUNDO;
fAnyOperationsAborted := False;
hNameMappings := nil;
lpszProgressTitle := nil;
end;
SHFileOperation(shRec);
end;
// 手法 1. (LoadFromStream): 5000行セットするのに1秒とかからなかった
procedure TForm1.Button1Click(Sender: TObject);
var MyStream: TMemoryStream;
Txt: String;
LineNo: Integer;
begin
MyStream := TMemoryStream.Create;
try
for LineNo := 1 to 5000 do begin
Txt := 'This line number is ' + IntToStr(LineNo) + #13#10;
MyStream.Write(PChar(Txt)^, Length(Txt));
end;
MyStream.Position := 0;
RichEdit1.Lines.LoadFromStream(MyStream);
finally
MyStream.Free;
end;
end;
// 手法 2. (Lines.Add): 2500行くらいセットするのに10秒かかり、しかも
// EOutOfResource例外が発生した(例外の原因不明)
procedure TForm1.Button2Click(Sender: TObject);
var Txt: String;
LineNo: Integer;
begin
RichEdit1.Lines.Clear;
for LineNo := 1 to 5000 do begin
Txt := 'This line number is ' + IntToStr(LineNo);
RichEdit1.Lines.Add(Txt);
end;
end;
function GetFocusWindow: HWND;
var ActiveProcessID, ActiveThreadID: DWORD;
begin
ActiveThreadID := GetWindowThreadProcessID(GetForegroundWindow, @ActiveProcessID);
if AttachThreadInput(GetCurrentThreadID, ActiveThreadID, True) then begin
Result := GetFocus;
AttachThreadInput(GetCurrentThreadID, ActiveThreadID, False);
end
else Result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var hWin: HWND;
DC: HDC;
begin
hWin := FindWindow('Progman', nil);
hWin := FindWindowEx(hWin, 0, 'SHELLDLL_DefView', nil);
hWin := FindWindowEx(hWin, 0, 'SysListView32', nil);
DC := GetDC(Handle);
SendMessage(hWin, WM_ERASEBKGND, DC, 0);
SendMessage(hWin, WM_PAINT, DC, 0);
ReleaseDC(Handle, DC);
end;
procedure TForm1.Button1Click(Sender: TObject);
var StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
hRead, hWrite: THandle;
Buff: array[0..4095] of Char;
BuffSize, Len: Integer;
begin
CreatePipe(hRead, hWrite, nil, 0);
FillChar(StartupInfo, Sizeof(StartupInfo), 0);
with StartupInfo do begin
cb := Sizeof(StartupInfo);
dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
hStdOutput := hWrite;
wShowWindow := SW_HIDE;
end;
if CreateProcess('\Windows\Command\Attrib.exe', nil, nil, nil,
True, 0, nil, nil, StartupInfo, ProcessInfo) then begin
while WaitForSingleObject(ProcessInfo.hProcess, 500) = WAIT_TIMEOUT do
Application.ProcessMessages;
CloseHandle(hWrite);
BuffSize := Sizeof(Buff) - 1;
while True do begin
Len := FileRead(hRead, Buff, BuffSize);
Buff[Len] := #0;
Memo1.Lines.Add(Buff);
if Len < BuffSize then break;
end;
end;
CloseHandle(hRead);
end;
hDosWin := FindWindow('tty', nil);
PostMessage(hDOSWin, WM_COMMAND, 57360, 0);
2) keybd_eventを使って、キーイベントを発生させる方法
hDosWin := FindWindow('tty', nil);
SetForegroundWindow(hDosWin);
keybd_event(VK_CANCEL, MapVirtualKey(VK_CANCEL, 0), 0, 0);
keybd_event(VK_CANCEL, MapVirtualKey(VK_CANCEL, 0), KEYEVENTF_KEYUP, 0);
procedure DisplayGIF(wFileName: WideString);
var Flags, TargetFrameName, PostData, Headers: OleVariant;
begin
Flags := 0;
WebBrowser1.Navigate(wFileName, Flags, TargetFrameName, PostData, Headers);
end;
var WebBrowser1: TWebBrowser;
procedure TForm1.CreateBrowserControl;
begin
if WebBrowser1 = nil then begin
try
WebBrowser1 := TWebBrowser.Create(Self);
with WebBrowser1 do begin
Align := alClient;
TOleControl(WebBrowser1).Parent := Self;
HandleNeeded;
end;
except
// Shdocvw.dll が存在しないときの処理
end;
end;
end;
function GetFocusInAnotherProcess: HWND;
var hWin: HWND;
TID, PID, MyID: Integer;
begin
MyID := GetCurrentThreadID;
hWin := GetForegroundWindow;
TID := GetWindowThreadProcessID(hWin, @PID);
if AttachThreadInput(TID, MyID, True) then begin
SetForegroundWindow(hWin);
Result := GetFocus;
AttachThreadInput(TID, MyID, False);
else Result := 0;
end;
procedure FMain.ChangeFont;
var M, D: Integer;
begin
if FontDialog.Execute then begin
M := FontDlg.Font.Size; // 新しいフォントサイズ
D := FMain.Font.Size; // 元のフォントサイズ
ChangeScale(M, D);
:
end;
end;
Mainフォームの場合は上記でOKですが、Mainフォーム以外では、ChangeScaleが未定義というコンパイルエラーになりますので、次のような宣言を追加する必要があります。
type
TFSubForm = class(TForm)
:
private
{ Private 宣言 }
public
{ Public 宣言 }
procedure ChangeScale(M, D: Integer); override;
end;
:
implementation
:
procedure TFSubForm.ChangeScale(M, D: Integer);
begin
inherited;
end;
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
GFSR_SYSTEMRESOURCES = 0;
GFSR_GDIRESOURCES = 1;
GFSR_USERRESOURCES = 2;
var
Form1: TForm1;
pFunc: Pointer;
function LoadLibrary16(LibraryName: PChar): THandle; stdcall;
external kernel32 index 35;
procedure FreeLibrary16(HInstance: THandle); stdcall;
external kernel32 index 36;
function GetProcAddress16(Hinstance: THandle; ProcName: PChar): Pointer; stdcall;
external kernel32 index 37;
procedure QT_Thunk; cdecl; external kernel32 name 'QT_Thunk';
function GetFreeRes(var nSystem: WORD; var nGDI: WORD; var nUser: WORD): Boolean;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var nSystem, nGDI, nUser: WORD;
begin
if GetFreeRes(nSystem, nGDI, nUser) then begin
Edit1.Text := IntToStr(nSystem) + ' %';
Edit2.Text := IntToStr(nGDI) + ' %';
Edit3.Text := IntToStr(nUser) + ' %';
end;
end;
function GetFreeRes(var nSystem: WORD; var nGDI: WORD; var nUser: WORD): Boolean;
var hInst16: THandle;
function CallThunk(nArg: WORD): WORD;
var R1: WORD;
begin
asm
sub esp, $42 // Reserve ThunkTrash area
mov ax, nArg
push ax
mov edx, pFunc
call QT_Thunk
mov R1, ax
add esp, $42
end;
Result := R1;
end;
begin
Result := False;
hInst16 := LoadLibrary16('User.exe');
if hInst16 < 32 then exit;
pFunc := GetProcAddress16(hInst16, 'GetFreeSystemResources');
Result := (pFunc <> nil);
if Result then begin
nSystem := CallThunk(GFSR_SYSTEMRESOURCES);
nGDI := CallThunk(GFSR_GDIRESOURCES);
nUser := CallThunk(GFSR_USERRESOURCES);
end;
FreeLibrary16(hInst16);
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Forms, Clipbrd;
const
WM_POSTRENDER = WM_USER + 100;
type
TForm1 = class(TForm)
procedure FormActivate(Sender: TObject);
private
{ Private 宣言 }
SeqNo: Integer;
procedure WMRender(var Msg: TMessage); message WM_RENDERFORMAT;
procedure WMPostRender(var Msg: TMessage); message WM_POSTRENDER;
procedure SetRender;
public
{ Public 宣言 }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormActivate(Sender: TObject);
begin
SeqNo := 1;
SetRender;
end;
{アプリケーションでの貼り付け操作を検出した}
procedure TForm1.WMRender(var Msg: TMessage);
var Txt: String;
Data: THandle;
Buff: Pointer;
begin
// 実際のデータをレンダリングする
OpenClipboard(Handle);
EmptyClipboard;
Txt := Format('Sample Data %d', [SeqNo]);
Data := GlobalAlloc(GMEM_MOVEABLE, Length(Txt) + 1);
Buff := GlobalLock(Data);
StrCopy(Buff, PChar(Txt));
SetClipboardData(CF_TEXT, Data);
GlobalUnlock(Data);
PostMessage(Handle, WM_POSTRENDER, 0, 0);
end;
// 次の貼り付け操作に備える
procedure TForm1.WMPostRender(var Msg: TMessage);
begin
Sleep(100);
inc(SeqNo);
SetRender;
end;
// 遅延レンダリングを行うために、クリップボードのデータハンドルを
// NULLにセットする
procedure TForm1.SetRender();
begin
OpenClipboard(Handle);
EmptyClipboard;
SetClipboardData(CF_TEXT, 0);
CloseClipboard;
end;
end.
function GrabProc(hWin: HWnd; AMessage, WParam, LParam: Longint): Longint; stdcall; export;{ stdcall を忘れないように }
procedure CreateGrabWindow;
var WinClass: TWndClass;
begin
FillChar(WinClass, Sizeof(WinClass), #0);
{デフォルト値(=0)のパラメータは、上の行でまとめてセットしていることに注意}
WinClass.lpfnWndProc := @GrabProc;
WinClass.hInstance := hInstance;
WinClass.hCursor := LoadCursor(0, IDC_HELP);
WinClass.hbrBackground := GetStockObject(NULL_BRUSH);
WinClass.lpszClassName := 'FGrab';
if Windows.RegisterClass(WinClass) <> 0 then begin
GrabWindow := CreateWindow('FGrab', '', WS_POPUP, 0, 0,
Screen.Width, Screen.Height, 0, 0, hInstance, nil);
end;
end;
function GrabProc(hWin: HWnd; AMessage, WParam, LParam: Longint): Longint;
begin
case AMessage of
WM_LBUTTONDOWN:
begin
{ここに透明ウィンドウ上での何らかの処理(たとえばマウス
左ボタンを押した時の処理)を記述します}
end;
end;
Result := DefWindowProc(hWin, AMessage, WParam, LParam);
end;
