Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ GUI
I have a graphics console application which could be better if I could change various values in it in a form, then execute the console application after clicking a "start" button on the form.
 
Suppose I have a simple c++ program like this (which could execute in turbo c++)
//addition.cpp
#include<iostream.h>
int x=10;
int y=100;
int main()
{
  int sum;
  sum=x+y;
  cout << "\n The sum of x and y is " << sum;
  return 0;
}</iostream.h>
 
As you know, if I compile and run this program, it should open a console window which displays the text "The sum of x and y is 110" and quits.
 
What I want now, is to have a windows form that opens first. The windows form contains:
-2 text boxes. One accepts the value "x" and the other accepts "y".
-A "submit" button.
 
When I click the "submit" button, the global variables' (int x and y) values in addition.cpp should have changed to whatever I have written in the text boxes so that the console displays the result accordingly.
 
Now, I don't want to change the way this program works (like some brilliant bloke suggesting me to post the result in an alert message instead) as I already told you, this is for creating front-end for a graphics based program I already built.
 
In brief, this program consists of a bouncing ball with gravity acting on it. I want the user to be able to change various parameters like initial position of the ball, gravitational constant, elasticity etc from a front-end form.
 
Anyway for now, I would appreciate it if I could get a guide or a walkthrough to get this simple addition program running.
 
PS: -I have some working knowledge of Visual C++ 2010 express edition although a detailed walkthrough couldn't hurt.
-I am an undergrad student, and I have no prior experience with all sorts of technical jargon used at the workplace. Having learnt VC++ so far amidst my busy academic schedule and no pay is something I would consider, a pretty great achievement.
-I used Allegro libraries for the graphics. Again, learned this one from scratch.
 
Best regards,
Divy
Posted 2-Sep-11 2:20am
Edited 2-Sep-11 2:29am
Prerak Patel140.7K
v3
Comments
Philippe Mori at 3-Sep-11 8:14am
   
Although your question might be a good one, you seem to not want to do things how they should be done!
 
Either as someone suggested, add forms to your console application to changes settings. Or as I have suggested, uses a distinct application for that part (and save the configuration somewhere it can be used by both).
Divyanthj123 at 3-Sep-11 8:34am
   
You mean like a separate file? That's a good idea perhaps I can make the forms application save data to a file and start the console application as a process...the console application can fetch data from the file to execute.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I think that in this case, you should create a Windows application. That application would then create another application (the existing application) and exit.
 
CreateProcess might be used.
 
http://msdn.microsoft.com/en-us/library/ms682425(v=vs.85).aspx[^]
  Permalink  
Comments
Divyanthj123 at 2-Sep-11 11:42am
   
In the example program I gave, how about changing the name of the main function to "int addition()", then call the addition() function from the addition.cpp?
 
Is that possible?
Philippe Mori at 2-Sep-11 13:14pm
   
I'm not sure to understand... as the question won't make sense to be asked if you know programmation and if not, then it would not make sense to do what you are trying to do with your initial question before knowing such basic things as what is a function.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Better way to create new Window Application. And port your old console base code to Window Base Application Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Okay then, I'm bored and it's dark outside so I can't go flying for another 3 or 4 hours when the sun comes up. I'll see what I can do about giving you a walk-through of sorts.
 
I've just looked into the Console Functions (windows) as suggested in my last post. I have found that it appears that I only need to use 2 of the functions before I can use a console window from a gui app. HOWEVER - in order to get printf or cout output to appear in this console, there is a little more work to do - as mentioned at the bottom of the MSDN page for AllocConsole. Following the suggestion, and reading the comment at the bottom of the page for AttachConsole we are directed to a page [^] that details how to connect these (and other) functions to the newly created console window.
 
To test the requested functionality, I
 
1) Created a windows32 gui app.
2) Added 3 buttons - 2 dummies and 1 to create a console window
3) Wired the 3rd button up to call this function:
 
void handleConsoleButton()
{
    MessageBox(NULL,"Creating the console","msg - handleConsoleButton", MB_OK);
    FreeConsole();
    RedirectIOToConsole();
    printf("Hello, World!\n");
}
 
This is of course, carried out in the main window's function as shown here:
/*  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_DESTROY:
        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;
 
    case WM_COMMAND:
        switch LOWORD(wParam)
        {
        case IDC_BTN1:
            MessageBox(NULL,"text1","msg", MB_OK);
            break;
        case IDC_BTN2:
            MessageBox(NULL,"text2","msg", MB_OK);
            break;
        case IDC_BTN3:
            handleConsoleButton();
            break;
        }
 
    default:                      /* for messages that we don't deal with */
        return DefWindowProc (hwnd, message, wParam, lParam);
    }
 
    return 0;
}
 

 
The function RedirectIOToconsole comes from the page I linked to and is defined thusly:
 
#define MAX_CONSOLE_LINES 500
 
void RedirectIOToConsole()
 
{
 
    int hConHandle;
 
    long lStdHandle;
 
    CONSOLE_SCREEN_BUFFER_INFO coninfo;
 
    FILE *fp;
 
// allocate a console for this app

    AllocConsole();
 
// set the screen buffer to be big enough to let us scroll text

    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
 
                               &coninfo);
 
    coninfo.dwSize.Y = MAX_CONSOLE_LINES;
 
    SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),
 
                               coninfo.dwSize);
 
// redirect unbuffered STDOUT to the console

    lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
 
    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
 
    fp = _fdopen( hConHandle, "w" );
 
    *stdout = *fp;
 
    setvbuf( stdout, NULL, _IONBF, 0 );
 
// redirect unbuffered STDIN to the console

    lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
 
    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
 
    fp = _fdopen( hConHandle, "r" );
 
    *stdin = *fp;
 
    setvbuf( stdin, NULL, _IONBF, 0 );
 
// redirect unbuffered STDERR to the console

    lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
 
    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
 
    fp = _fdopen( hConHandle, "w" );
 
    *stderr = *fp;
 
    setvbuf( stderr, NULL, _IONBF, 0 );
 
// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog

// point to console as well

    ios::sync_with_stdio();
 
}
 

Naturally, your mileage may vary. Though I mujst say thanks for providing the impetus to me finally learning (a little, little bit) about the Console Functions. Cheers!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

#include <windows.h>
#include <stdio.h>
#include <commctrl.h>
 
#define IDC_BTN1    10000
#define IDC_BTN2    10001
#define IDC_EDIT1   10002
#define IDC_EDIT2   10003
#define IDC_EDIT3   10004
 

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
 
/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";
 
int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nCmdShow)
{
    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 colour 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,szClassName,"Code::Blocks Template Windows App",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
                           450,125, HWND_DESKTOP, NULL, hThisInstance, NULL);
 

    CreateWindowEx(0,WC_EDIT, "20.02", WS_CHILD|WS_VISIBLE, 20,20, 75,20, hwnd, (HMENU)IDC_EDIT1, hThisInstance, NULL);
    CreateWindowEx(0,WC_BUTTON, "Plus", WS_CHILD|WS_VISIBLE, 120,20, 75,20, hwnd, (HMENU)IDC_BTN1, hThisInstance, NULL);
    CreateWindowEx(0,WC_BUTTON, "Minus", WS_CHILD|WS_VISIBLE, 120,50, 75,20, hwnd, (HMENU)IDC_BTN2, hThisInstance, NULL);
    CreateWindowEx(0,WC_EDIT, "10", WS_CHILD|WS_VISIBLE, 225,20, 75,20, hwnd, (HMENU)IDC_EDIT2, hThisInstance, NULL);
 
    CreateWindowEx(0,WC_STATIC, "=", WS_CHILD|WS_VISIBLE, 320,20, 15,20, hwnd, (HMENU)NULL, hThisInstance, NULL);
 
    CreateWindowEx(0,WC_EDIT, "0", WS_CHILD|WS_VISIBLE|ES_READONLY, 350,20, 55,20, hwnd, (HMENU)IDC_EDIT3, hThisInstance, NULL);
 

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);
 
    /* 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;
}
 
// returns true if both inputs are valid, false otherwise.
bool readInputs(HWND hwnd, float *num1, float *num2)
{
    const int bufLen = 256;
    char buffer[bufLen];
    HWND editBox1, editBox2;
    int numInputsConverted=0;
 
    editBox1 = GetDlgItem(hwnd, IDC_EDIT1);
    editBox2 = GetDlgItem(hwnd, IDC_EDIT2);
 
    GetWindowText(editBox1, buffer, bufLen-1);
    numInputsConverted = sscanf(buffer, "%f", num1);
    if (numInputsConverted == 0)
        return false;
 
    GetWindowText(editBox2, buffer, bufLen-1);
    numInputsConverted = sscanf(buffer, "%f", num2);
    if (numInputsConverted == 0)
        return false;
 
    return true;
}
 
void handleBtn1(HWND hwnd)
{
    float num1, num2, result;
    bool validInputs;
    const int bufLen = 256;
    char buffer[bufLen];
    HWND editBox3;
 
    editBox3 = GetDlgItem(hwnd, IDC_EDIT3);
 
    validInputs = readInputs(hwnd, &num1, &num2);
    if (validInputs)
    {
        result = num1 + num2;
        sprintf(buffer, "%0.2f", result);
        SetWindowText(editBox3, buffer);
    }
    else
        MessageBox(hwnd, "Please enter two valid numbers", "Error", MB_ICONERROR|MB_OK);
}
 
void handleBtn2(HWND hwnd)
{
    float num1, num2, result;
    bool validInputs;
    const int bufLen = 256;
    char buffer[bufLen];
    HWND editBox3;
 
    editBox3 = GetDlgItem(hwnd, IDC_EDIT3);
 
    validInputs = readInputs(hwnd, &num1, &num2);
    if (validInputs)
    {
        result = num1 - num2;
        sprintf(buffer, "%0.2f", result);
        SetWindowText(editBox3, buffer);
    }
    else
        MessageBox(hwnd, "Please enter two valid numbers", "Error", MB_ICONERROR|MB_OK);
}
 

/*  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_DESTROY:
        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;
 
    case WM_COMMAND:
        switch LOWORD(wParam)
        {
        case IDC_BTN1:
            handleBtn1(hwnd);
            break;
        case IDC_BTN2:
            handleBtn2(hwnd);
            break;
 
        }
 
    default:                      /* for messages that we don't deal with */
        return DefWindowProc (hwnd, message, wParam, lParam);
    }
 
    return 0;
}</commctrl.h></stdio.h></windows.h>
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 4 Sep 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100