Páginas

segunda-feira, 24 de novembro de 2014

Criando botões em C++!!!

Bom pessoal, como prometido, venho por meio deste tutorial mostrar a vocês como pode ser criado um botão em linguagem de programação c++. O exemplo que darei, será bem simples e prático, pois criarei "2" dois botões básicos para ilustração. Estes serão apenas para demonstrar o quão fácil é
fazer essa tarefa na linguagem c++, lembrando que esse tópico está destinado somente para o ambiente Windows.



Colocarei abaixo o código e explicarei algumas partes dele, porque já expliquei no primeiro post várias funções da linguagem. Por isso não entrarei em detalhes, aconselho quem ainda não viu o primeiro tutorial, que veja, para entender melhor. Então, vamos lá:

#include <windows.h>  

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);  
char szClassName[ ] = "WindowsApp";  
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)  
 {  
   HWND hwnd;         
   MSG messages;  
   WNDCLASSEX wincl;      
   wincl.hInstance = hThisInstance;  
   wincl.lpszClassName =  
   szClassName;  
   wincl.lpfnWndProc =  
   WindowProcedure;     
   wincl.style = CS_DBLCLKS;  
   wincl.cbSize = sizeof  
   (WNDCLASSEX);  
   wincl.hIcon = LoadIcon (NULL,  
   IDI_APPLICATION);  
   wincl.hIconSm = LoadIcon (NULL,  
   IDI_APPLICATION);  
   wincl.hCursor = LoadCursor  
   (NULL, IDC_ARROW);  
   wincl.lpszMenuName = NULL;   
   wincl.cbClsExtra = 0;             
   wincl.cbWndExtra = 0;             
   wincl.hbrBackground = (HBRUSH)  
   COLOR_BACKGROUND;  
   if (!RegisterClassEx (&wincl))  
   return 0;  
   hwnd = CreateWindowEx (  
      0,           
      szClassName,               
      "Botões",              
      WS_OVERLAPPEDWINDOW,  
      540,                         
      230,                        
      280,                        
      180,                        
      HWND_DESKTOP,           
      NULL,                     
      hThisInstance,  
      NULL                    
      );  
   ShowWindow (hwnd, nFunsterStil);  
   while (GetMessage (&messages, NULL, 0, 0))  
   {  
     TranslateMessage(&messages);  
     DispatchMessage(&messages);  
   }  
   return messages.wParam;  
 }  
HWND Teste;  
HWND Sair;  
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)  
 {  
   switch (message)           
   {  
     case WM_CREATE:  
       {  
     Teste = CreateWindowEx (  
       0,  
       "BUTTON"  
       "TESTE"  
       WS_CHILD | WS_BORDER,  
       110,  
       30,  
       50,  
       35,  
       hwnd,  
       NULL,  
       g_inst,  
       NULL,  
       );  

     Sair = CreateWindowEx (  
       0,  
       "BUTTON"  
       "SAIR"  
       WS_CHILD | WS_BORDER,  
       110,  
        75,  
        50,  
        35,  
        hwnd,  
        NULL,  
        g_inst,  
        NULL,  
        );  

     case WM_DESTROY:  
       PostQuitMessage (0);  
       break;  
     default:             
       return DefWindowProc (hwnd, message, wParam, lParam);  
   }  
   return 0;  
 }  

Agora vamos, para as explicações:
HWND Teste; (Esta função serve como identificador para o nosso botão criado, "Teste").
HWND Sair; (Esta função serve como identificador para o nosso botão criado, "Sair").
Sempre que você adicionar WM_CREATE o aplicativo envia uma mensagem solicitando que uma janela seja criada, chamando a função CreateWindowEx ou CreateWindow. a classe "BUTTON" nos "2" dois botões criados, serve para definir o tipo de controle.
Terminamos aqui mais um tuto, espero que tenham gostado, qualquer dúvida deixem nos comentários. valew galera e até a próxima!!!

7 comentários:

  1. tentei utilizar o código, mas não funcionou... tentei corrigir os erros que apareceram, mas também não consegui

    ResponderExcluir
  2. Qual Ambiente de desenvolvimento está usando? O Dev C++? Se for, instalou todas as bibliotecas necessárias? Quais os erros? Lembre-se de registrar a classe na criação da janela: if (!RegisterClassEx (&wincl)). E identificar o HWND!!!

    ResponderExcluir
  3. não funcionou não amigo. To usando o codeblocks.

    ResponderExcluir
  4. #include

    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

    HWND Teste;
    HWND Sair;

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow){
    MSG msg;
    HWND hwnd;
    WNDCLASSW wc;

    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.lpszClassName = L"Janela_Main";
    wc.hInstance = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpszMenuName = NULL;
    wc.lpfnWndProc = WndProc;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassW(&wc);
    hwnd = CreateWindowW(L"Janela_Main", L"Teste", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 800, 600, NULL, NULL, hInstance, NULL);
    Teste = CreateWindowEx(0, L"BUTTON", L"TESTE", WS_CHILD | WS_BORDER, 110, 30, 50, 35, hwnd, NULL, hInstance, NULL);
    Sair = CreateWindowEx(0, L"BUTTON", L"SAIR", WS_CHILD | WS_BORDER, 110, 75, 50, 35, hwnd, NULL, hInstance, NULL);

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    ShowWindow(Teste, nCmdShow);
    UpdateWindow(Teste);
    ShowWindow(Sair, nCmdShow);
    UpdateWindow(Sair);

    while (GetMessage(&msg, NULL, 0, 0)) {
    DispatchMessage(&msg);
    }

    return (int)msg.wParam;
    }

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
    switch (msg)
    {
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
    }

    ResponderExcluir
  5. The Eight-Wheel Classic - TITIAN Arts
    The eight-wheel classic bicycle is available in six sizes. poormansguidetocasinogambling.com The casino-roll.com Bicycle Wheel is a classic https://access777.com/ bicycle made in USA, titanium flat iron but there are three variations https://vannienailor4166blog.blogspot.com/ in

    ResponderExcluir