Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 14th, 2006, 2:31 PM   #1
hervens48
Programmer
 
Join Date: Apr 2006
Location: Montreal, Canada
Posts: 93
Rep Power: 3 hervens48 is on a distinguished road
Send a message via AIM to hervens48 Send a message via MSN to hervens48
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 is offline   Reply With Quote
Old Jun 14th, 2006, 3:30 PM   #2
hervens48
Programmer
 
Join Date: Apr 2006
Location: Montreal, Canada
Posts: 93
Rep Power: 3 hervens48 is on a distinguished road
Send a message via AIM to hervens48 Send a message via MSN to hervens48
anyone?
hervens48 is offline   Reply With Quote
Old Jun 14th, 2006, 3:58 PM   #3
hervens48
Programmer
 
Join Date: Apr 2006
Location: Montreal, Canada
Posts: 93
Rep Power: 3 hervens48 is on a distinguished road
Send a message via AIM to hervens48 Send a message via MSN to hervens48
Please somebody, this is really important. I know a lot of you out there know win32
hervens48 is offline   Reply With Quote
Old Jun 14th, 2006, 4:17 PM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jun 14th, 2006, 4:29 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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!
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 14th, 2006, 4:30 PM   #6
hervens48
Programmer
 
Join Date: Apr 2006
Location: Montreal, Canada
Posts: 93
Rep Power: 3 hervens48 is on a distinguished road
Send a message via AIM to hervens48 Send a message via MSN to hervens48
i thought this was the right forum for it
hervens48 is offline   Reply With Quote
Old Jun 14th, 2006, 4:31 PM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Current Project Work
This is a place to discuss your current projects.
What's not clear about that?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jun 14th, 2006, 5:06 PM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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);
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jun 14th, 2006, 5:47 PM   #9
hervens48
Programmer
 
Join Date: Apr 2006
Location: Montreal, Canada
Posts: 93
Rep Power: 3 hervens48 is on a distinguished road
Send a message via AIM to hervens48 Send a message via MSN to hervens48
wow, thx a lot nnxion
hervens48 is offline   Reply With Quote
Old Jun 18th, 2006, 8:40 AM   #10
frankish
Hobbyist Programmer
 
frankish's Avatar
 
Join Date: Oct 2005
Location: Ohio
Posts: 177
Rep Power: 0 frankish is an unknown quantity at this point
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.
frankish is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:34 AM.

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