LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder XE4 > 実装例: (a)TPanel上の表示をStretchBlt()使用で拡大 + (b)クリップボードへ送る + (c)ペイントブラシ起動

Last updated at Posted at 2018-12-25
動作環境
C++ Builder XE4
Windows 7 pro (32bit)

処理概要

備考 > Clipboard()

#include <ClipBrd.hpp>
がClipboard()のために必要。

フォームデザイン

2018-12-25_14h05_15.png

  • TPanel(Panel1)上にTShapeで適当な模様を作成
    • TPanel上の画像をキャプチャする例として

実装例

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>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    TPanel *Panel1;
    TShape *Shape1;
    TShape *Shape2;
    TShape *Shape3;
    TButton *Button1;
    void __fastcall Button1Click(TObject *Sender);
private:    // ユーザー宣言
    void __fastcall TForm1::copyToClipboard(TPanel *panelPtr);
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::copyToClipboard(TPanel *panelPtr)
{
    if (panelPtr == NULL) {
        return; // error
    }

    TBitmap *lBmp = new TBitmap();
    try {
        int dstWidth = panelPtr->ClientWidth * 4;  // 72dpi -> 約300dpi
        int dstHeight = panelPtr->ClientHeight * 4;  // 72dpi -> 約300dpi

        lBmp->SetSize(dstWidth, dstHeight);
        SetStretchBltMode(lBmp->Canvas->Handle, HALFTONE);
        HDC dc = GetDC(panelPtr->Handle);
        ::StretchBlt(lBmp->Canvas->Handle, 0, 0, dstWidth, dstHeight, dc, 0, 0, panelPtr->ClientWidth, panelPtr->ClientHeight, SRCCOPY);
        Clipboard()->Assign(lBmp);
    } catch (...) {
    }
    delete lBmp;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    copyToClipboard(Panel1);

    // ペイントブラシ起動
    ShellExecute(NULL, NULL, L"pbrush.exe", L"", NULL, SW_SHOWNORMAL);
}
//---------------------------------------------------------------------------

動作例

  1. ソフトを実行する
    • => ペイントブラシが起動する
  2. Ctrl+Vする
    • => 画像が貼付けされる

2018-12-25_14h07_11.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