domingo, 27 de fevereiro de 2022

C++ Builder - estilos e funções nativas do win32


O programador que utiliza o C++ Builder,
e que possui pelo menos conhecimentos
intermediário nesta excelente IDE,
está repleto de opções para a criação
de interfaces modernas e de recursos avançados,
exibindo estilos profissionais em seus programas.
Como meus programas são em sua maioria programas
didáticos nunca me preocupei de incorporar
estilos na interface gráfica.
O C++ Builder possui estilos de interfaces
prontos para serem inseridos por seus usuários,
mas se isto não for suficiente, existem empresas
que criam pacotes de interfaces para aplicações
em programas em Delph ou C++, e com preços interessantes.
Mas o programador pode criar suas próprias interfaces
personalizadas, e o único limite nisto tudo só depende
do nível de conhecimento próprio.
As interfaces personalizadas são criadas e salvas
com a extensão .vsf, e pode ser editadas ou reutilizadas por
outros programas quando for necessário.
Um outro recurso importante do C++ Builder
é a possibilidade de utilização das funções nativas
do Win32, e suporte completo das bibliotecas gráfica gdiplus.
Para demonstrar um dos vários estilos prontos do C++ Builder,
e para utilizar algumas funções do Win32, criei este programa,
que faz cálculo do fatorial dos dez primeiros números naturais.
O programa calcula os fatoriais e armazena num vetor 
de estilo C de dez posições.



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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
#include <gdiplusheaders.h>
#include <gdipluseffects.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
#include <Gdiplus.h>

#include <sstream>
#include <iostream>
#include <iomanip>

using namespace std;

using std::setw;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
// ---------------------------------------------------------------------------
void Label_Manual ( ) {

	Form1 -> Label1 -> Font->Size = 12;
	Form1 -> Label1 -> Font->Name = "Arial";
	Form1 -> Label1 -> Font->Color = clBlue;
	//Form1 -> Label1 -> Width = 30;
	//Form1 -> Label1 -> Height = 20;
	Form1 -> Label1 -> Left = 50;
	Form1 -> Label1 -> Top = 40;

	Form1 -> Label2 -> Font->Size = 12;
	Form1 -> Label2 -> Font->Name = "alarm clock";
	Form1 -> Label2 -> Font->Color = clBlack;
	//Form1 -> Label2 -> Width = 30;
	//Form1 -> Label2 -> Height = 20;
	Form1 -> Label2 -> Left = 105;
	Form1 -> Label2 -> Top = 65;

	Form1 -> Label3 -> Font->Size = 12;
	Form1 -> Label3 -> Font->Name = "alarm clock";
	Form1 -> Label3 -> Font->Color = clRed;
	//Form1 -> Label2 -> Width = 30;
	//Form1 -> Label2 -> Height = 20;
	Form1 -> Label3 -> Left = 105;
	Form1 -> Label3 -> Top = 65;

	Form1 -> BitBtn1 -> Font -> Size = 12;
	Form1 -> BitBtn1 -> Font -> Color = clBlue;
	Form1 -> BitBtn1 -> Font -> Style = TFontStyles ( ) << fsItalic;
	Form1 -> BitBtn1 -> Top = 260;
	Form1 -> BitBtn1 -> Left = 150;
	Form1 -> BitBtn1 -> Height = 23;
	Form1 -> BitBtn1 -> Width = 50;
	Form1 -> BitBtn1 -> Caption = "Sair";
}
 //---------------------------------------------------------------------------
