Thread: What's wrong with my GUI code?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    What's wrong with my GUI code?

    Code:
    #include <windows.h>
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    	static char gszClassName[ ] = "SecSuite";
    	static HINSTANCE ghInstance=NULL;
    
    int WINAPI WinMain (HINSTANCE hInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpCmdLine,
                        int nCmdShow)
    {
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
        HWND hwnd;               /* This is the handle for our window */
        MSG Msg;            /* Here messages to the application are saved */
    
    	ghInstance=hInstance;
    
        /* The Window structure */
    	wincl.cbSize=sizeof (WNDCLASSEX);
    	wincl.style=CS_DBLCLKS;/* Catch double-clicks */
    	wincl.lpfnWndProc=WndProc;/* This function is called by windows */
    	wincl.cbClsExtra=0;/* No extra bytes after the window class */
    	wincl.cbWndExtra=0;/* structure or the window instance */
    	wincl.hInstance=ghInstance;
    	wincl.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    	wincl.hCursor=LoadCursor(NULL,IDC_ARROW);
    	wincl.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
    	wincl.lpszMenuName=NULL;/* No menu */
    	wincl.lpszClassName=gszClassName;
    	wincl.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
    	{
    		MessageBox(0,"Error registering window!","Error",MB_ICONSTOP|MB_OK);
            return 0;
    	}
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               WS_EX_STATICEDGE,    /* Extended possibilites for variation */
               gszClassName,         /* Classname */
               "Woot",              /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               320,                 /* The programs width */
               240,                 /* and height in pixels */
               NULL,                /* The window is a child-window to desktop */
               NULL,                /* No menu */
               ghInstance,          /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
    	if(hwnd==NULL)
    	{
    		MessageBox(0,"Window creation failed!","Error",MB_ICONSTOP|MB_OK);
    		return 0;
    	}
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nCmdShow);
    	UpdateWindow(hwnd);
    
        while(GetMessage(&Msg,NULL,0,0))
        {
            TranslateMessage(&Msg);/* Translate virtual-key msgs into char msgs */
            DispatchMessage(&Msg);/* Send message to WindowProcedure */
        }
    	return Msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	HWND hButton,hCombo,hEdit,hList,hScroll,hStatic;
    	HDC hdc;
    	PAINTSTRUCT ps;
    	LPSTR szMessage="^_^";
        switch (Message)/* handle the messages */
        {
    		case WM_CREATE:
    			hButton=CreateWindowEx(NULL,"Button","Button Example",WS_BORDER|
    				WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,0,0,100,30,hwnd,NULL,
    				ghInstance,NULL);
    			hCombo=CreateWindowEx(NULL,"ComboBox","Cj is the ........",WS_BORDER|
    				WS_CHILD,WS_VISIBLE,CBS_DROPDOWNLIST,0,30,100,100,hwnd,NULL,
    				ghInstance,NULL);
    			hEdit=CreateWindowEx(NULL,"Edit","edit box example",WS_BORDER|
    				WS_CHILD|WS_VISIBLE,0,60,100,30,hwnd,NULL,ghInstance,NULL);
    			hList=CreateWindowEx(NULL,"ListBox",">.<",WS_BORDER|WS_CHILD|
    				WS_VISIBLE,100,0,100,200,hwnd,NULL,ghInstance,NULL);
    			hScroll=CreateWindowEx(NULL,"ScrollBar","",WS_BORDER|WS_CHILD|
    				WS_VISIBLE|SBS_VERT,210,0,100,200,hwnd,NULL,ghInstance,NULL);
    			hStatic=CreateWindowEx(NULL,"Static","",WS_BORDER|WS_CHILD|
    				WS_VISIBLE|SS_BLACKRECT,0,90,100,30,hwnd,NULL,ghInstance,NULL);
    			break;
    		case WM_PAINT:
    			hdc=BeginPaint(hwnd,&ps);
    			TextOut(hdc,70,50,szMessage,strlen(szMessage));
    			EndPaint(hwnd,&ps);
    			break;
            case WM_CLOSE:
    			DestroyWindow(hwnd);
    			break;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			break;
            default:/* for messages that we don't deal with */
                return DefWindowProc(hwnd, Message, wParam, lParam);
        }
        return 0;
    }
    All the errors are happining when It gets to case WM_CREATE (near the end) it's making no sense to me... I did the tutorial exactly how it told me to

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Try this. I made a few corrections, mainly writing ghInstance correctly and getting the bit masks to CreateWindow right.

    Code:
    #include <windows.h>
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    static char gszClassName[ ] = "SecSuite";
    static HINSTANCE ghInstance=NULL;
    
    int WINAPI WinMain (HINSTANCE hInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpCmdLine,
                        int nCmdShow)
    {
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
        HWND hwnd;               /* This is the handle for our window */
        MSG Msg;            /* Here messages to the application are saved */
    
        ghInstance=hInstance;
    
        /* The Window structure */
        wincl.cbSize=sizeof (WNDCLASSEX);
        wincl.style=CS_DBLCLKS;/* Catch double-clicks */
        wincl.lpfnWndProc=WndProc;/* This function is called by windows */
        wincl.cbClsExtra=0;/* No extra bytes after the window class */
        wincl.cbWndExtra=0;/* structure or the window instance */
        wincl.hInstance=ghInstance;
        wincl.hIcon=LoadIcon(NULL,IDI_APPLICATION);
        wincl.hCursor=LoadCursor(NULL,IDC_ARROW);
        wincl.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
        wincl.lpszMenuName=NULL;/* No menu */
        wincl.lpszClassName=gszClassName;
        wincl.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
        {
            MessageBox(0,"Error registering window!","Error",MB_ICONSTOP|MB_OK);
            return 0;
        }
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
            WS_EX_STATICEDGE,    /* Extended possibilites for variation */
            gszClassName,         /* Classname */
            "Woot",              /* Title Text */
            WS_OVERLAPPEDWINDOW, /* default window */
            CW_USEDEFAULT,       /* Windows decides the position */
            CW_USEDEFAULT,       /* where the window ends up on the screen */
            320,                 /* The programs width */
            240,                 /* and height in pixels */
            NULL,                /* The window is a child-window to desktop */
            NULL,                /* No menu */
            ghInstance,          /* Program Instance handler */
            NULL                 /* No Window Creation data */
            );
    
        if(hwnd==NULL)
        {
            MessageBox(0,"Window creation failed!","Error",MB_ICONSTOP|MB_OK);
            return 0;
        }
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        while(GetMessage(&Msg,NULL,0,0))
        {
            TranslateMessage(&Msg);/* Translate virtual-key msgs into char msgs */
            DispatchMessage(&Msg);/* Send message to WindowProcedure */
        }
        return Msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
        HWND hButton,hCombo,hEdit,hList,hScroll,hStatic;
        HDC hdc;
        PAINTSTRUCT ps;
        LPSTR szMessage="^_^";
        switch (Message)/* handle the messages */
        {
        case WM_CREATE:
            hButton=CreateWindowEx(NULL,"Button","Button Example",WS_BORDER|
                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,0,0,100,30,hwnd,  NULL,
                ghInstance,NULL);
            hCombo=CreateWindowEx(NULL,"ComboBox","Cj is the ........",WS_BORDER|
                WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,0,30,100,100,  hwnd,NULL,
                ghInstance,NULL);
            hEdit=CreateWindowEx(NULL,"Edit","edit box example",WS_BORDER|
                WS_CHILD|WS_VISIBLE,0,60,100,30,hwnd,NULL,ghInstance,NULL);
            hList=CreateWindowEx(NULL,"ListBox",">.<",WS_BORDER|WS_CHILD|
                WS_VISIBLE,100,0,100,200,hwnd,NULL,ghInstance,NULL  );
            hScroll=CreateWindowEx(NULL,"ScrollBar","",WS_BORDER|WS_CHILD|
                WS_VISIBLE|SBS_VERT,210,0,100,200,hwnd,NULL,ghInstance,NULL);
            hStatic=CreateWindowEx(NULL,"Static","",WS_BORDER|WS_CHILD|
                WS_VISIBLE|SS_BLACKRECT,0,90,100,30,hwnd,NULL,ghInstance,NULL);
            break;
        case WM_PAINT:
            hdc=BeginPaint(hwnd,&ps);
            TextOut(hdc,70,50,szMessage,strlen(szMessage));
            EndPaint(hwnd,&ps);
            break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:/* for messages that we don't deal with */
            return DefWindowProc(hwnd, Message, wParam, lParam);
        }
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Thanks, I appreciate that alot.
    |
    |
    V
    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    static char szClassName[ ] = "WindowsApp";
    static HINSTANCE ghInstance=NULL;
    
    int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,
                        LPSTR lpCmdLine,int nCmdShow)
    {
        HWND hwnd;
        MSG Msg;
        WNDCLASSEX wincl;
    
        ghInstance=hInstance;
    
        wincl.hInstance = hInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof (WNDCLASSEX);
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    
    
        if (!RegisterClassEx (&wincl))
        {
            MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONSTOP | MB_OK);
            return 0;
        }
    
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Security Suite",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               320,                 /* The programs width */
               240,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        if(hwnd==NULL)
        {
            MessageBox(0,"Window Creation Failed!","Error",0);
            return 0;
        }
    
        ShowWindow (hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        while (GetMessage (&Msg, NULL, 0, 0))
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HWND hButton,hCombo,hEdit,hScroll;
        HDC hdc;
        PAINTSTRUCT ps;
        LPSTR szMessage="Hey what's up?!";
        switch (message)
        {
            case WM_CREATE:
                hButton=CreateWindowEx(NULL,"Button","WooT",WS_BORDER|WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                262,181,50,25,hwnd,NULL,ghInstance,NULL);
                hEdit=CreateWindowEx(NULL,"Edit","Edit box example",WS_BORDER|WS_CHILD|WS_VISIBLE,
                140,181,122,25,hwnd,NULL,ghInstance,NULL);
                /*hList=CreateWindowEx(NULL,"Listbox","AB CD EF",WS_BORDER|WS_CHILD|WS_VISIBLE,
                190,225,100,25,hwnd,NULL,ghInstance,NULL);
                hCombo=CreateWindowEx(NULL,"ComboBox","ComboBox",WS_BORDER|WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,
                190,250,100,25,hwnd,NULL,ghInstance,NULL);
                hScroll=CreateWindowEx(NULL,"ScrollBar","",WS_BORDER|WS_CHILD|WS_VISIBLE|SBS_VERT,
                0,0,20,207,hwnd,NULL,ghInstance,NULL);*/
                break;
            case WM_PAINT:
                hdc=BeginPaint(hwnd,&ps);
                TextOut(hdc,25,185,szMessage,strlen(szMessage));
                EndPaint(hwnd,&ps);
                break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
                break;
            case WM_DESTROY:
                PostQuitMessage (0);
                break;
            default:
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    Last edited by bikr692002; 04-08-2006 at 03:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 12:02 PM
  3. Replies: 7
    Last Post: 08-06-2004, 09:14 AM
  4. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM