大阪市中央区 システムソフトウェア開発会社

営業時間:平日09:15〜18:15
MENU

Code::Blocks+wxSmithでDrawing

株式会社クローバーフィールドの経営理念
著者:津路高広
公開日:2019/04/11
最終更新日:2019/04/11
カテゴリー:技術情報
タグ:

津路です。
前回では、目標として、画面に図形やテキストを描いて、更に、図形をファイルに保存することを考えました。
そして、まず、四角形をデバイスコンテキスト上に描きました。
次に、ブラシとペンを赤色に設定し、四角形を描きます。

    //set the brush and pen to Red
    dc.SetBrush(*wxRED_BRUSH);
    dc.SetPen(*wxRED_PEN);
    //draw a renctangle 40 * 40
    dc.DrawRectangle(10,10,40,40);

次に、四角形の右側に、緑の線を描きます。

   wxPen myGreenPen(*wxGREEN, 3, wxPENSTYLE_SOLID);
    dc.SetPen(myGreenPen);
    //draw a horizontal line
    dc.DrawLine(55,40,290,40);

次に、文字列を緑の線の上下に描きます。

    //set foreground to black
    dc.SetTextBackground(*wxBLACK);

    //put some text on the drawing
    dc.DrawText(wxT("テスト文字列"),50,60);

    //craete a 16 point , serif font, that is not bold
    wxFont BigFonr(16,wxFONTFAMILY_ROMAN,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false);
    dc.SetFont(BigFonr);
    dc.DrawText(wxT("Red Square"),60,10);

ここまでで、以下のような図形になります。

描かれた図形

更に、今日は、描いた図形をファイルに保存します。
まず、ボタンイベントを定義し、メモリに描いて、それをファイルに保存します。

    wxBitmap *paper = new wxBitmap(300,200);
    wxMemoryDC memDC;
    memDC.SelectObject(*paper);
    PictureFrame* fram = new PictureFrame(this);
    fram->Repin(memDC);  //Repin関数は、上記の描画を行う関数です。
    memDC.SelectObject(wxNullBitmap);
    //put the cotentens of paper into a png and into a jpeg file
    paper->SaveFile(_T("RedSquare.png"), wxBITMAP_TYPE_PNG, NULL);
    paper->SaveFile(_T("RedSquare.jpg"), wxBITMAP_TYPE_JPEG, NULL);
    delete paper;
    上に戻る