忍者ブログ

[PR]

2025年04月29日
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

フォームへのファイルドラッグ対応

2013年11月24日

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    procedure WMDropFiles(var msg: TWMDROPFILES); message WM_DROPFILES;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  //ファイルのドラッグ受け入れ準備
  DragAcceptFiles(Handle, true);
end;

procedure TForm1.WMDropFiles(var msg: TWMDROPFILES);
var
  Count    : Integer;
  FileName : String;
begin
  try
    //ドラッグされたファイルの数を取得(今回は無視) 
    Count := DragQueryFile(msg.Drop, $ffffffff, nil, 0);
    //ファイル名を受け取る文字列を必要な容量分確保する
    SetLength( FileName , DragQueryFile(msg.Drop, 0, nil, 0) + 1 );
    //変数FailNameにドラッグされたファイルのフルパスを代入
    DragQueryFile(msg.Drop, 0, @FileName[1], Length(FileName));
  finally
    //ドラッグ終了
    DragFinish(msg.Drop);
  end;
end;

拍手[0回]

PR

多重起動の防止

2013年11月06日

アプリケーションの多重起動を抑止します。
アプリケーションのソースを表示し(.dpr)下記のように変更します。

program Project1;

uses
  Windows,
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

const
  MutexName      = 'TEST_APPLICATION'; //この文字列を一意になるよう変更
var
  hMutex : THANDLE;
begin
  hMutex := OpenMutex(MUTEX_ALL_ACCESS, False, MutexName);
  if (hMutex <> 0) then begin CloseHandle(hMutex); Exit; end;
  hMutex := CreateMutex(nil, False, MutexName);

  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;

  CloseHandle(hMutex);
end.

MutexNameの文字列は自由に変更して使いますが、他のミューテックス名と被ると一つも起動できなくなる恐れがありますのでユニークになるように工夫してください。

拍手[1回]

アプリ自身のバージョン取得

2013年11月06日

プロジェクトのオプションよりバージョン番号を含めると、アプリケーションファイル内にバージョン情報を埋め込むことが出来ます。
その自身のバージョン番号を取得する方法は下記のとおりです。
文字列として取得するようにしたのでフォームのキャプションなどにそのまま使えます。
※新しいDelphiで64bitアプリとして生成されたプログラムでは動作しないようです。

function GetSelfVersion: String;
var
  VerInfoSize  : DWORD;
  VerInfo      : Pointer;
  VerValueSize : DWORD;
  VerValue     : PVSFixedFileInfo;
  Dummy        : DWORD;
begin
  VerInfoSize := GetFileVersionInfoSize( PChar(ParamStr(0)), Dummy );

  GetMem(VerInfo, VerInfoSize);
  try
    GetFileVersionInfo( PChar(ParamStr(0)), 0, VerInfoSize, VerInfo );
    VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);

    with VerValue^ do begin
      Result := Format('Ver%d.%d.%.3d [Build:%.4d]' , [(dwFileVersionMS shr 16)
                                          , (dwFileVersionMS and $FFFF)
                                          , (dwFileVersionLS shr 16)
                                          , (dwFileVersionLS and $FFFF)])
    end;
  finally
    FreeMem(VerInfo, VerInfoSize);
  end;
end;

拍手[2回]

アプリを起動できるWindowsのバージョンを限定する

2013年11月06日

SysUtilsのCheckWin32Versionを使えば簡単にWindowsのバージョンが要望を満たしているのか調べることが出来ます。

procedure TForm1.FormCreate(Sender: TObject);
begin
  //OSバージョンチェック、Windows7以降なら起動できる
  if not CheckWin32Version( 6, 1 ) then begin
    MessageDlg('残念ながらこのWindowsのバージョンでは動作を認められていません。'#13#10'アプリケーションを終了します。', mtError, [mbOk], 0);
    Application.Terminate; Exit;
  end;
end;

CheckWin32Versionの引数はMajorとMinorの数値。
Windows2000は5.0
WindowsXPは5.1
WindowsVistaは6.0
Windows7は6.1
Windows8は6.2
Windows8.1は6.3

Windowsのバージョンが引数に指定したものと同等かそれより新しければTrueが帰ります。

拍手[0回]

カンタン入力

2013年11月01日
例えばメモリーリークを検出するようにしようとして

 ReportMemoryLeaksOnShutdown := True;

と入力しようとした時、
Ctl + Spaceキーを押してからr,e,p,oと押していけば入力候補が少しずつ絞られていき、すぐに入力できるようになります。

拍手[0回]