LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder XE4 > Clipboard > クリップボードへ画像を送り、クリップボードから画像ファイルへ保存

Last updated at Posted at 2018-12-07
動作環境
C++ Builder XE4

関連

処理概要

  • TeeChartのChart1をクリップボードへ送る
  • クリップボードから画像ファイル(.bmp)へ保存する

参考

フォームデザイン

2018-12-07_13h27_23.png

実装

Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
#include <VCLTee.Chart.hpp>
#include <VCLTee.TeEngine.hpp>
#include <VCLTee.TeeProcs.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    TChart *Chart1;
    TButton *Button1;
    TImage *Image1;
    void __fastcall Button1Click(TObject *Sender);
private:    // ユーザー宣言
public:     // ユーザー宣言
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <ClipBrd.hpp>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    // 1. Clipboardへ
    TBitmap *lBmp = new TBitmap();
    try {
        lBmp->SetSize(Chart1->ClientWidth, Chart1->ClientHeight);
        HDC dc = GetDC(Chart1->Handle);
        BitBlt(lBmp->Canvas->Handle, 0, 0, Chart1->ClientWidth, Chart1->ClientHeight, dc, 0, 0, SRCCOPY);
        Clipboard()->Assign(lBmp);
    } catch (...) {
    }
    delete lBmp;

    // 2. Clipboardから画像ファイルへ保存
    if ( Clipboard()->HasFormat(CF_BITMAP) ) {
        Image1->Picture->Assign(Clipboard());
        Image1->Picture->SaveToFile(L"clip.bmp");
    }
}
//---------------------------------------------------------------------------

動作例

  1. Button1を押す

clip.bmpが.exeファイルと同じフォルダに保存される。

clip.bmp (Windowsフォトビューアーで開いた状態)
2018-12-07_13h31_34.png

0
1
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
0
1