hi all,

i am very new to programing and i have just writen a calculator for measuring building marerials, the program works great, but it dosn't look to pretty. how can i get my calculator to work in a window, instead of the default dev c++ command window thingy i plan on designing some sort of grid maybe, but im just unsure how to display text and user input in window. if this question makes anysense what so ever can someone help please ?
thanks, ryan.

Recommended Answers

All 3 Replies

The bad news is that GUI programming (graphical user interface, i.e., program appears in a window - not output to a console screen) is very different from console mode programming. If you are just starting out it would be a good idea to stick with the console for awhile. To move into GUI programming you have a number of choices in C++ but they are all somewhat taxing to learn. You either need to learn a class framework to create windows, or learn the Windows Api and to Sdk style coding. If you have Dev C++ there is a template installed you can look at that creates a window. Open Dev C++ and go to the File menu and choose New Project ... then from the templates pick 'Windows Application'. It will come up with a file for you to save and you can save it as 'Project1' and Main.cpp. Compile & run that and you'll see a window with nothing in it. To write text to that window like you would write text to a console screen you would use the Api functions TextOut() or DrawText(). Look over the code & you'll see what's involved.

Here I modified the Project1, 'Windows Application' code of the default template to add a WM_PAINT handler for you to show how to output text. Everything is the same as the template except for a few lines in the Window Procedure. Replace the default in your code editor with this & run it.

#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
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 */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    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;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* 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 */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_PAINT:
          {
             HDC hDC;
             PAINTSTRUCT ps;
             hDC=BeginPaint(hwnd,&ps);
             SetBkMode(hDC,TRANSPARENT);
             TextOut(hDC,20,20,"Here Is Some Text For You",25);
             EndPaint(hwnd,&ps);
             break;
          }   
        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);
    }

    return 0;
}

thanks very much for taking the time to do this, it explains alot,
i guess your right, stick to the console window for now and experiment a bit. anyways thanks again dude.

It would be good to mark the thread as solved if you are indeed satisfied?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.