Thread: creating window interfaces

  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    46

    creating window interfaces

    hey all,


    so, Ive been practicing C for a while now,


    i know the basic syntax like char, int, arrays, strcat, strcpy, itoa and the loops




    so the next thing i want to learn is how to open up a window interface, i have tried to google it, but cant exactly find out how to do it,


    can anyone help?
    Last edited by WaterSerpentM; 10-23-2023 at 05:37 PM.

  2. #2
    Registered User
    Join Date
    Jul 2022
    Posts
    46
    would anyone recommend this?

    Introduction to C & GUI Programming


    Introduction to C & GUI Programming: Amazon.co.uk: Simon Long: 9781912047659: Books

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    939
    I'm not familiar with that book, but just be aware that it's for GTK rather than for native Windows GUI programming. GTK does work on Windows but it might be more challenging to set up. But it can be an advantage since you can learn a cross-platform GUI toolkit rather than being tied down to just Windows (especially as the world moves away from Windows to other operating systems).

  4. #4
    Registered User
    Join Date
    Jul 2022
    Posts
    46
    Quote Originally Posted by christop View Post
    I'm not familiar with that book, but just be aware that it's for GTK rather than for native Windows GUI programming. GTK does work on Windows but it might be more challenging to set up. But it can be an advantage since you can learn a cross-platform GUI toolkit rather than being tied down to just Windows (especially as the world moves away from Windows to other operating systems).
    is there one for native windows GUI?
    Last edited by WaterSerpentM; 10-24-2023 at 03:49 PM.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Jul 2022
    Posts
    46
    thanks,

    but is that c++?

    I am using C

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Why do you think it's C++?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    Jul 2022
    Posts
    46
    Quote Originally Posted by john.c View Post
    Why do you think it's C++?
    because it says C++ at the top of the code

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    It's C.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,652
    > because it says C++ at the top of the code
    You need to be able to tell the difference between them.

    An HTML formatting box with a language tag of C++ doesn't know or care what is posted within it.

    Which compiler / editor / IDE are you using?
    For example, visual studio defaults to giving you C++ projects and files. You have to do a bit of extra work to keep it to being C only.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Feb 2019
    Posts
    1,075
    I don't know any modern good books about Win32 GUI programming. I recommend an old one: Charles Petzold's Windows Programming (for Windows 95 or an older one: for Windows 3.1 - really outdated, but the basis is still there).

    Essentially, your programming will shift from a sequential point of view to an assynchronous one (where your application responds to "messages").

  12. #12
    Registered User
    Join Date
    Feb 2019
    Posts
    1,075
    Here's the usual "Hello, World" example for Win32 GUI:
    Code:
    /* main.c */
    #include <windows.h>
    
    /* Windows procedure (will deal with the messages) */
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    /* the main() procedure for Windows Apps */
    int WINAPI WinMain(HINSTANCE hInst,
                         HINSTANCE hPrevInst,
                         LPSTR lpszCmdLine,
                         int nCmdShow)
    {
      /* your window must have a "classname" */
      const static LPCSTR szClassName = "SimpleAppClass";
    
      HWND hWnd;
      MSG msg;
    
      /* An Window class have attributes for your window, including its class name */
      WNDCLASS wc =
      {
        .style = CS_HREDRAW | CS_VREDRAW,      // redraw window if there's WIDTH or HEIGHT changes.
        .lpfnWndProc = WndProc,                // pointer to the window procedure.
        .hInstance = hInst,                    // "instance" is a handler for your "process".
        .hIcon = LoadIcon(NULL, IDI_APPLICATION),
        .hCursor = LoadCursor(NULL, IDC_ARROW),
        .hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1),
        .lpszClassName = szClassName
      };
    
      // register your class in Windows.
      RegisterClass(&wc);
    
    
      /* These window styles will create a 'normal' Window,
         which cannot be maximized or its area changed. */
      #define WINDOW_STYLE WS_OVERLAPPED  | \
                           WS_SYSMENU     | \
                           WS_VISIBLE     | \
                           WS_BORDER      | \
                           WS_MINIMIZEBOX | \
                           WS_CAPTION
    
      // Finally, ask Windows to create the window.
      // this will send WM_CREATE message to the window procedure.
      hWnd = CreateWindowEx( 0,
                             szClassName,
                             "My Simple App",             // Window "title".
                             WINDOW_STYLE,
                             CW_USEDEFAULT, CW_USEDEFAULT,
                             320, 100,
                             NULL, NULL,
                             hInst,
                             NULL );
    
      if ( ! hWnd )
      {
        /* If cannot create Window, show to the user and exit app. */
        MessageBox( NULL,
                    "Erro ao tentar criar a janela da aplicação.",
                    "Erro",
                    MB_OK | MB_ICONERROR);
    
        return 0;
      }
    
      // Makes window visible, sending messages like WM_PAINT.
      UpdateWindow( hWnd );
      ShowWindow( hWnd, SW_SHOW );
    
      // Process enqueued messages for our window.
      while ( GetMessage( &msg, NULL, 0, 0 ) )
      {
        TranslateMessage( &msg );  // keyboard messages must be translated!
    
        DispatchMessage( &msg );  // dispatch enqueued messages to our window procedure.
      }
    
      // if received WM_QUIT, the loop ends and we return the errorcode.
      return msg.wParam;
    }
    Code:
    // winproc.c
    
    #include <windows.h>
    
    // Our simple Window Procedure.
    LRESULT CALLBACK WndProc( HWND hWnd,
                              UINT uMsg,
                              WPARAM wParam,
                              LPARAM lParam )
    {
      PAINTSTRUCT ps;
      RECT rc;
    
      switch (uMsg)
      {
      // When our window is destroyed, WM_DESTROY should be processed.
      case WM_DESTROY:
        PostQuitMessage(0);   // Enqueue WM_QUIT message.
        return 0;
    
      // When Windows needs to paint a Windows, WM_PAINT is sent to our procedure.
      case WM_PAINT:
        GetClientRect( hWnd, &rc );
        BeginPaint( hWnd, &ps );
          DrawText( ps.hdc, "Hello, world!", -1, &rc, DT_CENTER | DT_VCENTER );
        EndPaint( hWnd, &ps );
        return 0;
      }
    
      // Ask Windows to Deal with other messages.
      return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    Code:
    # Makefile for mingw-w64 (linux)... Change
    # CC and LDLIBS for MSYS2 or Cygwin (or change the Makefile for nmake, cl.exe and link.exe - for Visual Studio).
    
    CC=x86_64-w64-mingw32-gcc
    CFLAGS=-O2 -march=native -fomit-frame-pointer -fcf-protection=none
    LDFLAGS=-s -Wl,-subsystem=windows
    LDLIBS=-lkernel32 -luser32 -lgdi32
    
    app.exe: main.o winproc.o
        $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
    
    main.o: main.c
    winproc.o: winproc.c
    
    .PHONY: clean distclarn
    
    clean:
        -rm *.o
    
    distclean: clean
        rm app.exe

  13. #13
    Registered User
    Join Date
    Mar 2023
    Posts
    33
    Quote Originally Posted by WaterSerpentM View Post
    would anyone recommend this?

    Introduction to C & GUI Programming


    Introduction to C & GUI Programming: Amazon.co.uk: Simon Long: 9781912047659: Books
    As a linux desktop user, I really like the looks of that book, and am surprised I haven't come across it because I have looked for a book on GUI programming in C multiple times over the past year. Based on the reviews and description, it seems pretty good, at least if you are not tremendously experienced with C.

    However, I'm going to say that based on what you have said so far, there isn't a whole lot interesting you can do in C without an extensive understanding of of how to make custom headers/functions and memory management, so you should also continue to work on the text based stuff because there's a lot you can do with that. The GUI stuff can be pretty mind mindbogglingly complicated as you have to deal with windowing systems and the libraries needed to make those happen.

    thanks,

    but is that c++?

    I am using C
    C++ is C, it just has a lot more features and capabilities. The comment syntax is exactly the same, and a c++ compiler will run c programs written in the same language. C++ has iosteam.h instead of stdio.h but c++ compilers will still compile the c standard library. I don't know anything about c# though except that it tends to be used as the backend for websites.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to creating interfaces
    By rpm28 in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2007, 08:20 AM
  2. Creating a child window in a parent window
    By vopo in forum Windows Programming
    Replies: 8
    Last Post: 10-06-2007, 04:15 PM
  3. Creating a Window
    By ILoveVectors in forum Windows Programming
    Replies: 4
    Last Post: 07-13-2005, 09:46 PM
  4. Creating a New Window
    By tyouk in forum Windows Programming
    Replies: 3
    Last Post: 01-02-2005, 09:39 PM
  5. Problem with creating new window, from another window
    By Garfield in forum Windows Programming
    Replies: 6
    Last Post: 01-11-2004, 02:10 PM

Tags for this Thread