VOID Moldura ( HDC hdc, int left, int top, int right, int bottom,
	 int bewel, int a, int r, int g, int b ) {

	Gdiplus::Graphics graphics(hdc);

	Gdiplus::Graphics* myGraphics;
	GraphicsPath myGraphicsPath;

	Pen penJoin1 ( Color ( a, r, g, b ), bewel );
	penJoin1.SetLineJoin ( LineJoinBevel );

	graphics.DrawRectangle ( &penJoin1, Gdiplus::Rect ( left, top, right, bottom ) );
	graphics.DrawPath ( &penJoin1, &myGraphicsPath );
}
//----------------------------------------------------------------------------
VOID Informe_2 ( HDC hdc, int x, int y, int a_1, int a_2, int a_3, int a_4,
	int b_1, int b_2, int b_3, int b_4, int c_1, int c_2, int c_3, int c_4,
	LPCWSTR font, FontStyle style, int fontTam ) {

	Gdiplus::Graphics graphics(hdc);

	SolidBrush solidBrush_1 ( Color ( a_1, a_2, a_3, a_4 ) );
	SolidBrush solidBrush_2 ( Color ( b_1, b_2, b_3, b_4 ) );
	SolidBrush solidBrush_3 ( Color ( c_1, c_2, c_3, c_4 ) );

	FontFamily fontFamily ( font );
	Font font1 ( &fontFamily, fontTam, style, UnitPixel );

	Gdiplus::PointF pointF ( x, y );

	const WCHAR* string_1 = L"Por: ";
	const WCHAR* string_2 = L"Samuel Lima";
	const WCHAR* string_3 = L"sa_sp10@hotmail.com";

	graphics.DrawString ( string_1, 4, &font1, Gdiplus::PointF ( x, y ), &solidBrush_1 );
	graphics.DrawString ( string_2, 12, &font1, Gdiplus::PointF ( x + 40, y ), &solidBrush_2 );
	graphics.DrawString ( string_3, 20, &font1, Gdiplus::PointF ( x, y + 18 ), &solidBrush_3 );
}
 //---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint ( TObject *Sender ) {

	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR  gdiplusToken;
	// Inicializando GDI+.
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	HDC hdc = Canvas -> Handle;

	static HFONT hFont;

		 HFONT holdFont;
		 HFONT m_newFont;

		 hFont = CreateFont (
	18,                       // nHeight
	8,                        // nWidth
	0,                        // nEscapement
	0,                        // nOrientation
	FW_NORMAL,                // nWeight
	TRUE,                    // bItalic
	FALSE,                    // bUnderline
	0,                        // cStrikeOut
	ANSI_CHARSET,             // nCharSet
	OUT_DEFAULT_PRECIS,       // nOutPrecision
	CLIP_DEFAULT_PRECIS,      // nClipPrecision
	DEFAULT_QUALITY,          // nQuality
	DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
	_T ( "Times New Roman" ) );
				m_newFont = CreateFont ( 80, 0, 0, 0,
        FW_THIN, FALSE, TRUE, FALSE,
        ANSI_CHARSET, OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_DONTCARE, _T ( "Times" ) );

		SelectObject ( hdc, hFont );
		DeleteObject ( hFont );

	SetTextColor ( hdc, RGB ( 255, 0, 0 ) );
	SetBkColor ( hdc, RGB ( 0, 0, 0 ) );
	TextOutA ( hdc, 100, 10, "C++ BUILDER - ARMAZENANDO FATORIAL EM VETOR", 43 );

	Moldura ( hdc, 5, 5, 590, 290, 10, 255, 0, 0, 255 );

		Informe_2 (
		hdc, 230, 250,
		255, 255, 0, 0,
		255, 0, 0, 255,
		255, 0, 255, 0,
		L"Times New Roman",
		FontStyleItalic, 14 );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
	Label_Manual ( );

	string str_1;
	string str_2;
	string str_3;

	const  char *Conv_1;
	const  char *Conv_2;
	const  char *Conv_3;

	int vet_fat [ 10 ];

	int val, fatorial = 1;

	stringstream sst;
	sst << setw ( 29 ) << "Posição" << setw ( 29 ) << "Fatorial" << endl;
	str_1 += sst.str ( );

	//Convertendo std::string em const  char
	Conv_1 = str_1.c_str ( );
	Label1 -> Caption += ( Conv_1 );

	for ( int i = 1; i <= 10; i++ ) {
		//stringstream deve ser declarado aqui
		stringstream sst_1;
		stringstream sst_2;

		val = i;
		if ( val > 1 ) {
			fatorial = fatorial * val;
			val--;
		}

		vet_fat [ i ] =  fatorial;
		sst_1 << setw ( 7 ) << i << + "°" << endl;
		str_2 += sst_1.str ( );

		//Convertendo std::string em const  char
		Conv_2 = str_2.c_str ( );
		Label2 -> Caption = ( Conv_2 );

		sst_2 << setw ( 23 ) << vet_fat [ i ] << endl;
		str_3 += sst_2.str ( );

		//Convertendo std::string em const  char
		Conv_3 = str_3.c_str ( );
	}

	Label3 -> Caption += ( Conv_3 );

}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click ( TObject *Sender ) {
			   Close ( );
}
//---------------------------------------------------------------------------