quarta-feira, 22 de setembro de 2021

C# - rodando dll criada em C++ padrão


Peguei um de meus códigos antigos,
à saber, um relógio digital criado
com a biblioteca graphics.h,
e encapsulei-o numa dll utilizando
uma linha de comando com o g++ do MinGw,
chamando as libs necessárias para o 
funcionamento da biblioteca graphics.h.
E tudo foi realizado como esperava,
mas o que realmente me surpreendeu
foi a facilidade de chamar este código,
não gerenciado num aplicativo de console
feito no C#, isto mesmo, não estamos
escrevendo besteiras e podemos provar
para quem duvidar, estamos rodando
uma dll criada em linguagem c++,
contendo um código que utiliza a antiga
biblioteca graphics.h, num projeto
de console do C#.
Abaixo vou postar o código que utilizei
no C#, e no link, qualquer interessado
pode ter acesso ao código do relógio
digital em C, utilizando a biblioteca graphics.h, 
mas antes veja o relógio funcionando:





using System;
using System.Runtime.InteropServices;

class PlatformInvokeTest {

    [DllImport ( "gera_dll.dll" )]

    public static extern int main ( );

    public static void Main ( ) {
        main (  );
    }
}

domingo, 27 de junho de 2021

C# - pesquisando e marcando vogais


Este programinha marca todas
as ocorrências das vogais 
encontrada numa matriz 2D de string.
É indicado a iniciantes, e não faz
uso de nenhum método de pesquisa do C#.
Quer saber como fiz, clique no link
agora mesmo, mas antes assista o vídeo.




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Textbox_to_Array {
    public partial class Form1 : Form {
        string [ ] texto = {
         "Em cismar, sozinho, à noite, ",
         "Mais prazer encontro eu lá;  ",
         "Minha terra tem palmeiras,   ",
         "Onde canta o Sabiá.          ",
         "Minha terra tem primores,    ",
         "Que tais não encontro eu cá; ",
         "Em cismar — sozinho, à noite ",
         "Mais prazer encontro eu lá;  ",
         "Minha terra tem palmeiras,   ",
         "Onde canta o Sabiá.          "};
        /*============================================================*/
        void Informe ( PaintEventArgs e ) {
            Graphics dc = e.Graphics;
            Font myFont = new System.Drawing.Font ( "Helvetica"11FontStyle.Italic );
            Brush myBrush_1 = new SolidBrush ( System.Drawing.Color.Red );
            Brush myBrush_2 = new SolidBrush ( System.Drawing.Color.Blue );
            Brush myBrush_3 = new SolidBrush ( System.Drawing.Color.Black );
            dc.DrawString ( "Por: "myFontmyBrush_1350250 );
            dc.DrawString ( "Samuel Lima "myFontmyBrush_2380250 );
            dc.DrawString ( "sa_sp10@hotmail.com "myFontmyBrush_3350265 );
            //Imprime_Array ( e );
        }
        /*============================================================*/
        void Label_1 ( int Xint Yint Wint Hstring st ) {
            label1.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
            9.75FSystem.Drawing.FontStyle.Italic,
            System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            label1.ForeColor = System.Drawing.Color.Black;
            label1.Location = new System.Drawing.Point ( XY );
            label1.Name = "label1";
            label1.Size = new System.Drawing.Size ( WH );
            label1.TabIndex = 2;
            label1.Text = st;
        }
        /*============================================================*/
        void TextBox_1 ( int Xint Yint Wint H ) {
            textBox1.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
            8FSystem.Drawing.FontStyle.Italic,
            System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            textBox1.ForeColor = System.Drawing.Color.Black;
            textBox1.Location = new System.Drawing.Point ( XY );
            textBox1.Name = "textBox1";
            textBox1.Size = new System.Drawing.Size ( WH );
            textBox1.TabIndex = 3;
        }
        /*============================================================*/
        public void Button_1 ( int Xint Yint Wint Hstring st ) {
            textBox1.Focus ( );
            button1.Location = new Point ( XY );
            this.button1.Size = new System.Drawing.Size ( WH );
            button1.Font = new Font ( "Times New Roman"10F,
             System.Drawing.FontStyle.ItalicSystem.Drawing.GraphicsUnit.Point0 );
            button1.BackColor = Color.Pink;
            button1.ForeColor = Color.FromArgb ( 000 );
            this.button1.Text = st;
        }
        /*============================================================*/
        public void Button_2 ( int Xint Yint Wint Hstring st ) {
            button2.Location = new Point ( XY );
            this.button2.Size = new System.Drawing.Size ( WH );
            button2.Font = new Font ( "Times New Roman"13F,
             System.Drawing.FontStyle.ItalicSystem.Drawing.GraphicsUnit.Point0 );
            button2.BackColor = Color.LightGreen;
            button2.ForeColor = Color.FromArgb ( 000 );
            this.button2.Text = st;
        }
        /*============================================================*/
        private void Moldura ( PaintEventArgs e ) {
            System.Drawing.Graphics dc;
            dc = this.CreateGraphics ( );
            Pen Red = new Pen ( Brushes.Red );
            Red.Width = 10.0F;
            Red.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
            dc.DrawRectangle ( Rednew Rectangle ( 55590285 ) );
            Red.Dispose ( );
        }
        /*============================================================*/
        protected override void OnPaint ( PaintEventArgs e ) {
            Moldura ( e );
            this.Size = new System.Drawing.Size ( 615335 );
            this.Text = "C# - PESQUISANDO E MARCANDO VOGAIS";
            this.BackColor = Color.White;
            Graphics dc = e.Graphics;
            Font myFont = new System.Drawing.Font ( "Consolas"12,
                FontStyle.Italic );
            Brush myBrush_1 = new SolidBrush ( Color.Red );
            dc.DrawString ( "C# - PESQUISANDO E MARCANDO VOGAIS",
                myFontmyBrush_115010 );
            Imprime_Array ( e );
            Informe ( e );
        }
        /*============================================================*/
        public Form1 ( ) {
            InitializeComponent ( );
        }
        /*============================================================*/

        private void Imprime_Array ( PaintEventArgs e ) {
            Graphics dc = e.Graphics;
            Font myFont = new System.Drawing.Font ( "Consolas"11,
                FontStyle.Italic );
            Brush myBrush_1 = new SolidBrush ( Color.Blue );

            for ( int i = 0i < 10i++ ) {
                for ( int j = 0j < 28j++ ) {
                    dc.DrawString ( "" + texto [ i ] [ j ], myFont,
                    myBrush_190 + ( j * 15 ), 43 + ( i * 17 ) );
                }
            }
        }

        /*============================================================*/
        private void Button1_Click ( object senderEventArgs e ) {
            System.Drawing.Graphics dc;
            dc = this.CreateGraphics ( );
            Font myFont = new System.Drawing.Font ( "Consolas"10,
                FontStyle.Italic );
            Brush myBrush_1 = new SolidBrush ( Color.Black );

            char [ ] busca;
            string str_2 = textBox1.Text;

            
        }
        /*============================================================*/
        private void Form1_Shown ( object senderEventArgs e ) {
            TextBox_1 ( 19024510010 );
            Label_1 ( 702458080"Digite uma vogal: " );
            Button_1 ( 3002453522"Ok" );
            Button_2 ( 5202605025"Sair" );
        }
        /*============================================================*/
        private void button2_Click ( object senderEventArgs e ) {
            Application.Exit ( );
        }
        /*============================================================*/
    }
}

terça-feira, 22 de junho de 2021

C# - pesquisa de string em matriz 2D highlighter


Para representar dinamicamente um painel
com conteúdo composto por linhas e colunas,
O controle TableLayoutPanel é perfeito para isto.
O conteúdo de um TableLayoutPanel é organizado em uma grade,
e o seu layout é executado em tempo de design ou de execução.
Para criar este programa, arrastamos para o Form,
quatro controles, e adicionamos eventos para o form
e para o botão, e acoplamos no TableLayoutPanel
um label de duas dimensões.
O programa permite uma precisa pesquisa,
com destaque em highlighter, numa matriz
bidimensional de string, que é imprimida
pelo label criado manualmente.
Como a matriz é de 10 x 5, o label criado
também é, e evidentemente o TableLayoutPanel
possui as mesmas características para
linhas e colunas.
Este programa teve sua versão original
criada por nós mesmo no C++ Builder,
e achei muito justo recriá-lo em C#.
Poderíamos por exemplo, fazer subir
um arquivo com centenas de números
em extenso, para ser carregado
por uma lista, e aplicado para a impressão
no TableLayoutPanel, com sua propriedade
AutoScroll definida como verdadeiro;
Mas o programa é didático, e não há
a menor necessidade disto.
Confira no vídeo seu perfeito funcionamento.





        /*============================================================*/
        protected override void OnPaint ( PaintEventArgs e ) {
            Paint_Imagem ( e );
            this.Size = new System.Drawing.Size ( 615337 );
            this.Text = "C# - PESQUISA STRING EM MATRIZ 2D HIGHLIGHTER";
            this.BackColor = Color.White;
            Graphics dc = e.Graphics;
            Font myFont = new System.Drawing.Font ( "Consolas"12FontStyle.Italic );
            Brush myBrush_1 = new SolidBrush ( Color.Red );
            dc.DrawString ( "C# - PESQUISA STRING EM MATRIZ 2D HIGHLIGHTER",
                myFontmyBrush_110015 );
        }
        /*============================================================*/
        void Label_1 ( int Xint Yint Wint Hstring st ) {
            this.label1.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
               9.75FSystem.Drawing.FontStyle.ItalicSystem.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            this.label1.ForeColor = System.Drawing.Color.Blue;
            this.label1.Location = new System.Drawing.Point ( XY );
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size ( WH );
            this.label1.TabIndex = 3;
            this.label1.Text += st;
        }
        /*============================================================*/
        void TableLayoutPanel1_1 ( int Xint Yint Wint Hint Lint C ) {
            this.tableLayoutPanel1.AutoScroll = true;
            this.tableLayoutPanel1.ColumnCount = C;
            this.tableLayoutPanel1.ColumnStyles.Add ( new System.Windows.Forms.ColumnStyle ( System.Windows.Forms.SizeType.Percent30F ) );
            this.tableLayoutPanel1.ColumnStyles.Add ( new System.Windows.Forms.ColumnStyle ( System.Windows.Forms.SizeType.Percent30F ) );
            this.tableLayoutPanel1.Location = new System.Drawing.Point ( XY );
            this.tableLayoutPanel1.AutoSize = true;
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = L;
            this.tableLayoutPanel1.RowStyles.Add ( new System.Windows.Forms.RowStyle ( System.Windows.Forms.SizeType.Percent30F ) );
            this.tableLayoutPanel1.RowStyles.Add ( new System.Windows.Forms.RowStyle ( System.Windows.Forms.SizeType.Percent30F ) );
            this.tableLayoutPanel1.Size = new System.Drawing.Size ( WH );
            this.tableLayoutPanel1.TabIndex = 4;
        }
        /*============================================================*/

C# - Jagged array de strings ( arrays irregulares )

 Postando exemplos práticos:


//Inicializando array jagged bidimensional de strings
using System;
using System.Collections;

class Arr {
    /*============================================================*/
    void Informe ( ) {
        Console.BackgroundColor = ConsoleColor.White;
        Console.SetCursorPosition ( 2016 );
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write ( "Por: " );
        Console.SetCursorPosition ( 2516 );
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.Write ( "Samuel Lima" );
        Console.SetCursorPosition ( 2017 );
        Console.ForegroundColor = ConsoleColor.Black;
        Console.Write ( "sa_sp10@hotmail.com" );
        Console.SetCursorPosition ( 3019 );
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write ( "MUITO OBRIGADO" );
    }
    /*============================================================*/
    void Moldura ( int tam_lin_iniint tam_lin_fimint tam_ini_col,
        int tam_fim_col ) {
        int ic;
        for ( i = tam_lin_inii < tam_lin_fimi++ ) {
            for ( c = tam_ini_colc < tam_fim_colc++ ) {
                Console.SetCursorPosition ( ci );
                Console.BackgroundColor = ConsoleColor.White;
                Console.Write ( " " );
            }
        }
    }
    /*============================================================*/
    void Ini_Array ( ) {
        int ij;
        //Inicializando array jagged bidimensional de strings
        /*
        //Assim abaixo, cada palavra ocupa uma coluna na matriz
        string [ ] [ ] texto = new string [ 12 ] [ ];
        texto [ 0 ] = new string [ 4 ] { "No", "Meio", "do", "Caminho" };
        texto [ 1 ] = new string [ 4 ] { "Carlos", "Drummond", "de", "Andrade" };
        texto [ 2 ] = new string [ 7 ] { "No", "meio", "do", "caminho", "tinha", "uma", "pedra" };
        texto [ 3 ] = new string [ 7 ] { "Tinha", "uma", "pedra", "no", "meio", "do", "caminho" };
        texto [ 4 ] = new string [ 3 ] { "Tinha", "uma", "pedra" };
        texto [ 5 ] = new string [ 7 ] { "No", "meio", "do", "caminho", "tinha", "uma", "pedra." };
        texto [ 6 ] = new string [ 5 ] { "Nunca", "me", "esquecerei", "desse", "acontecimento" };
        texto [ 7 ] = new string [ 7 ] { "Na", "vida", "de", "minhas", "retinas", "tao", "fatigadas." };
        texto [ 8 ] = new string [ 8 ] { "Nunca", "me", "esquecerei", "que", "no", "meio", "do", "caminho"};
        texto [ 9 ] = new string [ 3 ] { "Tinha", "uma", "pedra" };
        texto[ 10 ] = new string [ 7 ] { "Tinha", "uma", "pedra", "no", "meio", "do", "caminho" };
        texto[ 11 ] = new string [ 7 ] {"No", "meio", "do", "caminho", "tinha", "uma", "pedra."};
       
         */
        //Também pode ser inicialiado assim abaixo:
        //Assim abaixo, cada linha ocupa uma coluna na matriz
        /*
        string [ ] [ ] texto = {
        new string [ ] {"No Meio do Caminho                         "},
        new string [ ] {"Carlos Drummond de Andrade                 "},
        new string [ ] {"No meio do caminho tinha uma pedra         "},
        new string [ ] {"Tinha uma pedra no meio do caminho         "},
        new string [ ] {"Tinha uma pedra                            "},
        new string [ ] {"No meio do caminho tinha uma pedra.        "},
        new string [ ] {"Nunca me esquecerei desse acontecimento    "},
        new string [ ] {"Na vida de minhas retinas tao fatigadas.   "},
        new string [ ] {"Nunca me esquecerei que no meio do caminho "},
        new string [ ] {"Tinha uma pedra                            "},
        new string [ ] {"Tinha uma pedra no meio do caminho         "},
        new string [ ] {"No meio do caminho tinha uma pedra.        "}};
         */

        //Assim abaixo, cada linha ocupa uma coluna na matriz
        string [ ] [ ] texto = new string [ 12 ] [ ];
        texto [ 0 ] = new string [ 1 ] {"No Meio do Caminho                         "};
        texto [ 1 ] = new string [ 1 ] { "Carlos Drummond de Andrade                 " };
        texto [ 2 ] = new string [ 1 ] { "No meio do caminho tinha uma pedra         " };
        texto [ 3 ] = new string [ 1 ] { "Tinha uma pedra no meio do caminho         " };
        texto [ 4 ] = new string [ 1 ] { "Tinha uma pedra                            " };
        texto [ 5 ] = new string [ 1 ] { "No meio do caminho tinha uma pedra.  xx    " };
        texto [ 6 ] = new string [ 1 ] { "Nunca me esquecerei desse acontecimento    " };
        texto [ 7 ] = new string [ 1 ] { "Na vida de minhas retinas tao fatigadas.   " };
        texto [ 8 ] = new string [ 1 ] { "Nunca me esquecerei que no meio do caminho " };
        texto [ 9 ] = new string [ 1 ] { "Tinha uma pedra                            " };
        texto10 ] = new string [ 1 ] { "Tinha uma pedra no meio do caminho         " };
        texto11 ] = new string [ 1 ] { "No meio do caminho tinha uma pedra.        " };

        //Posiciona os textos no console
        Console.SetCursorPosition ( 214 );
        Console.ForegroundColor = ConsoleColor.Blue;
        for ( i = 0i < texto.Lengthi++ ) {
            for ( j = 0j < texto [ i ].Lengthj++ ) {
                Console.Write ( texto [ i ] [ j ] + " " );
            }
            Console.SetCursorPosition ( 21i + 5 );
        }
        Console.ReadKey ( );
        Console.WriteLine ( );
        Console.SetCursorPosition ( 2117 );
        Console.Write ( texto [ 5 ] [ 0 ] );//do
        Console.ReadKey ( );
    }
    /*============================================================*/
    public static void Main ( ) {
        Console.Title = ( "JAGGED ARRAY DE STRING" );
        Arr array = new Arr ( );
        array.Moldura ( 120268 );
        Console.SetCursorPosition ( 282 );
        Console.BackgroundColor = ConsoleColor.White;
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write ( "JAGGED ARRAY DE STRING" );
        //array.Informe ( );
        array.Ini_Array ( );
        //Segura a execução do programa
        Console.ReadKey ( );
    }
}
/*============================================================*/

sexta-feira, 18 de junho de 2021

C# - Marcando ocorrências de múltiplos


A primeira versão deste programa,
foi criada em C++ com o Qt,
e me lembro que me deu bastante trabalho,
porque não é simplesmente pesquisar
os múltiplos, se fosse só isto seria muito fácil,
mas o que agente queria mesmo era marcar
as ocorrências usando um bitimap como fundo;
Depois de várias cabeçadas nós conseguimos,
e depois criemos outra versão no C++ Builder,
ainda em C++, e agora aproveitando um pouco
do tempo que nos sobrou fizemos o mesmo programa
em C# usando o VS Studio da microsoft. 
Veja no vídeo ele funcionando.




        /*============================================================*/
        void Informe ( PaintEventArgs e ) {
            Graphics dc = e.Graphics;
            Font myFont = new System.Drawing.Font ( "Helvetica"11,
                FontStyle.Italic );
            Brush myBrush_1 = new SolidBrush ( System.Drawing.Color.Red );
            Brush myBrush_2 = new SolidBrush ( System.Drawing.Color.Blue );
            Brush myBrush_3 = new SolidBrush ( System.Drawing.Color.Black );
            dc.DrawString ( "Por: "myFontmyBrush_1350300 );
            dc.DrawString ( "Samuel Lima "myFontmyBrush_2380300 );
            dc.DrawString ( "sa_sp10@hotmail.com "myFontmyBrush_3350315 );
        }
        /*============================================================*/
        private void Paint_Imagem ( PaintEventArgs e ) {
            Image newImage = Image.FromFile ( "Moldura_7.bmp" );
            Point ulCorner = new Point ( 00 );
            e.Graphics.DrawImage ( newImageulCorner );
        }
        /*============================================================*/
        protected override void OnPaint ( PaintEventArgs e ) {
            Paint_Imagem ( e );
            this.Size = new System.Drawing.Size ( 615379 );
            this.Text = "C# - MÚLTIPLOS MARCANDO OCORRÊNCIAS";
            this.BackColor = Color.White;
            Graphics dc = e.Graphics;
            Font myFont = new System.Drawing.Font ( "Consolas"12,
                FontStyle.Italic );
            Brush myBrush_1 = new SolidBrush ( Color.Red );
            dc.DrawString ( "C# - MÚLTIPLOS MARCANDO OCORRÊNCIAS",
                myFontmyBrush_113015 );
            Informe ( e );
        }
        /*============================================================*/
        void FlowLayoutPanel_1 ( int Xint Yint Wint H ) {
            flowLayoutPanel1.AutoScroll = true;
            this.flowLayoutPanel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.flowLayoutPanel1.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
            8FSystem.Drawing.FontStyle.Italic,
            System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            this.flowLayoutPanel1.ForeColor = System.Drawing.Color.Blue;
            this.flowLayoutPanel1.Location = new System.Drawing.Point ( XY );
            this.flowLayoutPanel1.Name = "flowLayoutPanel1";
            this.flowLayoutPanel1.Size = new System.Drawing.Size ( WH );
            this.flowLayoutPanel1.TabIndex = 2;
        }
        /*============================================================*/
        void TextBox_1 ( int Xint Yint Wint H ) {
            //this.label1.AutoSize = true;
            textBox1.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
            8FSystem.Drawing.FontStyle.Italic,
            System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            textBox1.ForeColor = System.Drawing.Color.Blue;
            textBox1.Location = new System.Drawing.Point ( XY );
            textBox1.Name = "textBox1";
            textBox1.Size = new System.Drawing.Size ( WH );
            textBox1.TabIndex = 3;
        }
        /*============================================================*/
        void Label_1 ( int Xint Yint Wint Hstring st ) {
            //this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
9.75FSystem.Drawing.FontStyle.Italic
System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            this.label1.ForeColor = System.Drawing.Color.Blue;
            this.label1.Location = new System.Drawing.Point ( XY );
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size ( WH );
            this.label1.TabIndex = 3;
            this.label1.Text += st;
        }
        /*============================================================*/
        public Form1 ( ) {
            InitializeComponent ( );
        }
        /*============================================================*/
        public void Button_1 ( int Xint Yint Wint Hstring st ) {
            button1.Location = new Point ( XY );
            this.button1.Size = new System.Drawing.Size ( WH );
            button1.Font = new Font ( "Times New Roman"10F,
             System.Drawing.FontStyle.ItalicSystem.Drawing.GraphicsUnit.Point0 );
            button1.BackColor = Color.Pink;
            button1.ForeColor = Color.FromArgb ( 000 );
            this.button1.Text = st;
        }
        /*============================================================*/