NO

Author Topic: Is there any easier abroad to made gui in C?  (Read 483 times)

Offline cnoob

  • Member
  • *
  • Posts: 15
Is there any easier abroad to made gui in C?
« on: September 06, 2018, 01:22:03 pm »
I'm not used to this forum yet. I don't see anyone replied to my post so I add this as final post. If still no one interested, I should quit. Is this forum dead also?  :-\

The main problem of mine: I found it's difficult for my brain to remember and write a very long but unnatural code in Win32 API gui. I learned from examples from Zetcode and TheForge. Is there any way to archive the same result with the shorter code to write and lesser functions to remember? I found SGL project from this forum which does just this but it seemed it's dead  :-[ On the C++ side there is Win32++ with I'm very pleased but my lack of C++ skill prevent me from digging further.

I tried and successfully build and run a GTK+ example from a guide also on this forum with Pelles C. I found GTK+ is not a solution because of it dependency hell. It support best only GCC/MingW family compiler with the aid of pkg-config, unfortunately pkg-config doesn't support Pelles C. I've to find any report of the compiler complains missing lib, missing header and add it manually from the menu, very slow and time consuming.

I tried Nuklear but I could say it's completely trash. Doesn't works with any compiler MingW including not only Pelles C.

I looked at IUP but didn't what to download and how to integrate it with Pelles C either  :'(

You guys are programmed in Win32 for a very long time could be before my own birth (I'm young) doesn't this problem you already thought of and found a solution that made your life as a GUI programmer easier, I wonder?  :( 

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 1654
Re: Is there any easier abroad to made gui in C?
« Reply #1 on: September 07, 2018, 10:03:48 am »
I'm not used to this forum yet. I don't see anyone replied to my post so I add this as final post. If still no one interested, I should quit. Is this forum dead also?  :-\
People in this forum are not just waiting for someone to knock.
Most of us are professionals, and we are busy sometime. So may happen that you have to wait a while for an answer.

The main problem of mine: I found it's difficult for my brain to remember and write a very long but unnatural code in Win32 API gui. I learned from examples from Zetcode and TheForge. Is there any way to archive the same result with the shorter code to write and lesser functions to remember?
You wrote that you're young, there should be a problem if you can't manage memory...  ;D
I joke, now seriously: the C programming is so. C is the more high level of low level languages. That's why it is used for system programming. It's main characteristics are low level access and portability.
Even if you will find almost only PHD declarations around and language sophistic discussions on purity of syntax and semantic (read answers on stackoverflow for example), it is used because with it you can do those dirty things, when necessary (and if you know how-to of course), which are needed to system programmers.
So if you want some graphic programming support like VB, or some other tools alike, choosing C language you mistaken your choice.
C is as is, all you can need have to be created from scratch. Of course there are a lot of libraries for almost all needs, but nothing so automated to exclude manual trimming on each application.

You guys are programmed in Win32 for a very long time could be before my own birth (I'm young) doesn't this problem you already thought of and found a solution that made your life as a GUI programmer easier, I wonder?  :(
For each job there is always the right tool! 8)
Can't be so quick with C or whatever language (yes even VB).
Anyway if you want something fast and dirty you can find other tools (have you tried .net? C#?).

Offline jj2007

  • Member
  • *
  • Posts: 490
Re: Is there any easier abroad to made gui in C?
« Reply #2 on: September 09, 2018, 01:42:50 am »
Below a small template (88 lines) for coding a GUI application with Pelles C. It has a menu and an edit control, and you can read a text file into that control.

Windows GUI programming is neither easy nor complicated. You simply have to study the essential stuff. The old Petzold book is a good starter, and MSDN is your friend.

Code: [Select]
#include <windows.h> // simple GUI template
#include <stdio.h> // needed for fopen
#pragma comment(linker, "gdi32.lib comdlg32.lib comctl32.lib") // needed for GetOpenFileName, GetStockObject and InitCommonControlsEx
#pragma warn(disable:2216 2118)    // retval never used, parameter .. is not referenced
#define winX 44 // main window x, y, width and height
#define winY 44
#define winW 888
#define winH 666
void ReadFile2EditControl(char* filename, HWND hEdit) { // does what its name says
  FILE *ofp = fopen(filename, "rb");
  if (ofp) {
fseek(ofp, 0, SEEK_END); // GetFileSize() won't work, so let's do it the archaic C way
int fLen=ftell(ofp); // position at end equals length of file
fseek(ofp, 0, SEEK_SET); // go back to start
PSTR pstrBuffer=malloc(fLen+1); // buffer includes a zero delimiter
if (pstrBuffer) {
   fread(pstrBuffer, 1, fLen, ofp); // read content into buffer
   pstrBuffer[fLen] = '\0'; // set the delimiter explicitly
   SetWindowText(hEdit, pstrBuffer) ; // put text into the edit control
   free(pstrBuffer);
}
fclose(ofp);
  }
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  switch(msg) {
case WM_CREATE:
{
#define mOpen 101
#define mSave 102
HMENU hMenu=CreateMenu(); // create the main menu
HMENU hSubMenu=CreatePopupMenu(); // create a sub-menu
AppendMenu(hMenu, MF_POPUP, (int)hSubMenu, "&File"); // add it to the main menu
AppendMenu(hSubMenu, MF_STRING, mOpen, "&Open"); // and add
AppendMenu(hSubMenu, MF_STRING, mSave, "&Save"); // two items
SetMenu(hwnd, hMenu); // attach menu to main window
HWND hEdit=CreateWindowEx(WS_EX_CLIENTEDGE, "edit", NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | ES_MULTILINE,
2, 1, winW-12, winH-GetSystemMetrics(SM_CYMENU)-33, hwnd, (HMENU) 103, GetModuleHandle(0), NULL); // we have added an edit control
SendMessage(hEdit, EM_LIMITTEXT, 0, 0); // no limit
SetFocus(hEdit); // make sure you can start typing right away
SendMessage(hEdit, WM_SETFONT, (int)GetStockObject(ANSI_FIXED_FONT), 0); // SYSTEM_FIXED_FONT is bold and larger
}
break;

case WM_COMMAND:
if ((short)wParam==mOpen) {
char fileName[MAX_PATH]="\x0000";
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter ="Sources and Headers\0*.c;*.h;*.asm\0\0";
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (GetOpenFileName(&ofn)) ReadFile2EditControl(fileName, GetDlgItem(hwnd, 103));
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
  MSG msg; WNDCLASS wc;
  wc.style = CS_HREDRAW | CS_VREDRAW;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.lpszClassName = "MyClass";
  wc.hInstance = hInstance;
  wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE+1);
  wc.lpszMenuName = NULL;
  wc.lpfnWndProc = WndProc;
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  RegisterClass(&wc);
  CreateWindow(wc.lpszClassName, "Hello World",
WS_OVERLAPPEDWINDOW | WS_VISIBLE | nCmdShow,
winX, winY, winW, winH,
NULL, NULL, hInstance, NULL);
  while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
  return (int) msg.wParam;
}

Offline Robert

  • Member
  • *
  • Posts: 186
Re: Is there any easier abroad to made gui in C?
« Reply #3 on: September 09, 2018, 06:31:41 am »
The line
Code: [Select]
#pragma comment(linker, "gdi32.lib comdlg32.lib comctl32.lib") // needed for GetOpenFileName, GetStockObject and InitCommonControlsEx

should include user32.lib, like this

Code: [Select]
#pragma comment(linker, "user32.lib gdi32.lib comdlg32.lib comctl32.lib") // needed for GetOpenFileName, GetStockObject and InitCommonControlsEx

Otherwise, this happens

POLINK: error: Unresolved external symbol '__imp__SetWindowTextA@8' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__GetDlgItem@8' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__CreateMenu@0' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__CreatePopupMenu@0' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__AppendMenuA@16' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__SetMenu@8' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__GetSystemMetrics@4' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__CreateWindowExA@48' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__SendMessageA@16' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__SetFocus@4' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__PostQuitMessage@4' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__DefWindowProcA@16' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__LoadCursorA@8' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__LoadIconA@8' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__RegisterClassA@4' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__TranslateMessage@4' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__DispatchMessageA@4' - referenced from 'snip.obj'.
POLINK: error: Unresolved external symbol '__imp__GetMessageA@16' - referenced from 'snip.obj'.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1745
Re: Is there any easier abroad to made gui in C?
« Reply #4 on: September 09, 2018, 09:53:43 am »
Just for faster compiling
Code: [Select]
#define WIN32_LEAN_AND_MEAN // faster compiling
#include <windows.h> // simple GUI template
#include <commdlg.h> // for GetOpenFileName() ...
#include <stdio.h> // needed for fopen
#include <stdlib.h> // for malloc
#pragma comment(linker, "user32.lib gdi32.lib comdlg32.lib comctl32.lib") // needed for GetOpenFileName, GetStockObject and InitCommonControlsEx
Wizards could be good help for beginners, starting with dialog-based GUI application and using resource editor.
VB users are used to use 'handlers', so C example could use subroutines for command handlers. (OnCreate(), OnCommand(), OnClose())
Add-In like https://forum.pellesc.de/index.php?topic=2486.msg9415#msg9415 or similar helps with it.
« Last Edit: September 09, 2018, 09:59:19 am by TimoVJL »
May the source be with you

Offline jj2007

  • Member
  • *
  • Posts: 490
Re: Is there any easier abroad to made gui in C?
« Reply #5 on: September 10, 2018, 05:09:15 am »
The line
Code: [Select]
#pragma comment(linker, "gdi32.lib comdlg32.lib comctl32.lib") // needed for GetOpenFileName, GetStockObject and InitCommonControlsEx

should include user32.lib, like this

Code: [Select]
#pragma comment(linker, "user32.lib gdi32.lib comdlg32.lib comctl32.lib") // needed for GetOpenFileName, GetStockObject and InitCommonControlsEx

That's right. I have kernel32 user32 in the linker's commandline (I don't use the IDE but rather a batch file). My fault :(

Offline henrik

  • Member
  • *
  • Posts: 4
Re: Is there any easier abroad to made gui in C?
« Reply #6 on: September 21, 2018, 09:03:35 pm »
As stated by cnoob, SGL was designed to simplify Win32 programming

The project is not dead; it just come back to life !
- a minor update is here (version 1.3.2)
- porting to PellesC 9 is on the way

look here : https://forum.pellesc.de/index.php?topic=6788.0
and here : http://perso.numericable.fr/hserindat/sgl/

Offline sgraffe

  • Member
  • *
  • Posts: 20
Re: Is there any easier abroad to made gui in C?
« Reply #7 on: October 09, 2018, 07:24:41 pm »
Henrik:
My programs are plain vanilla C, and I opened them in the new Pelles C 9.0 (from 8.0) with no problem whatsoever. When you say you are porting SGL to Pelles C 9, do you expect them to need adjustments, what kind of adjustments?

Simon

Offline cnoob

  • Member
  • *
  • Posts: 15
Re: Is there any easier abroad to made gui in C?
« Reply #8 on: October 12, 2018, 08:02:54 am »
There're not many libraries for GUI on C because the nature of GUI interlinked with OOP. Sadly, I checked most options now is for Unix/X11 first, on Windows it depends on posix shell like msys2 to work (not even work with plain mingw with no msys shell): gtk3, agar, iup... I don't want to spend some GBs of disk space to install all of this (thing I don't know and don't want to invest on) so I gave up.

WinAPI based thin wrapper like SGL helps thing a lot but it's still very difficult for non-professional like me. I gave up on GUI with C completely and since now I only write cli  :-[
« Last Edit: October 13, 2018, 04:40:22 pm by cnoob »

Offline DMac

  • Member
  • *
  • Posts: 250
Re: Is there any easier abroad to made gui in C?
« Reply #9 on: October 15, 2018, 05:27:32 am »
There are a number of tools that I like to use that make standard windows Gui programming fairly easy.

1) When I begin the project I'll use the "Windows Application" wizard and select the "dialog based program" option.  This will create some initial code including a dialog in the resource editor that I can modify in the dialog editor.

2) I use https://www.codeproject.com/Articles/4948/Message-Cracker-Wizard-for-Win-SDK-Developers to make the window procedure, message cracking macros, and message processing functions.  If I am writing a windows procedure for a dialog based application I'll change HANDLE_MSG to HANDLE_DLGMSG in the generated code.

3) If I want to handle button clicks I'll use the WM_COMMAND message cracker macro
Code: [Select]
HANDLE_DLGMSG(hwndDlg, WM_COMMAND, Main_OnCommand);
and then break out the events in the WM_COMMAND message processing function like so:
Code: [Select]
static VOID Main_OnCommand(HWND hwnd, INT id, HWND hwndCtl, UINT codeNotify)
{
    switch (id)
    {
        case CMD_OPEN:
            CmdOpen_Click(hwnd);
            break;
        case CMD_SAVE:
            CmdSave_Click(hwnd);
            break;
           //etc...

and so:
Code: [Select]
void CmdSave(HWND hwnd)
{
     // TODO: Save code here
}

These tools take a lot of guess work out of GUI coding and were a great help to me when I was just beginning.

No one cares how much you know,
until they know how much you care.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1745
Re: Is there any easier abroad to made gui in C?
« Reply #10 on: October 16, 2018, 05:32:14 pm »
And if you get bored with WindowsX.h, check this:
https://forum.pellesc.de/index.php?topic=2486.0
and tune it for a your needs.
May the source be with you