Jump to content
Fórum Script Brasil
  • 0

Problema com char


BraulioChaves
 Share

Question

Olá pessoal, venho aqui pedir a ajuda de vocês em uma questão com uma variável do tipo char, é o seguinte, tenho um formulário win32 com um botão que quando você clica nesse botão é aberto outra janela, que contém um edit, um botão para fechar e voltar a janela principal, um botão pra pegar os dados do edit e gravar em um arquivo de texto comum, o problema é que quando eu gravo algo acontece como o esperado, mas se eu FECHAR a janela e voltar para a janela principal e clicar novamente no botão para abrir a segunda janela e for tentar gravar algo, não vai gravar os dados, é como se a variável não se limpasse, continuasse os dados antigos.. abaixo o code:

#include <windows.h>
#include <string>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#define className "Class"

LRESULT CALLBACK WinProc(HWND window,UINT msg, WPARAM WParam,LPARAM LParam);
HINSTANCE g_inst;
// Interagindo com o usuário
HWND Botao1;
    HWND hwnd;
HWND Botao2;
HWND EditNum1;
HWND option;
HFONT Minhaletra;
HWND BotaoFechar;
// Identificando os Botões
#define ID_BOTAO1  1001
#define IDC_BOTAO2  1003
#define IDC_BOTAOFECHAR 1004
// Sistema
char frase[100]="0";
using namespace std;


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR IpCmdLine,int nCmdShow){

    WNDCLASSEX win;
    MSG message;
    win.cbClsExtra = 0;
    win.cbSize=sizeof(win);
    win.cbWndExtra = 0;
    win.hbrBackground = CreateSolidBrush(RGB(217,83,83));
    win.hCursor = LoadCursor(NULL,IDC_ARROW);
    win.hIcon = LoadIcon(NULL,IDI_WARNING);
    win.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
    win.hInstance = hPrevInstance;
    win.lpfnWndProc = WinProc;
    win.lpszClassName = className;
    win.lpszMenuName = NULL;
    win.style = 0;
    if(!RegisterClassEx(&win)) return 0;

    hwnd = CreateWindowEx(0,className,"Formulário",WS_SIZEBOX | WS_TILED | WS_CAPTION | WS_SYSMENU,
                          300,200,700,430,NULL,NULL,hInstance,NULL);

                           option = CreateWindowEx(0,className,"a",WS_SIZEBOX | WS_TILED | WS_CAPTION,
                         450,350,350,200,0,0,hInstance,0);



    if(hwnd == NULL) return 0;
    ShowWindow(hwnd,SW_SHOW);
    UpdateWindow(hwnd);

    while(GetMessage(&message,0,0,0)){
        TranslateMessage(&message);
        DispatchMessage(&message);
    }
    return message.wParam;
}

LRESULT CALLBACK WinProc(HWND window,UINT msg, WPARAM wParam,LPARAM lParam)
{
    switch(msg){
        case WM_CREATE:

     Botao1 = CreateWindowEx(WS_EX_STATICEDGE,"BUTTON","Inverter",WS_VISIBLE | WS_CHILD,300,12,75,20,hwnd,(HMENU)ID_BOTAO1,g_inst,NULL);
        break;

        case WM_CLOSE:
             DestroyWindow(window);
        break;

        case WM_DESTROY:
             PostQuitMessage(0);
        break;

        case WM_COMMAND:
             switch(LOWORD(wParam)){
                 case ID_BOTAO1:{

               //       MessageBox(NULL,frase,frase,MB_OK);
         HINSTANCE g_inst1;
          EditNum1= CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_VISIBLE|WS_CHILD,12,142,320,20,option,NULL,g_inst1,NULL);
     Botao2 = CreateWindowEx(WS_EX_STATICEDGE,"BUTTON","Gravar",WS_VISIBLE | WS_CHILD,200,12,75,20,option,(HMENU)IDC_BOTAO2,g_inst1,NULL);

 BotaoFechar = CreateWindowEx(WS_EX_STATICEDGE,"BUTTON","FECHAR",WS_VISIBLE | WS_CHILD,100,12,75,20,option,(HMENU)IDC_BOTAOFECHAR,g_inst1,NULL);

                        ShowWindow(option, SW_SHOW);
                 }
                 break;
                 case IDC_BOTAOFECHAR:
                 {
                        ShowWindow(option, SW_HIDE);
                 }
                 break;
                 case IDC_BOTAO2:
                 {
                           SendMessage((HWND)EditNum1,(UINT)EM_GETLINE,(WPARAM)1,(LPARAM) &frase);
                            //         sprintf(frase,"[basico]\nplayername =%s\ndirpath =%s\nrompath =%s",);
                        ofstream myfile;
                        myfile.open ("xsnes.ini");
                        myfile << frase;
                        myfile.close();
                       MessageBox(NULL,frase,frase,MB_OK);
//
                        sprintf(frase,".");
                       // ShowWindow(option, SW_HIDE);

                 }
                 break;
             }
             break;

        default: return DefWindowProc(window, msg, wParam, lParam);
    }
    return 0;
}

