View Full Version: How to write a short GUI

C++ Learning Community > C++ Tips > How to write a short GUI


Title: How to write a short GUI
Description: Can anyone make a shorter one?


TheHawgMaster - August 13, 2003 11:30 PM (GMT)
Here's just about the shortest GUI you can write that has a window and all:
CODE
#include <Windows.h>
PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
   WNDCLASS W;
   memset(&W, 0, sizeof(W));
   W.hInstance = GetModuleHandle(0);
   W.lpszClassName = "W";
   W.lpfnWndProc = DefWindowProc;
   RegisterClass(&W);
   HWND Window = CreateWindow("W", "ShortGUI", WS_OVERLAPPEDWINDOW, 0, 0, 320, 240, 0, 0, GetModuleHandle(0), 0);
   ShowWindow(Window, SW_SHOW);
   while(GetAsyncKeyState(VK_ESCAPE) ? 0 : 1) {
       MSG msg;
       if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
           TranslateMessage(&msg);
           DispatchMessage(&msg);
       } // end if
   } // end while
} // end WinMain
That should work, but I didn't compile it so tell me if there are any errors.

FHCandyman - August 14, 2003 12:16 AM (GMT)
my compiler(Dev-C++) doesnt even go through a real compile time, it just says error on 'line 2 - unable to run program file' the moment i attempt to compile.

FHCandyman - August 14, 2003 12:19 AM (GMT)
this appears to be something wrong with my compiler, since i cant even compile Hello World. I'll try reinstalling Dev-C++ and then compiling your program B)

ih8censorship - August 14, 2003 12:27 AM (GMT)
NO DONT TRY TO RE INSTALL! theres something wrong with the code. i tryed vc++5 and dev c++ and nothing works. he said he dident compile it so its the code not your compiler.

FHCandyman - August 14, 2003 12:50 AM (GMT)
well i cant get anything to compile so I'm supposing I have a compiler problem anyway, unless the code ruined my compiler but I doubt it.

ih8censorship - August 14, 2003 12:54 AM (GMT)
try re starting your computer that usualy seems to help it kills all the windows bugs for a few minutes

TheHawgMaster - August 14, 2003 04:40 AM (GMT)
Very... Odd. You said on line 2? Maybe your compilers don't like the stack-reserve arguments to WinMain() try putting in place-holder names for the arguments to WinMain() and then maybe it would work. (I don't see why it should be a problem though, it's a perfectly legal syntax)

FHCandyman - August 14, 2003 09:56 AM (GMT)
restarted my comp and still cant compile anything, dont worry i think its just my compiler gone funky. Dev-C++ does that to me sometimes, which is why I want to buy a compiler, I'm hoping buying one and spending money will maybe stop the compiler errors :lol: anyway i'll test the code once i get around to reinstalling Dev-C++

FHCandyman - August 14, 2003 10:02 AM (GMT)
i read in your other post(I believe it was you) that you have to compile as a GUI, I'd like to do that but I can't find how. How do you compile as a GUI on Dev-C++?

dr voodoo - August 14, 2003 12:45 PM (GMT)
It works for me but I didn't try with Dev-C++.
ih8 try replacing line 2 with:
CODE
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)


But when I close the window (by pushing the X), the program is still running because in that case it didn't break out of the message loop!

QUOTE
Here's just about the shortest GUI you can write that has a window and all:

Here's a shorter one with window :P
CODE
#include <Windows.h>
PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
      MessageBox(NULL,"Hello","Hello",MB_OK);
      return 0;
}

TheHawgMaster - August 14, 2003 03:25 PM (GMT)
Dr. Voodoo,

Press <escape> to quit. And I meant a window that you could access the handle to.

TheHawgMaster

Dragon - August 14, 2003 08:21 PM (GMT)
QUOTE
i read in your other post(I believe it was you) that you have to compile as a GUI, I'd like to do that but I can't find how. How do you compile as a GUI on Dev-C++?


I think you're talking about me. Go to Project Options and select Win32 GUI.

dsdeck - November 19, 2003 01:40 AM (GMT)
I have Dev C++ 4.9.8.4 beta and I can't compile much of anything!!
Can't even compile this:

#include <stdin.h>
main()
{
printf("Hello World\n");
}

(from the manual)
It can't find stdin.h either

Dragon - November 19, 2003 01:50 AM (GMT)
Isn't that C?

TheHawgMaster - November 19, 2003 02:20 AM (GMT)
stdio not stdin

Dixon - November 19, 2003 12:58 PM (GMT)
I have Dev C++ and I have never had much trouble with it (I didn't try this code yet) but some compilers need things slightly different then others and if you are trying to compiler a GUI as a console you might not get to far.

Incubator - November 19, 2003 10:24 PM (GMT)
heres a short one:
CODE


#include <qapplication.h>
#include <qmainwindow.h>

int main(int argc, char* argv[]) {
QApplication a(argc, argv);
QMainWindow *window = new QMainWindow("Hello World");
a.setMainWidget(*window);
window->setSize(200,200);
window->show();
return a.exec();
}


ih8censorship - November 23, 2003 07:39 PM (GMT)
lets see those headers too :P they dont count i dont think cause there not standard headers where windows.h is........

Incubator - November 23, 2003 10:47 PM (GMT)
qapplication.h is the standard for the widely known Qt framework wich is cross=platfor;
and so it does count
unless you want me to start writing something in XLib (it includes xlib.h wich is the equivalent of windows.h)
http://users.skynet.be/incubator/qapplication.h
http://users.skynet.be/incubator/qmainwindow.h
it makes gui development in c++ so easy :)

Dragon - November 24, 2003 03:58 AM (GMT)
Even if the Qt framework is widely known most of the people here do not use it, so it would have helped if you said you used the Qt framework and not the API, MFC, etc.

Incubator - November 24, 2003 11:13 PM (GMT)
what about this:
CODE

#include <stdio.h>
#include <X11/Xlib.h>

int main (int argc, char *argv[])
{
Display              *dpy;
Visual               *visual;
int                   depth;
XSetWindowAttributes  attributes;
Window                win;
XKeyEvent event;

dpy = XOpenDisplay(NULL);
visual = DefaultVisual(dpy, 0);
depth  = DefaultDepth(dpy, 0);
attributes.background_pixel = XWhitePixel(dpy, 0);
/* create the application window */
win = XCreateWindow(dpy, XRootWindow(dpy, 0),50, 50, 400, 400, 5, depth,
  InputOutput, visual, CWBackPixel,
  &attributes);
XSelectInput(dpy, win, ExposureMask | KeyPressMask |
  ButtonPressMask | StructureNotifyMask);

XMapWindow(dpy, win);

/* run till key press */
while(1){
 XNextEvent(dpy, &event);
 switch(event.type) {
  case KeyPress:
   XCloseDisplay(dpy);
   exit(0);
   break;
  default:
   break;
 }
}
return(0);
}

now tell me, does this make more sense then the winapi or not?

nickeax - December 29, 2003 02:54 PM (GMT)
That looks very nice, with good programming structure. Seems like it'd be hard to put security holes in that!



TheHawgMaster - December 29, 2003 06:54 PM (GMT)
Heh that's why I might move to Linux.

Dante Shamest - January 5, 2004 09:04 AM (GMT)
With resource scripts, you can easily make GUI programs in Windows.
Its nice because the graphical side of the program is separated from the program flow. Even if you don't have MSVC++ resource scripts aren't hard to code by hand. They're like CSS in HTML.

Here's a simple resource script that defines a window...

Resource Script
CODE

#include <windows.h>
MainDialog DIALOG 0, 0, 100, 100
CAPTION "My Window"
STYLE WS_OVERLAPPEDWINDOW | WS_VISIBLE
{}


And here's the code that calls it.

Program Code
CODE

#include <windows.h>

BOOL CALLBACK MainDialogProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
{
   switch( msg )
   {
       case WM_INITDIALOG:  return 1;
       case WM_CLOSE:       EndDialog( hwnd, 0 );
       default:             return 0;
   }
}


int WINAPI WinMain(HINSTANCE hThis, HINSTANCE hPrev, LPSTR nCmdLine, int nShow)
{
   return DialogBox( hThis, "MainDialog", 0, MainDialogProc );
}

Stuy GM - January 5, 2004 09:22 AM (GMT)
QUOTE
well i cant get anything to compile so I'm supposing I have a compiler problem anyway, unless the code ruined my compiler but I doubt it.

Dude, i had the same problem, and nothing worked after reinstalling, it was on line two, i had to resort to installing Dev C++ 4




* Hosted for free by InvisionFree