C++ Create window

C++ Create window

Post by Mrfred on Sun Sep 12, 2010 5:27 pm
([msg=45603]see C++ Create window[/msg])

Hey.

Recently I started learning C++ and have become familiar with basic command line interface to run my programs. I have been using Microsoft Visual Studio to compile and run my programs.

What I wanted to start doing was to learn how to create a window so that I could make a GUI. To learn what I have before, I have been using tutorials and information that I have found online. I was wondering if there was some resource online that would show or if someone would explain to me how building a window works and the easiest way to do it. Thanks in advance.

Mrfred
Mrfred
New User
New User
 
Posts: 3
Joined: Sun Sep 12, 2010 5:20 pm
Blog: View Blog (0)


Re: C++ Create window

Post by mischief on Sun Sep 12, 2010 8:02 pm
([msg=45614]see Re: C++ Create window[/msg])

you will need to decide on the toolkit/api you want to use. there are scores out there. the simplest is probably using the win32 API, but that will be in C. there are libraries that wrap native APIs in C++, however.

gtkmm is one such library, but its a wrapper around gtk (which wraps native apis..). it looks pretty simple to get the hang of: http://library.gnome.org/devel/gtkmm-tu ... ld.html.en

i hear this book is good. http://www.amazon.com/dp/157231995X/
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha
User avatar
mischief
Poster
Poster
 
Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)


Re: C++ Create window

Post by Mrfred on Sun Sep 12, 2010 8:18 pm
([msg=45617]see Re: C++ Create window[/msg])

Well I think that Microsoft incorporates win32 into their program, so I was hoping to use this. Sorry I didn't mention it in my op.

The stuff I was seeing and was confused about was something like this:

Code: Select all
//---------------------------------------------------------------------------
#include <windows.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
//---------------------------------------------------------------------------
LPCTSTR ClsName = L"FundApp";
LPCTSTR WndName = L"Resources Fundamentals";
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
               WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
   MSG        Msg;
   HWND       hWnd;
   WNDCLASSEX WndClsEx;

   // Create the application window
   WndClsEx.cbSize        = sizeof(WNDCLASSEX);
   WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
   WndClsEx.lpfnWndProc   = WndProcedure;
   WndClsEx.cbClsExtra    = 0;
   WndClsEx.cbWndExtra    = 0;
   WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
   WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
   WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
   WndClsEx.lpszMenuName  = NULL;
   WndClsEx.lpszClassName = ClsName;
   WndClsEx.hInstance     = hInstance;
   WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

   // Register the application
   RegisterClassEx(&WndClsEx);

   // Create the window object
   hWnd = CreateWindowEx(0,
                          ClsName,
                          WndName,
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);
   
   // Find out if the window was created
   if( !hWnd ) // If the window was not created,
      return FALSE; // stop the application

   // Display the window to the user
   ShowWindow(hWnd, nCmdShow);// SW_SHOWNORMAL);
   UpdateWindow(hWnd);

   // Decode and treat the messages
   // as long as the application is running
   while( GetMessage(&Msg, NULL, 0, 0) )
   {
             TranslateMessage(&Msg);
             DispatchMessage(&Msg);
   }

   return Msg.wParam;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
            WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
    case WM_DESTROY:
        PostQuitMessage(WM_QUIT);
        break;
    default:
        // Process the left-over messages
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    // If something was not done, let it go
    return 0;
}
//---------------------------------------------------------------------------


I was not introduced to most of these functions, so I was thinking if I knew what all these functions did, I would be able to create my own windows for my own purposes. THanks a lot for the reply, and I will look further into that link when I have more time.

Thanks
Mrfred
Mrfred
New User
New User
 
Posts: 3
Joined: Sun Sep 12, 2010 5:20 pm
Blog: View Blog (0)


Re: C++ Create window

Post by mischief on Sun Sep 12, 2010 8:23 pm
([msg=45618]see Re: C++ Create window[/msg])

Microsoft, et al, is the company who wrote the windows API. they don't merely 'incorporate' it.
surprisingly, Microsoft does actually have some documentation. i suggest you become very familiar with MSDN.
http://msdn.microsoft.com/en-us/library/ee663300(VS.85).aspx
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha
User avatar
mischief
Poster
Poster
 
Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests