Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Existing Project Development (http://www.programmingforums.org/forum51.html)
-   -   Help me fix this code please (http://www.programmingforums.org/showthread.php?t=10361)

hervens48 Jun 14th, 2006 3:31 PM

Help me fix this code please
 
Hello guys, im working on this windows calculator and im having a few trouble doing stuff.
The following code will display the unfinished calculator
:

#include <windows.h>
#include <iostream>
//define all the buttons
#define IDE_TEXT 101
#define IDB_BUTTON1 500
#define IDB_BUTTON2 501
#define IDB_BUTTON3 502
#define IDB_BUTTON4 503
#define IDB_BUTTON5 504
#define IDB_BUTTON6 505
#define IDB_BUTTON7 506
#define IDB_BUTTON8 507
#define IDB_BUTTON9 508
#define IDB_BUTTONANSWER 509
#define IDB_BUTTONADD 510
#define IDB_BUTTONMINUS 511

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInst;

char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;              /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */
    g_hInst=hThisInstance;
    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;     
    wincl.style = CS_DBLCLKS;                /* Catch double-clicks */
    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;                /* No menu */
    wincl.cbClsExtra = 0;                   
    wincl.cbWndExtra = 0;                     
      wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;


    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
          0,                  /* Extended possibilites for variation */
          szClassName,        /* Classname */
          "Frantz corporation",      /* Title Text */
          WS_OVERLAPPEDWINDOW, /* default window */
          CW_USEDEFAULT,      /* Windows decides the position */
          CW_USEDEFAULT,      /* where the window ends up on the screen */
          544,                /* The programs width */
          375,                /* and height in pixels */
          HWND_DESKTOP,        /* The window is a child-window to desktop */
          NULL,                /* No menu */
          hThisInstance,      /* Program Instance handler */
          NULL                /* No Window Creation data */
          );

 
    ShowWindow (hwnd, nFunsterStil);


    while (GetMessage (&messages, NULL, 0, 0))
    {
       
        TranslateMessage(&messages);

        DispatchMessage(&messages);
    }


    return messages.wParam;
}




LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)


{
HDC hdc;
PAINTSTRUCT ps;
RECT rc;
static HWND Button1=0;
static HWND Button2=0;
static HWND Button3=0;
static HWND Button4=0;
static HWND Button5=0;
static HWND Button6=0;
static HWND Button7=0;
static HWND Button8=0;
static HWND Button9=0;
static HWND ButtonAnswer=0;
static HWND ButtonAdd=0;
static HWND ButtonMinus=0;
static HWND ButtonWindow=0;
    switch (message)                  /* handle the messages */
    {
          case WM_CREATE:
                //The following codes will draw all the buttons in calculator
                CreateWindow(
                "Button",
                "1",
                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                10,80,50,30,
                hwnd,
                (HMENU) IDB_BUTTON1,
                GetModuleHandle(NULL), NULL);
               
                CreateWindow(
                "Button",
                "2",
                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                70,80,50,30,
                hwnd,
                (HMENU) IDB_BUTTON2,
                GetModuleHandle(NULL), NULL);
               
                CreateWindow(
                "Button",
                "3",
                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                130,80,50,30,
                hwnd,
                (HMENU) IDB_BUTTON3,
                GetModuleHandle(NULL), NULL);
               
                Button4=CreateWindow(
                "Button",
                "4",
                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                10,120,50,30,
                hwnd,
                (HMENU) IDB_BUTTON4,
                GetModuleHandle(NULL), NULL);
               
                Button5=CreateWindow(
                "Button",
                "5",
                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                70,120,50,30,
                hwnd,
                (HMENU) IDB_BUTTON5,
                GetModuleHandle(NULL), NULL);
               
             
                Button6=CreateWindow(
                "Button",
                "6",
                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                130,120,50,30,
                hwnd,
                (HMENU) IDB_BUTTON6,
                GetModuleHandle(NULL), NULL);
               
                Button7=CreateWindow(
                "Button",
                "7",
                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                10,160,50,30,
                hwnd,
                (HMENU) IDB_BUTTON7,
                GetModuleHandle(NULL), NULL);
               
                Button8=CreateWindow(
                "Button",
                "8",
                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                70,160,50,30,
                hwnd,
                (HMENU) IDB_BUTTON8,
                GetModuleHandle(NULL), NULL);
               
                Button9=CreateWindow(
                "Button",
                "9",
                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                130,160,50,30,
                hwnd,
                (HMENU) IDB_BUTTON9,
                GetModuleHandle(NULL), NULL);
               
                ButtonAnswer=CreateWindow(
                "Button",
                "Answer",
                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                80,200,60,30,
                hwnd,
                (HMENU) IDB_BUTTONANSWER,
                GetModuleHandle(NULL), NULL);
               
                ButtonAdd=CreateWindow(
                "Button",
                "+",
                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                10,200,50,30,
                hwnd,
                (HMENU) IDB_BUTTONADD,
                GetModuleHandle(NULL), NULL);
               
               
                ButtonWindow=CreateWindow(
                "edit",
                NULL,
                WS_VISIBLE | WS_CHILD,
                80,300,100,20,
                hwnd,
                (HMENU) IDE_TEXT,
                GetModuleHandle(NULL),
                NULL);
                break;
               
          case WM_COMMAND:
                //was a button clicked?
                if( HIWORD( wParam ) == BN_CLICKED )
      {
                //this is where im having trouble
              switch( LOWORD( wParam ) )
                {
              //how do i make 1 show up in text window when i click this?????
                      //textout does not work.
                      //same thing go with all the other buttons
                case IDB_BUTTON1:
                    hdc=GetDC(hwnd);
                    TextOut(hdc, 80, 300, "1", 1);
                    break;
                    //how do i get the answer to show up?
                    case IDB_BUTTONANSWER:
                          break;
                          }
                             
                        //and also, when i click a button, the window leaves!!   
        case WM_DESTROY:
            PostQuitMessage (0);      /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
            break;
    }

break;
}
}


