Thread: Bad transmission of parameters from edit button

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    Question Bad transmission of parameters from edit button

    Hi,

    I'm new with windows programming. My application is calculator with 3 edit buttons, and few button operator. After input numbers in edit, they should be calculated and put into edit3. And there is the problem, number in edit3 is enormous huge, 0 + 0 = 1984502288.
    Please help me

    Code:
    #include <windows.h>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <string>
    #define SPACE " "
    
    
    HWND djednako, dplus, dminus, dputa, dkroz, Edit1, Edit2, EditRezultat;
    
    
    void FatalnaGreska(const char poruka[]) {
    MessageBox(0, poruka, "Fatalna greska!", MB_OK | MB_ICONEXCLAMATION);
    std::exit(1); 
    }
    
    
    int uzmiBroj(HWND prozor, HWND h, int broj, char (spremnik)[2]){
    SendMessage(h, EM_LIMITTEXT, 0, 0L);
    int len = GetWindowTextLength(h);
    GetWindowText(h, spremnik, len);
    broj = atoi(spremnik); 
    if (h == NULL) {MessageBox(prozor, "Unesite broj u prazno polje.", "Poruka", MB_OK);}
    return broj; 
    }
    
    
    LRESULT CALLBACK Dispecer(HWND prozor, UINT poruka, WPARAM p1, LPARAM p2) {
    RECT okvir;// STRUKTURA: left, top, right, bottom, prect
    int notifikacija;
    HWND ko_je_poslao_komandu;
    char spremnik1[2], spremnik2[2], spremnik3[2]; 
    int broj1, broj2=0, rezultat;
    int op=0;
    switch(poruka) { 
    case WM_DESTROY: 
    PostQuitMessage(0); 
    break; 
    case WM_COMMAND:
    ko_je_poslao_komandu = HWND(p2);
    notifikacija = HIWORD(p1);
    if (op!=0) uzmiBroj(prozor, Edit1, broj1, spremnik1);
    
    if (ko_je_poslao_komandu == dplus && notifikacija == BN_CLICKED){op = 1;}
    
    if(ko_je_poslao_komandu == dminus && notifikacija == BN_CLICKED){op = 2;}
    
    if (ko_je_poslao_komandu == dputa && notifikacija == BN_CLICKED){op = 3;}
    
    if (ko_je_poslao_komandu == dkroz && notifikacija == BN_CLICKED){op = 4;}
    /*
    if ((ko_je_poslao_komandu == dplus || ko_je_poslao_komandu == dminus || ko_je_poslao_komandu == dputa || ko_je_poslao_komandu == dkroz) && notifikacija == BN_CLICKED){
    uzmiBroj(prozor, Edit1, broj1, spremnik1);}*/
    
    if(ko_je_poslao_komandu == djednako && notifikacija == BN_CLICKED) { uzmiBroj(prozor, Edit2, broj2, spremnik2);
    switch(op){
    case 1: rezultat = broj1 + broj2; break;
    case 2: rezultat = broj1 - broj2; break;
    case 3: rezultat = broj1 * broj2; break;
    case 4: rezultat = broj1 / broj2; break;
    //return rezultat;
    }
    //itoa(rezultat, spremnik3, 10);
    sprintf(spremnik3, "%d", rezultat);
    SetWindowText(EditRezultat, spremnik3);
    SendMessage(EditRezultat, EM_LIMITTEXT, 0, 0L);
    }
    default:
    return DefWindowProc(prozor, poruka, p1, p2);
    }
    return 0;
    }
    
    
    int main() {
    HINSTANCE aplikacija(GetModuleHandle(0));
    WNDCLASSEX prozorska_klasa;
    prozorska_klasa.hInstance = aplikacija;
    prozorska_klasa.lpszClassName = "MOJPROZOR";
    prozorska_klasa.lpfnWndProc = Dispecer;
    prozorska_klasa.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
    prozorska_klasa.cbSize = sizeof(WNDCLASSEX);
    prozorska_klasa.hIcon = LoadIcon(0, IDI_APPLICATION);
    prozorska_klasa.hIconSm = LoadIcon(0, IDI_APPLICATION);
    prozorska_klasa.hCursor = LoadCursor(0, IDC_ARROW);
    prozorska_klasa.lpszMenuName = 0; 
    prozorska_klasa.cbClsExtra = 0;
    prozorska_klasa.cbWndExtra = 0;
    prozorska_klasa.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
    if(!RegisterClassEx(&prozorska_klasa)) FatalnaGreska("Problem u registraciji prozorske klase!");
    
    HWND prozor(CreateWindowEx(WS_EX_CLIENTEDGE, "MOJPROZOR", 
    "Kalkulator", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 
    CW_USEDEFAULT, 280, 250, 0, 0, aplikacija, 0));
    if(!prozor) FatalnaGreska("Problem u kreiranju prozora!\n");
    
    Edit1 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", 
    " ", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_NUMBER | ES_RIGHT
    | ES_LOWERCASE, //:left x, top y, sirina, visina
    10, 10, 100, 30, prozor, 0, aplikacija, 0); 
    
    Edit2 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", 
    " ", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_NUMBER | ES_RIGHT
    | ES_LOWERCASE, //:left x, top y, sirina, visina
    10, 90, 100, 30, prozor, 0, aplikacija, 0); 
    
    EditRezultat = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", 
    " ", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_NUMBER | ES_RIGHT
    | ES_LOWERCASE, //:left x, top y, sirina, visina
    10, 170, 100, 30, prozor, 0, aplikacija, 0);
    
    
    djednako = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON",
    "=", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 
    10, 130, 50, 30, prozor, 0, aplikacija, 0);
    
    dkroz = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", 
    "/", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 
    70, 50, 50, 30, prozor, 0, aplikacija, 0);
    
    dputa = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", 
    "*", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 
    10, 50, 50, 30, prozor, 0, aplikacija, 0);
    
    dminus = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", 
    "-", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 
    130, 50, 50, 30, prozor, 0, aplikacija, 0);
    
    dplus = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON",
    "+", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 
    190, 50, 50, 30, prozor, 0, aplikacija, 0); 
    
    ShowWindow(prozor, SW_SHOW);
    UpdateWindow(prozor);
    
    MSG poruka;
    int status;
    while(status = GetMessage(&poruka, 0, 0, 0) > 0) {
    TranslateMessage(&poruka);
    DispatchMessage(&poruka);
    }
    if(status < 0) FatalnaGreska("Problem u obradi poruka!\n");
    return poruka.wParam;
    }
    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    SourceForge.net: Indentation - cpwiki
    Your indentation is non-existent.

    > while(status = GetMessage(&poruka, 0, 0, 0) > 0)
    Be careful of operator precedence here.
    You've written
    while(status = (GetMessage(&poruka, 0, 0, 0) > 0))
    Most people expect
    while( (status =GetMessage(&poruka, 0, 0, 0)) > 0)


    > char spremnik1[2], spremnik2[2], spremnik3[2];
    Try increasing these to some large number, like say
    char spremnik1[200], spremnik2[200], spremnik3[200];
    An array of length 2 has only space for 1 character (and the \0).

    Later on, start making use of the actual length of strings
    int len = GetWindowTextLength(h);
    to at least validate that you have room to store the data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You need to use 'static' variables in callbacks.

    >>if (op!=0) uzmiBroj(prozor, Edit1, broj1, spremnik1);

    This line will never be called as 'op' is always zero, so 'broj1' is always 'undefined' (uninitialised, will probably be set to zero in debug mode, depending on your IDE)

    This is because the 'op' variable only holds its value for one message (ie only has scope for one button click), then next message 'op' will be set to zero again.

    PS I find it easier to use a SWITCH in WM_COMMAND
    First switch on the ID of the control (which is the LOWORD of the WPARAM) as you have hard coded this ID when you created the control (either in the resource editor or as the HMENU param of CreateWindowEx() for a child control).

    Then a series of 'IFs" to handle the differnet type of message, which is the HIWORD of the WPARAM.

    Code:
    CASE WM_COMMAND:
    
    switch (LOWORD(p1)) //the controls ID number
         CASE IDB_MINUS:
              //minus button code
              if(HIWORD(p1) == BN_CLICKED)
                   //minus button was clicked
                   //etc
         break;
         CASE IDC_PLUS:
              //plus button code
              if(HIWORD(p1) == BN_CLICKED)
                   //plus button was clicked
                   //etc
         break;
    //etc
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Opps, missed your CreateWindowEx() calls.

    When creating a control, which has the WS_CHILD style you must cast the HMENU param to the uneque ID of the control

    Code:
    //in resource.h
    //define the controls ID
    #define IDB_MINUS    12345
    #define IDB_PLUS      12346
    
    //in main
    //set the ID of the minus button to use our defined value
    dminus = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON",  "-", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE,  130, 50, 50, 30, prozor, (HMENU)IDB_MINUS, aplikacija, 0);
    PS Look at GetDlgItem(), GetDlgItemtext(), GetDlgItemInt() and their 'setters' (SetDlgItemText(), SetDlgItemInt() etc)
    Last edited by novacain; 08-28-2012 at 12:39 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    Thanks Salem, thanks novacain I applied your advices, but it's again all the same if you could pleeease post me some tutorial about defining control id, I don't know what this number after define should means
    Code:
    #include <windows.h>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <string>
    #define SPACE  " "
    #define IDB_PLUS //?
    #define IDB_MINUS //??
    #define IDB_PUTA
    #define IDB_KROZ
    #define IDB_JEDNAKO 
    HWND djednako, dplus, dminus, dputa, dkroz, Edit1, Edit2, EditRezultat;
    
    void FatalnaGreska(const char poruka[]) {
      MessageBox(0, poruka, "Fatalna greska!", MB_OK | MB_ICONEXCLAMATION);
      std::exit(1);   
    }
    
    int uzmiBroj(HWND prozor, HWND h, int broj, char (spremnik)[200]){
        SendMessage(h, EM_LIMITTEXT, 0, 0L);
        int len = GetWindowTextLength(h);
        GetWindowText(h, spremnik, len);
        broj = atoi(spremnik);
        //if (h == NULL) {MessageBox(prozor, "Unesite broj u prazno polje.", "Poruka", MB_OK);}
    return broj;     
    }
    
    LRESULT CALLBACK Dispecer(HWND prozor, UINT poruka, WPARAM p1, LPARAM p2) {
      RECT okvir;// STRUKTURA: left, top, right, bottom, prect
      int notifikacija;
      HWND ko_je_poslao_komandu;
      char spremnik1[200], spremnik2[200], spremnik3[200]; 
      int broj1=0, broj2=0, rezultat;
      int op=0;
      switch(poruka) {          
        case WM_DESTROY:        
          PostQuitMessage(0);   
          break;                
        case WM_COMMAND:
             switch(LOWORD(p1)){
                                case IDB_PLUS:
                                     if (HIWORD(p1)==BN_CLICKED) {
                                                                 uzmiBroj(prozor, Edit1, broj1, spremnik1); 
                                                                 op=1;
                                     }
                                     break;
                                case IDB_MINUS:
                                     if (HIWORD(p1)==BN_CLICKED){
                                                                 uzmiBroj(prozor, Edit1, broj1, spremnik1); 
                                                                 op=2;
                                     }
                                     break;
                                case IDB_PUTA:
                                     if (HIWORD(p1)==BN_CLICKED) {
                                                                 uzmiBroj(prozor, Edit1, broj1, spremnik1);
                                                                 op=3;
                                     }
                                     break;
                                case IDB_KROZ:
                                     if (HIWORD(p1)==BN_CLICKED) {
                                                                 uzmiBroj(prozor, Edit1, broj1, spremnik1);
                                                                 op=4;
                                     }
                                     break;
                                case IDB_JEDNAKO:
                                     if (HIWORD(p1)==BN_CLICKED) {
                                                                 uzmiBroj(prozor, Edit2, broj2, spremnik2);
                                                                 switch(op){
                                                                            case 1: rezultat = broj1 + broj2; break;
                                                                            case 2: rezultat = broj1 - broj2; break;
                                                                            case 3: rezultat = broj1 * broj2; break;
                                                                            case 4: rezultat = broj1 / broj2; break;
                                                                 }
                                     itoa(rezultat, spremnik3, 10);
                                     //sprintf(spremnik3, "%d", rezultat);
                                     SetWindowText(EditRezultat, spremnik1);
                                     //SendMessage(EditRezultat, EM_LIMITTEXT, 0, 0L);
                                     }
                                     break;
             }
             break;
        default:
        return DefWindowProc(prozor, poruka, p1, p2);
      }
      return 0;
    }
    
    int main() {
      HINSTANCE aplikacija(GetModuleHandle(0));
      WNDCLASSEX prozorska_klasa;
      prozorska_klasa.hInstance = aplikacija;
      prozorska_klasa.lpszClassName = "MOJPROZOR";
      prozorska_klasa.lpfnWndProc = Dispecer;
      prozorska_klasa.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
      prozorska_klasa.cbSize = sizeof(WNDCLASSEX);
      prozorska_klasa.hIcon = LoadIcon(0, IDI_APPLICATION);
      prozorska_klasa.hIconSm = LoadIcon(0, IDI_APPLICATION);
      prozorska_klasa.hCursor = LoadCursor(0, IDC_ARROW);
      prozorska_klasa.lpszMenuName = 0;                 
      prozorska_klasa.cbClsExtra = 0;
      prozorska_klasa.cbWndExtra = 0;
      prozorska_klasa.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
      if(!RegisterClassEx(&prozorska_klasa))  FatalnaGreska("Problem u registraciji prozorske klase!");
        
      HWND prozor(CreateWindowEx(WS_EX_CLIENTEDGE, "MOJPROZOR", 
        "Kalkulator", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 
        CW_USEDEFAULT, 280, 250, 0, 0, aplikacija, 0));
      if(!prozor) FatalnaGreska("Problem u kreiranju prozora!\n");
      
      Edit1 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", 
        "", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_NUMBER | ES_RIGHT
        | ES_LOWERCASE, //:left x, top y, sirina, visina
        10, 10, 100, 30, prozor, 0, aplikacija, 0); 
        
      Edit2 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", 
        "", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_NUMBER | ES_RIGHT
        | ES_LOWERCASE, //:left x, top y, sirina, visina
        10, 90, 100, 30, prozor, 0, aplikacija, 0); 
        
        EditRezultat = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT",  "", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_NUMBER | ES_RIGHT | ES_LOWERCASE, 10, 170, 100, 30, prozor, 0, aplikacija, 0);
         djednako = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "=", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 
      10, 130, 50, 30, prozor, (HMENU)IDB_JEDNAKO, aplikacija, 0);
      
      dkroz = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "/", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 70, 50, 50, 30, prozor, 0, aplikacija, 0);
      
      dputa = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "*", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 10, 50, 50, 30, prozor, 0, aplikacija, 0);
      
      dminus = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "-", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 130, 50, 50, 30, prozor, (HMENU)IDB_MINUS, aplikacija, 0);
      
      dplus = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "+", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 190, 50, 50, 30, prozor, (HMENU)IDB_PLUS, aplikacija, 0);     
          
      ShowWindow(prozor, SW_SHOW);
      UpdateWindow(prozor);
      
      MSG poruka;
      int status;
      while((status = GetMessage(&poruka, 0, 0, 0)) > 0) {
        TranslateMessage(&poruka);
        DispatchMessage(&poruka);
    // SendMessage(Edit,(UINT)WM_SETFONT, WPARAM, "1");
      }
      if(status < 0) FatalnaGreska("Problem u obradi poruka!\n");
      return poruka.wParam;
    }
    Last edited by Amela Bambur; 08-28-2012 at 08:36 AM.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    The value of the ID does not really matter, as long as you do not repeat the number on the same window/dialog.

    I use 40,000+ but any number is fine, as long as each ID is different.

    The main issue is that your variables are not STATIC.

    Code:
      static char spremnik1[200], spremnik2[200], spremnik3[200];    
      static int broj1=0, broj2=0, rezultat;   
      static int op=0;
    Adding the STATIC to the variable means it holds its value for the life of your application.

    Without the static the variable 'resets' each new message.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Here is some MSDN code showing how they create a series of buttons (scroll to the bottom for the code).

    MS uses #define ID_BUTTON1 0x8801 but I would just use an int. I use 40000 as it is greater than the ID numbers MSVC will use, so I can be sure my IDs are different from those the IDE creates automatically.

    Code:
    //pick a number and just increase it by 1 for each control
    #define IDB_PLUS 40000
    #define IDB_MINUS 40001
    #define IDB_PUTA 40002
    #define IDB_KROZ 40003
    #define IDB_JEDNAKO  40004
    Creating a Button

    NOTE: 2 of your button CreateWindowEx() calls are missing the HMENU param.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    hey I resolved my problem
    Code:
    #include <windows.h>#include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <string>
    #define SPACE  " "
    HWND djednako, dplus, dminus, dputa, dkroz, drec, dsqrt, dexp, d10, Edit1, Edit2, EditRezultat;
    
    
    void FatalnaGreska(const char poruka[]) {
      MessageBox(0, poruka, "Fatalna greska!", MB_OK | MB_ICONEXCLAMATION);
      std::exit(1);   
    }
    
    
    int broj1, broj2, rezultat;
    int op;
    
    
    LRESULT CALLBACK Dispecer(HWND prozor, UINT poruka, WPARAM p1, LPARAM p2) {
      RECT okvir;// STRUKTURA: left, top, right, bottom, prect
      int notifikacija;
      HWND ko_je_poslao_komandu;
      char spremnik1[200], spremnik2[200], spremnik3[200]; 
      switch(poruka) {          
        case WM_DESTROY:        
          PostQuitMessage(0);   
          break;                
        case WM_COMMAND:
             ko_je_poslao_komandu = HWND(p2);
             notifikacija = HIWORD(p1);
             if (ko_je_poslao_komandu == djednako && notifikacija == BN_CLICKED){
                                      GetWindowText(Edit1, spremnik1, sizeof spremnik1);
                                      GetWindowText(Edit2, spremnik2, sizeof spremnik2);
                                      broj1 = atoi(spremnik1);
                                      broj2 = atoi(spremnik2);
                                      switch (op){
                                             case 1: rezultat = broj1 + broj2; break;
                                             case 2: rezultat = broj1 - broj2; break;
                                             case 3: rezultat = broj1 * broj2; break;
                                             case 4: rezultat = broj1 / broj2; break;
                                             case 5: rezultat = 1 / broj1; break;
                                             case 6: rezultat = sqrt(broj1); break;
                                             case 7: rezultat = pow(broj1, broj2); break;
                                             case 8: rezultat = pow(broj1, 10); break;
                                      } 
                                      itoa (rezultat, spremnik3, 10);
                                      SetWindowText(EditRezultat, spremnik3);
             }
             if (ko_je_poslao_komandu == dplus && notifikacija == BN_CLICKED){
                                   op = 1;
             }
             if (ko_je_poslao_komandu == dminus && notifikacija == BN_CLICKED){
                                   op = 2;
             }
             if (ko_je_poslao_komandu == dputa && notifikacija == BN_CLICKED){
                                   op = 3;
             }
             if (ko_je_poslao_komandu == dkroz && notifikacija == BN_CLICKED){
                                   op = 4;
             }
             if (ko_je_poslao_komandu == drec && notifikacija == BN_CLICKED){
                                   op = 5;
             }
             if (ko_je_poslao_komandu == dsqrt && notifikacija == BN_CLICKED){
                                   op = 6;
             }
             if (ko_je_poslao_komandu == dexp && notifikacija == BN_CLICKED){
                                   op = 7;
             }
             if (ko_je_poslao_komandu == d10 && notifikacija == BN_CLICKED){
                                   op = 8;
             }
             break;
        default:
        return DefWindowProc(prozor, poruka, p1, p2);
      }
      return 0;
    }
    
    
    int main() {
      HINSTANCE aplikacija(GetModuleHandle(0));
      WNDCLASSEX prozorska_klasa;
      prozorska_klasa.hInstance = aplikacija;
      prozorska_klasa.lpszClassName = "MOJPROZOR";
      prozorska_klasa.lpfnWndProc = Dispecer;
      prozorska_klasa.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
      prozorska_klasa.cbSize = sizeof(WNDCLASSEX);
      prozorska_klasa.hIcon = LoadIcon(0, IDI_APPLICATION);
      prozorska_klasa.hIconSm = LoadIcon(0, IDI_APPLICATION);
      prozorska_klasa.hCursor = LoadCursor(0, IDC_ARROW);
      prozorska_klasa.lpszMenuName = 0;                 
      prozorska_klasa.cbClsExtra = 0;
      prozorska_klasa.cbWndExtra = 0;
      prozorska_klasa.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
      if(!RegisterClassEx(&prozorska_klasa))  FatalnaGreska("Problem u registraciji prozorske klase!");
        
    HWND prozor(CreateWindowEx(WS_EX_CLIENTEDGE, "MOJPROZOR", "Kalkulator", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 
    270, 290, 0, 0, aplikacija, 0));
    
    
    if(!prozor) FatalnaGreska("Problem u kreiranju prozora!\n");
      
    Edit1 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_NUMBER | ES_RIGHT | ES_LOWERCASE,
       //:left x, top y, sirina, visina
    75, 10, 100, 30, prozor, 0, aplikacija, 0); 
        
    Edit2 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_NUMBER | ES_RIGHT | ES_LOWERCASE, 
    75, 130, 100, 30, prozor, 0, aplikacija, 0); 
        
    EditRezultat = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_NUMBER | ES_RIGHT
    | ES_LOWERCASE, 75, 210, 100, 30, prozor, 0, aplikacija, 0);
        
    djednako = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "=", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 105, 170, 50, 30, prozor, 0,
     aplikacija, 0);
      
    dplus = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "+", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 10, 50, 50, 30, prozor, 0, 
    aplikacija, 0);
      
    dminus = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "-", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 70, 50, 50, 30, prozor, 0, 
    aplikacija, 0);
      
    dputa = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "*", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 130, 50, 50, 30, prozor, 0, 
    aplikacija, 0);
      
    dkroz = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "/", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 190, 50, 50, 30, prozor, 0, 
    aplikacija, 0);     
    
    
    drec = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "1/x", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 10, 90, 50, 30, prozor, 0, 
    aplikacija, 0);
    
    
    dsqrt = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "x^1/2", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 70, 90, 50, 30, prozor, 0, 
    aplikacija, 0);
    
    
    dexp = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "x^y", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 130, 90, 50, 30, prozor, 0, 
    aplikacija, 0);
    
    
    d10 = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "10^x", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE,  190, 90, 50, 30, prozor, 0, 
    aplikacija, 0);
      
      ShowWindow(prozor, SW_SHOW);
      UpdateWindow(prozor);
      
      MSG poruka;
      int status;
      while((status = GetMessage(&poruka, 0, 0, 0)) > 0) {
                    TranslateMessage(&poruka);
                    DispatchMessage(&poruka);
      }
      if(status < 0) FatalnaGreska("Problem u obradi poruka!\n");
      return poruka.wParam;
    }

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you "solved" your problem with global variables? that's not a solution. it's a hack. global variables are bad. just make them static inside the function, and it should work.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    Thanks novacain and Elkvis, I tried and it works

    Do you have any idea how to work with decimal numbers?
    thanks again for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button and edit control border
    By maxorator in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2005, 02:31 PM
  2. Set Button / Edit box font
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 09-30-2005, 10:21 AM
  3. IE end of transmission?
    By Sebastiani in forum C++ Programming
    Replies: 7
    Last Post: 10-22-2002, 05:31 PM

Tags for this Thread