C++Builder XE8でモバイルアプリケーションのassets\internal フォルダ(内部フォルダ)に配置したファイルを読み込むには

C++Builder XE8でモバイルアプリケーションのassets\internalフォルダ(内部フォルダ)に配置したファイルを読み込む方法を紹介します。

assets\internalフォルダ(内部フォルダ)にファイルを配置する

メニューの「プロジェクト」→「配置」を選択します。

10

「すべての構成 – すべてのプラットフォーム」を選択します。

11

「ファイルの追加」ボタンを押します。

12

追加するファイルを選択して、ファイルを登録します。

追加したファイルのリモートパスを編集して「assets\internal\」に書き換えます。

13

ファイルを読み込む

assets\internalフォルダに配置したファイルのディレクトリは、System::Ioutils::TPath::GetDocumentsPath()で取得できます。

#include <System.IOUtils.hpp>

UnicodeString path = System::Ioutils::TPath::Combine(
    System::Ioutils::TPath::GetDocumentsPath(), 
    L"save_256_h.png");

サンプルアプリケーション

次のサンプルアプリケーションでは、assets\internalフォルダに配置した画像ファイルを読み込んで、表示します。

フォームにボタンコンポーネント(TButton)とイメージコンポーネント(TImage)を配置します。

08

assets\internalフォルダに画像ファイルを配置します。

ボタンのOnClickイベントを記述します。

void __fastcall TForm2::Button1Click(TObject *Sender)
{
  UnicodeString path = System::Ioutils::TPath::Combine(System::Ioutils::TPath::GetDocumentsPath(), L"save_256_h.png");
  TBitmap* Bitmap = new TBitmap(path);

  Image1->Bitmap->Assign(Bitmap);
  delete Bitmap;
}

Androidで実行したところ

04

05

iOSシミュレータで実行したところ

06

07

XE8でTListBoxのScrollByメソッドが動かない問題が修正された

XE7までは、スクロールバーが表示されていないと、TListBoxのScrollByメソッドを呼んでもスクロールされませんでした。

XE8では、スクロールバーが表示されていなくても、スクロールされるようになりました。

XE7を使っている場合は、TCustomScrollBox.ScrollByのスクロールバーのVisibleプロパティのチェックを外しまします。

参考

Delphi XE8でリソースファイルの画像を読み込むには

Delphi XE8でリソースファイルの画像を読み込む方法を紹介します。

リソースファイルの登録

リソースファイルは、メニューの「プロジェクト」→「リソースと画像」から登録します。

01

02

03

リソース識別子とリソースタイプは、読み込むときに必要になるので、覚えておいてください。

リソースファイルの読み込み

リソースファイルを読み込むには、TResourceStreamを使用します。

TResourceStreamのコンストラクタの第二引数にリソース識別子、第三引数にリソースタイプを指定します。

RS := TResourceStream.Create(HInstance, 'PngImage_1', RT_RCDATA);

画像ファイルを読み込むには次のようなコードになります。

var
  RS: TResourceStream;
  Bitmap: TBitmap;
begin
  RS := TResourceStream.Create(HInstance, 'PngImage_1', RT_RCDATA);
  Bitmap:= TBitmap.CreateFromStream(RS);
  RS.Free;

サンプルアプリケーション

次のサンプルコードでは、ボタンを押すとリソースファイルの画像を読み込んで表示します。

フォームにボタンコンポーネント(TButton)とイメージコンポーネント(TImage)を配置します。

08

ボタンのOnClickイベントを記述します。

procedure TForm1.Button1Click(Sender: TObject);
var
  RS: TResourceStream;
  Bitmap: TBitmap;
begin
  RS := TResourceStream.Create(HInstance, 'PngImage_1', RT_RCDATA);
  Bitmap:= TBitmap.CreateFromStream(RS);
  RS.Free;

  Image1.Bitmap.Assign(Bitmap);
  Bitmap.Free;
end;

Androidで実行したところ

04

05

iOSシミュレータで実行したところ

06

07

C++Builder XE8でFiremonkeyアプリケーションでクリップボードに文字列をコピーする

C++Builder XE8でFiremonkeyアプリケーションでクリップボードに文字列をコピーする方法を紹介します。

クリップボードを使用するには、IFMXClipboardServiceインターフェースを使用します。

アプリケーションを実行しているプラットフォームがIFMXClipboardServiceを利用可能かどうかを調べます。

if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXClipboardService)))
{
  //IFMXClipboardServiceが利用な可能なとき
}

IFMXClipboardServiceが利用可能であれば、IFMXClipboardServiceのインスタンスを取得します。

_di_IFMXClipboardService ClipboardService = TPlatformServices::Current->GetPlatformService(__uuidof(IFMXClipboardService));

クリップボードに文字列をコピーするには、IFMXClipboardServiceのSetClipboardメソッドを使用します。

ClipboardService->SetClipboard(TValue::From(Memo1->Text));

クリップボードに文字列をコピーするコードは次のようになります。

#include <FMX.Platform.hpp>


if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXClipboardService)))
{
  _di_IFMXClipboardService ClipboardService = TPlatformServices::Current->GetPlatformService(__uuidof(IFMXClipboardService));
  ClipboardService->SetClipboard(TValue::From("コピーする文字列"));
}

次のサンプルアプリケーションでは、アプリケーションを実行しているプラットフォームで利用できるディレクトリのパスを取得して、クリップボードにコピーします。

フォームにボタンコンポーネント(TButton)とメモコンポーネント(TMemo)を配置します。

03

フォームのOnCreateイベントと、ボタンのOnClickイベントを記述します。

#include <fmx.h>
#pragma hdrstop

#include "Unit2.h"
#include <System.IOUtils.hpp>
#include <FMX.Platform.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormCreate(TObject *Sender)
{
  Memo1->Lines->Add("System::Ioutils::TPath::GetLibraryPath="+System::Ioutils::TPath::GetLibraryPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetDocumentsPath="+System::Ioutils::TPath::GetDocumentsPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetHomePath="+System::Ioutils::TPath::GetHomePath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetSharedAlarmsPath="+System::Ioutils::TPath::GetSharedAlarmsPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetPublicPath="+System::Ioutils::TPath::GetPublicPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetSharedDocumentsPath="+System::Ioutils::TPath::GetSharedDocumentsPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetDownloadsPath="+System::Ioutils::TPath::GetDownloadsPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetMoviesPath="+System::Ioutils::TPath::GetMoviesPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetMusicPath="+System::Ioutils::TPath::GetMusicPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetPicturesPath="+System::Ioutils::TPath::GetPicturesPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetRingtonesPath="+System::Ioutils::TPath::GetRingtonesPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetSharedDownloadsPath="+System::Ioutils::TPath::GetSharedDownloadsPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetSharedMoviesPath="+System::Ioutils::TPath::GetSharedMoviesPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetSharedMusicPath="+System::Ioutils::TPath::GetSharedMusicPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetSharedCameraPath="+System::Ioutils::TPath::GetSharedCameraPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetSharedPicturesPath="+System::Ioutils::TPath::GetSharedPicturesPath());
  Memo1->Lines->Add("System::Ioutils::TPath::GetSharedRingtonesPath="+System::Ioutils::TPath::GetSharedRingtonesPath());
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
  Memo1->SelectAll();

  if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXClipboardService)))
  {
    _di_IFMXClipboardService ClipboardService = TPlatformServices::Current->GetPlatformService(__uuidof(IFMXClipboardService));
    ClipboardService->SetClipboard(TValue::From(Memo1->Text));
  }
}
//---------------------------------------------------------------------------