Now ive got 2 problems
1: How do i make it like when i click a button, it outputs the wanted number on the screen.

2: How do i make the answer show up when i click the answer button.
Please, ive been working on this for hours.

Also, when i click a button, the calculator closes

Oh, and the code will compile under the devc++ compiler

hervens48 Jun 14th, 2006 4:30 PM

anyone?

hervens48 Jun 14th, 2006 4:58 PM

Please somebody, this is really important. I know a lot of you out there know win32

nnxion Jun 14th, 2006 5:17 PM

Yes, we know how, but you should put it in the right forum so someone looks at it. I'll take a look at it.

DaWei Jun 14th, 2006 5:29 PM

Let's see. An hour went by without someone fixing your problems. Then another 30 minutes. Damn, what IS wrong with our priorities. We certainly get paid to respond faster than that!

hervens48 Jun 14th, 2006 5:30 PM

i thought this was the right forum for it

nnxion Jun 14th, 2006 5:31 PM

Quote:

Current Project Work
This is a place to discuss your current projects.
What's not clear about that? :confused:

nnxion Jun 14th, 2006 6:06 PM

Here is something you can use, you can use stringstreams to convert from ints to strings.

Visualise in steps what you want to do.

:

#include <sstream>
#include <windows.h>

#define IDE_TEXT 101
#define IDB_BUTTON1 500
#define IDB_BUTTON2 501
#define IDB_BUTTON3 502
#define IDB_BUTTON4 503
#define IDB_BUTTON5 504
#define IDB_BUTTON6 505
#define IDB_BUTTON7 506
#define IDB_BUTTON8 507
#define IDB_BUTTON9 508
#define IDB_BUTTONANSWER 509
#define IDB_BUTTONADD 510
#define IDB_BUTTONMINUS 511

const char *clsName = "WindowsApp";
const char *wndName = "Calculator";

