quarta-feira, 6 de fevereiro de 2019

C++ builder - criando e abrindo um novo Form

Levei muitas pedradas dos inimigos do java,
e o pior é que muitos comentam até do que
nada entendem,mas isto eu prefiro esquecer.
Resolvi programar um pouco em C++,
e minha IDE preferida para C++ orientado a objetos
é o Rad Studio da Embarcadero,
e a versão que estou usando é a 10.2.2.
Neste prático exemplo indicado aos embarcaderos
estamos mostrando como criar uma barra de menu,
itens de menu, e também como abrir um novo Form,
e ainda como abrir imagens .png na tela do programa.







//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1( TComponent* Owner )
: TForm ( Owner )
{
pngPicture = new TPngImage;
mnuMain = new TMainMenu ( this );
mnuFile = new TMenuItem ( mnuMain );

mnuFileNew = new TMenuItem ( mnuFile );
mnuFileExit = new TMenuItem ( mnuFile );

mnuFile -> Caption = L"&Arq";
mnuFileNew -> Caption = L"&Form2";
mnuFileExit -> Caption = L"&Sair";

mnuFileNew -> OnClick = FileNewMenuClicked;
mnuFileExit -> OnClick = mnuFileExitClick;

mnuFile -> Add ( mnuFileNew );
mnuFile -> Add ( mnuFileExit );
mnuMain -> Items -> Add ( mnuFile );
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
Canvas -> Font -> Color = clBlack;
Canvas -> TextOut ( 200, 200, "Por: " );
Canvas -> Font -> Color = clRed;
Canvas -> TextOut ( 250, 200, "Samuel Lima" );
Canvas -> Font -> Name = "Garamond";
Canvas -> Font -> Size = 20;
Canvas -> Font -> Color = clSkyBlue;
Canvas -> TextOut ( 200, 235, "MUITO OBRIGADO" );
SetBkMode ( Canvas->Handle, TRANSPARENT );
Canvas -> Font -> Color = clBlue;
Canvas -> TextOut ( 200, 229, "MUITO OBRIGADO" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint ( TObject *Sender )
{
Canvas -> Font -> Size = 18;
Canvas -> Font -> Name = "Garamond";
Canvas -> Font -> Color = clRed;
Canvas -> Pen -> Width = 10;
Canvas -> Pen -> Color = RGB ( 0, 255, 0 );
Canvas -> Rectangle ( 05, 05, 595, 275 );
SetTextColor ( Canvas -> Handle, RGB ( 255, 0,0 ) );
Canvas -> TextOut ( 80, 18, "CRIANDO E ABRINDO UM NOVO FORM" );
pngPicture->LoadFromFile("C:\\Users\\Embarcadero\\Documents\\"
"\\Projetos C++ Builder\\OnPaint\\Abrindo imagem com canvas\\10.2.2.png");
Canvas -> Draw ( 130, 40, pngPicture );
//delete BmpMercedes;
Informe ( Sender );
}

//---------------------------------------------------------------------------
void __fastcall TForm1::FileNewMenuClicked ( TObject *Sender )
{
Form1 -> Hide ( );
Form2 -> Show();
}

//---------------------------------------------------------------------------
void __fastcall TForm1::mnuFileExitClick ( TObject *Sender )
{
exit ( 0 );
}


//---------------------------------------------------------------------------



#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.Menus.hpp>
#include <pngimage.hpp>
//---------------------------------------------------------------------------
enum PictureType { Bitmap, JPEG, GIF, PNG };
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall OnPaint(TObject *Sender);

private: // User declarations
public: // User declarations    TMainMenu *mnuMain;
TPngImage         * pngPicture;
  TMainMenu *mnuMain;
TMenuItem *mnuFile;
TMenuItem *mnuFileNew;
TMenuItem *mnuFileExit;

void __fastcall FileNewMenuClicked ( TObject *Sender );
void __fastcall mnuFileExitClick ( TObject *Sender );
    void __fastcall Informe ( TObject *Sender );
__fastcall TForm1( TComponent* Owner );
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 342
  Top = 165
  Caption = 'Form1'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = OnPaint
  PixelsPerInch = 96
  TextHeight = 13
end

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
pngPicture = new TPngImage;
mnuMain = new TMainMenu ( this );
mnuFile = new TMenuItem ( mnuMain );

mnuFileNew = new TMenuItem ( mnuFile );
mnuFileExit = new TMenuItem ( mnuFile );

mnuFile -> Caption = L"&Arq";
mnuFileNew -> Caption = L"&Form1";
mnuFileExit -> Caption = L"&Sair";

mnuFileNew -> OnClick = FileNewMenuClicked;
mnuFileExit -> OnClick = mnuFileExitClick;

mnuFile -> Add ( mnuFileNew );
mnuFile -> Add ( mnuFileExit );
mnuMain -> Items -> Add ( mnuFile );
}

 void __fastcall TForm2::OnPaint(TObject *Sender)
{
Canvas -> Font -> Size = 18;
Canvas -> Font->Name = "Garamond";
Canvas -> Pen -> Width = 10;
Canvas -> Pen -> Color = clRed;
Canvas -> Rectangle ( 05, 05, 595, 275 );
Canvas -> Font -> Color = clRed;
Canvas -> TextOut ( 80, 10, "CRIANDO E ABRINDO UM NOVO FORM" );
    pngPicture->LoadFromFile("C:\\Users\\Embarcadero\\Documents\\"
"\\Projetos C++ Builder\\OnPaint\\Abrindo imagem com canvas\\CX_1.png");
Canvas -> Draw ( 180, 30, pngPicture );
//delete BmpMercedes;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FileNewMenuClicked ( TObject *Sender )
{
Form2 -> Hide ( );
Form1 -> Show ( );
}

//---------------------------------------------------------------------------
void __fastcall TForm2::mnuFileExitClick ( TObject *Sender )
{
exit ( 0 );
}

//---------------------------------------------------------------------------


//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <pngimage.hpp>
//---------------------------------------------------------------------------

class TForm2 : public TForm
{
__published: // IDE-managed Components
void __fastcall OnPaint(TObject *Sender);
private: // User declarations
TPngImage         * pngPicture;
TMainMenu *mnuMain;
TMenuItem *mnuFile;
TMenuItem *mnuFileNew;
TMenuItem *mnuFileExit;

void __fastcall FormCreate ( TObject *Sender );
void __fastcall FileNewMenuClicked ( TObject *Sender );
void __fastcall mnuFileExitClick ( TObject *Sender );

public: // User declarations
__fastcall TForm2 ( TComponent* Owner );
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif


object Form2: TForm2
  Left = 342
  Top = 165
  Caption = 'Form2'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = OnPaint
  PixelsPerInch = 96
  TextHeight = 13
end

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.