Esse é só um code de teste que estou tentando resolver esse problema.

Obrigado desde já.

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Oi!

O problema é que você está criando o botão e o edit dentro da winproc. Depois você dá apenas um hide na janela se clicar no fechar. Quando manda abrir a janela de novo, você cria os controles novamente!

Assim fica um controle por cima do outro. Eo EditNum1 passa a apontar para o novo mas o que você digita o texto é o antigo!

Mova a criação dos objetos para o main direto como eu fiz aqui:

#include <windows.h>
#include <string>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#define className "Class"

LRESULT CALLBACK WinProc(HWND window,UINT msg, WPARAM WParam,LPARAM LParam);
HINSTANCE g_inst;
// Interagindo com o usuário
HWND Botao1;
    HWND hwnd;
HWND Botao2;
HWND EditNum1;
HWND option;
HFONT Minhaletra;
HWND BotaoFechar;
// Identificando os Botões
#define ID_BOTAO1  1001
#define IDC_BOTAO2  1003
#define IDC_BOTAOFECHAR 1004
// Sistema
char frase[100]="0";
using namespace std;


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR IpCmdLine,int nCmdShow){

    WNDCLASSEX win;
    MSG message;
    win.cbClsExtra = 0;
    win.cbSize=sizeof(win);
    win.cbWndExtra = 0;
    win.hbrBackground = CreateSolidBrush(RGB(217,83,83));
    win.hCursor = LoadCursor(NULL,IDC_ARROW);
    win.hIcon = LoadIcon(NULL,IDI_WARNING);
    win.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
    win.hInstance = hPrevInstance;
    win.lpfnWndProc = WinProc;
    win.lpszClassName = className;
    win.lpszMenuName = NULL;
    win.style = 0;
    if(!RegisterClassEx(&win)) return 0;

    hwnd = CreateWindowEx(0,className,"Formulário",WS_SIZEBOX | WS_TILED | WS_CAPTION | WS_SYSMENU,
                          300,200,700,430,NULL,NULL,hInstance,NULL);

                           option = CreateWindowEx(0,className,"a",WS_SIZEBOX | WS_TILED | WS_CAPTION,
                         450,350,350,200,0,0,hInstance,0);

          EditNum1= CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_VISIBLE|WS_CHILD,12,142,320,20,option,NULL,hInstance,NULL);
     Botao2 = CreateWindowEx(WS_EX_STATICEDGE,"BUTTON","Gravar",WS_VISIBLE | WS_CHILD,200,12,75,20,option,(HMENU)IDC_BOTAO2,hInstance,NULL);

    BotaoFechar = CreateWindowEx(WS_EX_STATICEDGE,"BUTTON","FECHAR",WS_VISIBLE | WS_CHILD,100,12,75,20,option,(HMENU)IDC_BOTAOFECHAR,hInstance,NULL);


    if(hwnd == NULL) return 0;
    ShowWindow(hwnd,SW_SHOW);
    UpdateWindow(hwnd);

    while(GetMessage(&message,0,0,0)){
        TranslateMessage(&message);
        DispatchMessage(&message);
    }
    return message.wParam;
}

LRESULT CALLBACK WinProc(HWND window,UINT msg, WPARAM wParam,LPARAM lParam)
{
    switch(msg){
        case WM_CREATE:

     Botao1 = CreateWindowEx(WS_EX_STATICEDGE,"BUTTON","Inverter",WS_VISIBLE | WS_CHILD,300,12,75,20,hwnd,(HMENU)ID_BOTAO1,g_inst,NULL);
        break;

        case WM_CLOSE:
             DestroyWindow(window);
        break;

        case WM_DESTROY:
             PostQuitMessage(0);
        break;

        case WM_COMMAND:
             switch(LOWORD(wParam)){
                 case ID_BOTAO1:{

               //       MessageBox(NULL,frase,frase,MB_OK);
                        ShowWindow(option, SW_SHOW);
                 }
                 break;
                 case IDC_BOTAOFECHAR:
                 {
                        ShowWindow(option, SW_HIDE);
                 }
                 break;
                 case IDC_BOTAO2:
                 {
                           SendMessage((HWND)EditNum1,(UINT)EM_GETLINE,(WPARAM)1,(LPARAM) &frase);
                            //         sprintf(frase,"[basico]\nplayername =%s\ndirpath =%s\nrompath =%s",);
                        ofstream myfile;
                        myfile.open ("xsnes.ini");
                        myfile << frase;
                        myfile.close();
                       MessageBox(NULL,frase,frase,MB_OK);
//
                        sprintf(frase,".");
                       // ShowWindow(option, SW_HIDE);

                 }
                 break;
             }
             break;

        default: return DefWindowProc(window, msg, wParam, lParam);
    }
    return 0;
}

Abraços,

Marcelo Utikawa da Fonseca

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



  • Forum Statistics

    • Total Topics
      151k
    • Total Posts
      649.1k
×
×
  • Create New...