LRESULT CALLBACK WndProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void OnWmCreate(HWND);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
        MSG        msg;
        HWND      hwnd;
        WNDCLASSEX wcx;

        wcx.cbSize        = sizeof(WNDCLASSEX);
        wcx.style        = CS_HREDRAW | CS_VREDRAW;
        wcx.lpfnWndProc  = WndProcedure;
        wcx.cbClsExtra    = 0;
        wcx.cbWndExtra    = 0;
        wcx.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
        wcx.hCursor      = LoadCursor(NULL, IDC_ARROW);
        wcx.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
        wcx.lpszMenuName  = NULL;
        wcx.lpszClassName = clsName;
        wcx.hInstance    = hInstance;
        wcx.hIconSm      = LoadIcon(NULL, IDI_APPLICATION);

        if(!RegisterClassEx(&wcx))
        {
              MessageBox(NULL, "RegisterClassEx Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
              return 1;
          }

        hwnd = CreateWindow(clsName,
                          wndName,
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          220,
                          380,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

        if(!hwnd)
        {
              MessageBox(NULL, "CreateWindow Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
              return 1;
          }

        ShowWindow(hwnd, SW_SHOWNORMAL);
        UpdateWindow(hwnd);

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

        return (int)msg.wParam;
}

LRESULT CALLBACK WndProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        static int first, second, calculated;

        std::stringstream answer;

    switch(msg)
    {
                case WM_CREATE:
                        OnWmCreate(hwnd);
                break;

                case WM_DESTROY:
                        PostQuitMessage(WM_QUIT);
                        break;

                case WM_COMMAND:
                        if(HIWORD(wParam) == BN_CLICKED)
                        {
                                switch(LOWORD(wParam))
                                {
                                        case IDB_BUTTON1:
                                                MessageBox(hwnd, "1", "Input", MB_ICONINFORMATION | MB_OK);
                                        break;

                                        case IDB_BUTTON2:
                                                MessageBox(hwnd, "2", "Input", MB_ICONINFORMATION | MB_OK);
                                        break;

                                        case IDB_BUTTON3:
                                                MessageBox(hwnd, "3", "Input", MB_ICONINFORMATION | MB_OK);
                                        break;

                                        case IDB_BUTTON4:
                                                MessageBox(hwnd, "4", "Input", MB_ICONINFORMATION | MB_OK);
                                        break;

                                        case IDB_BUTTON5:
                                                MessageBox(hwnd, "5", "Input", MB_ICONINFORMATION | MB_OK);
                                        break;

                                        case IDB_BUTTON6:
                                                MessageBox(hwnd, "6", "Input", MB_ICONINFORMATION | MB_OK);
                                        break;

                                        case IDB_BUTTON7:
                                                MessageBox(hwnd, "7", "Input", MB_ICONINFORMATION | MB_OK);
                                        break;

                                        case IDB_BUTTON8:
                                                MessageBox(hwnd, "8", "Input", MB_ICONINFORMATION | MB_OK);
                                        break;

                                        case IDB_BUTTON9:
                                                MessageBox(hwnd, "9", "Answer", MB_ICONINFORMATION | MB_OK);
                                        break;

                                        case IDB_BUTTONANSWER:
                                                MessageBox(hwnd, "Answer", "Answer", MB_ICONINFORMATION | MB_OK);
                                        break;
                                }
                        }
                        break;

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

void OnWmCreate(HWND hwnd)
{
        static HWND button1 = CreateWindow(
        "button",
        "1",
        WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
        10,80,50,30,
        hwnd,
        (HMENU) IDB_BUTTON1,
        GetModuleHandle(NULL), NULL);

        static HWND button2 = CreateWindow(
        "button",
        "2",
        WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
        70,80,50,30,
        hwnd,
        (HMENU) IDB_BUTTON2,
        GetModuleHandle(NULL), NULL);

        static HWND button3 = CreateWindow(
        "button",
        "3",
        WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
        130,80,50,30,
        hwnd,
        (HMENU) IDB_BUTTON3,
        GetModuleHandle(NULL), NULL);

        static HWND button4 = CreateWindow(
        "button",
        "4",
        WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
        10,120,50,30,
        hwnd,
        (HMENU) IDB_BUTTON4,
        GetModuleHandle(NULL), NULL);

        static HWND button5 = CreateWindow(
        "button",
        "5",
        WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
        70,120,50,30,
        hwnd,
        (HMENU) IDB_BUTTON5,
        GetModuleHandle(NULL), NULL);


        static HWND button6 = CreateWindow(
        "button",
        "6",
        WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
        130,120,50,30,
        hwnd,
        (HMENU) IDB_BUTTON6,
        GetModuleHandle(NULL), NULL);

        static HWND button7 = CreateWindow(
        "button",
        "7",
        WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
        10,160,50,30,
        hwnd,
        (HMENU) IDB_BUTTON7,
        GetModuleHandle(NULL), NULL);

        static HWND button8 = CreateWindow(
        "button",
        "8",
        WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
        70,160,50,30,
        hwnd,
        (HMENU) IDB_BUTTON8,
        GetModuleHandle(NULL), NULL);

        static HWND button9 = CreateWindow(
        "button",
        "9",
        WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
        130,160,50,30,
        hwnd,
        (HMENU) IDB_BUTTON9,
        GetModuleHandle(NULL), NULL);

        static HWND buttonAnswer = CreateWindow(
        "button",
        "Answer",
        WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
        80,200,60,30,
        hwnd,
        (HMENU) IDB_BUTTONANSWER,
        GetModuleHandle(NULL), NULL);

        static HWND buttonAdd = CreateWindow(
        "button",
        "+",
        WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
        10,200,50,30,
        hwnd,
        (HMENU) IDB_BUTTONADD,
        GetModuleHandle(NULL), NULL);


        static HWND buttonWindow = CreateWindow(
        "edit",
        NULL,
        WS_VISIBLE | WS_CHILD,
        80,300,100,20,
        hwnd,
        (HMENU) IDE_TEXT,
        GetModuleHandle(NULL),
        NULL);
}


hervens48 Jun 14th, 2006 6:47 PM

wow, thx a lot nnxion

frankish Jun 18th, 2006 9:40 AM

Since you are doing this in C++ you should use single line comments and try to avoid multiline ones. This will make debugging much easier.


All times are GMT -5. The time now is 8:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC