Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple c programming gui

I developed the steam table equation solver in C language...but the inputing the values in black screen console is boring.

So I strictly wanted to create simple GUI in C.

I searched for hello world codes, all were pretty long. But this was the only one I understood.

#include <windows.h>

int main()
{
MessageBoxA( NULL, "Hello World!", "Hello", MB_OK );
}

By using a gui builder for C, i got this code, now I was thinking how to scan values from TEXTBOX1 and TEXTBOX2 on Clicking of COMMANDBUTTON1 and display the output in TEXTBOX3?

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include "hello.auto.h"


HWND hwnd_Label1, hwnd_Label2, hwnd_TextBox1, hwnd_TextBox2, hwnd_CommandButton1,
hwnd_TextBox3;

HFONT MSSansSerif_8pt;



void CreateChildWindows(HWND hwndMainWindow, HINSTANCE hInstance)
{
InitCommonControls();

MSSansSerif_8pt = CreateFont(-11,0,0,0,FW_NORMAL,0,0,0,0,0,0,0,0,"MS Sans Serif");

hwnd_Label1 = CreateWindowEx(0, "Static", "Pressure",
    WS_CHILD | WS_VISIBLE,
    11, 55, 95, 38, hwndMainWindow,
    (HMENU)Label1, hInstance, NULL);

SetWindowFont(hwnd_Label1, MSSansSerif_8pt, TRUE);

hwnd_Label2 = CreateWindowEx(0, "Static", "Temperature",
    WS_CHILD | WS_VISIBLE,
    11, 110, 95, 38, hwndMainWindow,
    (HMENU)Label2, hInstance, NULL);

SetWindowFont(hwnd_Label2, MSSansSerif_8pt, TRUE);

hwnd_TextBox1 = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit" , NULL,
    WS_CHILD | ES_WANTRETURN | WS_VISIBLE,
    187, 55, 83, 35, hwndMainWindow,
    (HMENU)TextBox1, hInstance, NULL);

SetWindowFont(hwnd_TextBox1, MSSansSerif_8pt, TRUE);

hwnd_TextBox2 = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit" , NULL,
    WS_CHILD | ES_WANTRETURN | WS_VISIBLE,
    187, 99, 83, 35, hwndMainWindow,
    (HMENU)TextBox2, hInstance, NULL);

SetWindowFont(hwnd_TextBox2, MSSansSerif_8pt, TRUE);

hwnd_CommandButton1 = CreateWindowEx(0, "Button", "CommandButton1",
    WS_CHILD | BS_MULTILINE | BS_PUSHBUTTON | WS_VISIBLE,
    308, 77, 117, 52, hwndMainWindow,
    (HMENU)CommandButton1, hInstance, NULL);


SetWindowFont(hwnd_CommandButton1, MSSansSerif_8pt, TRUE);

hwnd_TextBox3 = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit" , NULL,
    WS_CHILD | ES_WANTRETURN | WS_VISIBLE,
    66, 220, 385, 35, hwndMainWindow,
    (HMENU)TextBox3, hInstance, NULL);

SetWindowFont(hwnd_TextBox3, MSSansSerif_8pt, TRUE);

return;
}



HWND GetItem(int nIDDlgItem)
{
switch(nIDDlgItem)
{
    case -1:
        return GetParent(hwnd_Label1);
    case Label1:
        return hwnd_Label1;
    case Label2:
        return hwnd_Label2;
    case TextBox1:
        return hwnd_TextBox1;
    case TextBox2:
        return hwnd_TextBox2;
    case CommandButton1:
        return hwnd_CommandButton1;
    case TextBox3:
        return hwnd_TextBox3;
    default: return NULL;
}
}



void Form_Unload(HWND hMainWnd)
{
DeleteFont(MSSansSerif_8pt);
return;
}

I tried many times, but failed. Even if you people give me links of good sites, then I will be greatful.

like image 906
Santosh Avatar asked Aug 02 '12 03:08

Santosh


People also ask

Can you write a GUI in C?

All operating systems are written in C. So, any application, console/GUI you write in C is the standard way of writing for the operating system.

What is the best GUI for C?

GTK is a popular GUI that works with C . Here is a simple “Hello World!” in C and GTK. The program “hello.

Can you use GTK with C?

GTK is a widget toolkit. Each user interface created by GTK consists of widgets. This is implemented in C using GObject, an object-oriented framework for C. Widgets are organized in a hierarchy.

Can you make a GUI with C++?

To develop C++ GUI or C++ graphical user interface application, you need an IDE that supports the C++ GUI application. To create the GUI app, you must use Visual Studio 2019 because it is better suited for the C++ GUI application.


1 Answers

You're looking for a good book on Win32 API programming using C. And you're in luck, there's a great book that pretty much everyone uses to learn it. It's written by Charles Petzold, and it's called (aptly enough) Programming Windows. You want the 5th edition, which is the most recent version that discusses Win32 programming.

There are also various tutorials available online if you search for "Win32 programming". However, a number of them contain some errors and misunderstandings (like the difference between ANSI and Unicode strings), and the good ones are rather short and incomplete. You won't learn everything you need to know to write a non-trivial program from online tutorials, but you should be able to get something really simple up on the screen. This one by theForger is one I've seen recommended many times.

Be warned, though, that writing GUI code (especially at such a low level) tends to be a lot more verbose than simply getting text onto the screen for a console program. You're going to end up writing a bunch of code that is used only to make the GUI work and has nothing to do with the logic of your program. It's much easier if you learn the C language first, and that's best done through simple console-type, text-only programs.


As for your specific question, you've created three textbox controls on the screen, named hwnd_TextBoxX, where X is a number between 1 and 3. As you probably already know, hwnd indicates a handle to a window (wnd), and so those variables hold handles to the textbox windows.

The Win32 API provides a GetWindowText function, which you can use to retrieve the text from a particular window. You pass it a handle to the desired window (hWnd), a pointer to a character buffer, and an integer value indicating the length of your buffer. Already, the ugly nature of the C language crops up—you have to understand how strings work in C in order to call the function correctly.

Once you've retrieved the string displayed in one of the textboxes, you can display it in another textbox window using the similar SetWindowText function, which takes only the window handle (hWnd) and a pointer to the buffer containing the string.

The code would look something like this:

// Get the text displayed in textbox 1
TCHAR szBuffer[100];
GetWindowText(hwnd_TextBox1, szBuffer, 100);

// Display that text in textbox 3
SetWindowText(hwnd_TextBox3, szBuffer);

To do this in response to a click on a button, you'll need to learn about the Win32 equivalent of "events"—window messages. Child windows, like button controls, send notification messages to their parent window (i.e., a dialog) when something potentially interesting happens, like the user clicks on them.

In particular, a button control sends its parent a BN_CLICKED notification through the WM_COMMAND message. By handling the WM_COMMAND message in your main window's window procedure (WndProc) method, you can check that the lParam parameter matches your button control's window handle (hWnd) and that the HIWORD(wParam) matches the BN_CLICKED notification code.

You definitely need a good book that contains step-by-step sample code for that one. It's a bit too difficult to explain thoroughly in a Stack Overflow answer.

like image 136
Cody Gray Avatar answered Oct 15 '22 05:10

Cody Gray