LoginSignup
6
7

More than 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > htmlファイルを作ってInternetExplorer / Chrome で表示する

Last updated at Posted at 2015-09-03
動作確認
C++ Builder XE4

TMemoで色付け表示したかったが、htmlファイルを作成してIEで開いてもいいかも、と思い始めた。

IEで開く

IEでファイルを開く処理は以下のようにできる。

参考
http://kwikwi.cocolog-nifty.com/blog/2005/12/bcb_shellexecut_ea97.html

void __fastcall TForm1::Button1Click(TObject *Sender)
{

    makeDummyHtml();

    String filename = L"index.html";
    String curDir = ExtractFileDir(Application->ExeName);

    String filepath = curDir + L"\\" + filename;

    if (FileExists(filepath)) {
        ShellExecute(NULL, NULL, L"iexplore.exe", filepath.c_str(), NULL, SW_SHOWNORMAL);
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::makeDummyHtml()
{
    TStringList *fileSL = new TStringList();

    String line = L"<font color=red>Hello World</font>";
    line = line + L" on " + Now().FormatString(L"yyyy/mm/dd hh:nn:ss");
    fileSL->Add(line);

    fileSL->SaveToFile(L"index.html");

    delete fileSL;
}
//---------------------------------------------------------------------------

上記を実行すれば
Hello World on 2015/09/03 10:19:39
のような表示がIEで見える。

chromeで開く

chromeの場合は、実行exeを"chrome.exe"に変更する。

しかしながら、フォルダ名などに空白が含まれている場合、そのままでは空白までのパス認識しかされずファイルオープン失敗する。
(C:\\Rad Studio\index.htmlC:\\Radになる)。

chrome用には以下のように空白を「%20」に変更する必要がある。

void __fastcall TForm1::Button1Click(TObject *Sender)
{

    makeDummyHtml();

    String filename = L"index.html";
    String curDir = ExtractFileDir(Application->ExeName);

    String filepath = curDir + L"\\" + filename;
    String url;

    // for chrome
    url = StringReplace(filepath, L" ", L"%20", TReplaceFlags()<<rfReplaceAll);

    if (FileExists(filepath)) {
        ShellExecute(NULL, NULL, L"iexplore.exe", filepath.c_str(), NULL, SW_SHOWNORMAL);
        ShellExecute(NULL, NULL, L"chrome.exe", url.c_str(), NULL, SW_SHOWNORMAL);
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::makeDummyHtml()
{
    TStringList *fileSL = new TStringList();

    String line = L"<font color=red>Hello World</font>";
    line = line + L" on " + Now().FormatString(L"yyyy/mm/dd hh:nn:ss");
    fileSL->Add(line);

    fileSL->SaveToFile(L"index.html");

    delete fileSL;
}
//---------------------------------------------------------------------------

10.2 Tokyo

動作確認
Rad Studio 10.2 Tokyo Update 2

10.2 TokyoでビルドしたソフトをWindows 10 Pro上で動かし、IEとChromeでの動作を確認した。

Windows 10 Proの場合はIEでなくEdgeを使うという選択肢もある。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7