View Full Version: api coden wont work

C++ Learning Community > win32 api C++ programming > api coden wont work


Title: api coden wont work
Description: wont create second window


cloudncali - September 4, 2005 07:05 PM (GMT)
im trying to make a second window for my game and its not createing the second window. im using dev-c++. here is the code
CODE

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT ("GAMEWIN");
    static TCHAR szSecAppName[] = TEXT ("STATD");
    HWND         hwnd;
    HWND         hwnds;
    MSG          msg;
    WNDCLASS     wndclass;
    HWND hMenu;
    wndclass.style         = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc   = WndProc;
    wndclass.cbClsExtra    = 0;
    wndclass.cbWndExtra    = 0;
    wndclass.hInstance     = hInstance;
    wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = szAppName;
    wndclass.lpszClassName = szAppName;

    if (!RegisterClass (&wndclass))
    {
         MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                     szAppName, MB_ICONERROR);
         return 0;
    }
    hwnd = CreateWindow (szAppName,                  // window class name
                         TEXT ("The Game"),          // window caption
                         WS_OVERLAPPEDWINDOW,        // window style
                         CW_USEDEFAULT,              // initial x position
                         CW_USEDEFAULT,              // initial y position
                         CW_USEDEFAULT,              // initial x size
                         CW_USEDEFAULT,              // initial y size
                         NULL,                       // parent window handle
                         NULL,                       // window menu handle
                         hInstance,                  // program instance handle
                         NULL);                     // creation parameters  

    hwnds = CreateWindow ( szSecAppName,
                         TEXT("STATS"),
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT,
                          CW_USEDEFAULT,
                         150,
                         480,
                         hwnd,
                          NULL,
                         NULL,
                          NULL);
    ShowWindow (hwnd, iCmdShow);
    UpdateWindow (hwnd);
    ShowWindow(hwnds,iCmdShow);
    UpdateWindow(hwnds);  
    while (GetMessage (&msg, NULL, 0, 0))
    {
         TranslateMessage (&msg);
         DispatchMessage (&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    HDC         hdc;
    PAINTSTRUCT ps;
    RECT        rect;
    static int xx=100;
    static int yy=100;
    switch (message)
    {
    case WM_CREATE:
         return 0;
         break;
    case WM_KEYDOWN:
         switch (wParam)
         {
         case VK_UP:
              yy-=3;
              InvalidateRect(hwnd, NULL, true);
              break;

         case VK_DOWN:
              yy+=3;
             InvalidateRect(hwnd, NULL, true);
              break;
             
         case VK_LEFT:
              xx-=3;
              InvalidateRect(hwnd, NULL, true);
              break;
             
         case VK_RIGHT:
              xx+=3;
              InvalidateRect(hwnd, NULL, true);
              break;
         case VK_ESCAPE:
              PostQuitMessage (0);          
              break;
              return 0;
         }
    case WM_PAINT:
         hdc = BeginPaint (hwnd, &ps);
         MoveToEx(hdc, xx, yy, NULL);
         LineTo(hdc, xx+5, yy);
         MoveToEx(hdc, xx+5, yy, NULL);
         LineTo(hdc, xx+5, yy+5);
         MoveToEx(hdc, xx+5, yy+5, NULL);        
         LineTo(hdc, xx, yy+5);
         MoveToEx(hdc, xx, yy+5, NULL);        
         LineTo(hdc, xx, yy);
         EndPaint (hwnd, &ps);
         return 0;
         
    case WM_DESTROY:
         PostQuitMessage (0);
         return 0;
    }
    return DefWindowProc (hwnd, message, wParam, lParam);
}

for some resone the second window is not dispplayd <_<

Joel - September 4, 2005 07:40 PM (GMT)
Well:
CODE

wndclass.lpszMenuName = szAppName;
   wndclass.lpszClassName = szAppName;

Is that part ok? Do you have a class name string with the same menu resource name? :mellow:

I recommend that you use dialogs....

* My $0.02

gnschmidt - September 4, 2005 09:24 PM (GMT)
cloudncali, I don't think you need szSecAppName, and the reason the second window doesn't appear is that you specify szSecAppName as the window class. (You only define szAppName.)

All you need to do is comment out line 9 and change line 44 so that the first parameter for hwnds's CreateWindow call is szAppName, not szSecAppName.

The following works fine on my computer:

CODE

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                  PSTR szCmdLine, int iCmdShow)
{
   static TCHAR szAppName[] = TEXT ("GAMEWIN");
   // omitted: static TCHAR szSecAppName[] = TEXT ("STATD");
   HWND         hwnd;
   HWND         hwnds;
   MSG          msg;
   WNDCLASS     wndclass;
   HWND hMenu;
   wndclass.style         = CS_HREDRAW | CS_VREDRAW;
   wndclass.lpfnWndProc   = WndProc;
   wndclass.cbClsExtra    = 0;
   wndclass.cbWndExtra    = 0;
   wndclass.hInstance     = hInstance;
   wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
   wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
   wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
   wndclass.lpszMenuName = NULL; // was szAppName; see Joel's message
   wndclass.lpszClassName = szAppName;

   if (!RegisterClass (&wndclass))
   {
        MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                    szAppName, MB_ICONERROR);
        return 0;
   }
   hwnd = CreateWindow (szAppName,
                        TEXT ("The Game"),
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        NULL,
                        NULL,
                        hInstance,
                        NULL); // creation parameters  

   hwnds = CreateWindow ( szAppName, // was: szSecAppName
                        TEXT("STATS"),
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        150,
                        480,
                        hwnd,
                        NULL,
                        NULL,
                        NULL);
   ShowWindow (hwnd, iCmdShow);
   UpdateWindow (hwnd);
   ShowWindow(hwnds,iCmdShow);
   UpdateWindow(hwnds);  
   while (GetMessage (&msg, NULL, 0, 0))
   {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
   }
   return msg.wParam;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

   HDC         hdc;
   PAINTSTRUCT ps;
   RECT        rect;
   static int xx=100;
   static int yy=100;
   switch (message)
   {
   case WM_CREATE:
        return 0;
        break;
   case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_UP:
             yy-=3;
             InvalidateRect(hwnd, NULL, true);
             break;

        case VK_DOWN:
             yy+=3;
            InvalidateRect(hwnd, NULL, true);
             break;
             
        case VK_LEFT:
             xx-=3;
             InvalidateRect(hwnd, NULL, true);
             break;
             
        case VK_RIGHT:
             xx+=3;
             InvalidateRect(hwnd, NULL, true);
             break;
        case VK_ESCAPE:
             PostQuitMessage (0);          
             break;
             return 0;
        }
   case WM_PAINT:
        hdc = BeginPaint (hwnd, &ps);
        MoveToEx(hdc, xx, yy, NULL);
        LineTo(hdc, xx+5, yy);
        MoveToEx(hdc, xx+5, yy, NULL);
        LineTo(hdc, xx+5, yy+5);
        MoveToEx(hdc, xx+5, yy+5, NULL);        
        LineTo(hdc, xx, yy+5);
        MoveToEx(hdc, xx, yy+5, NULL);        
        LineTo(hdc, xx, yy);
        EndPaint (hwnd, &ps);
        return 0;
       
   case WM_DESTROY:
        PostQuitMessage (0);
        return 0;
   }
   return DefWindowProc (hwnd, message, wParam, lParam);
}


Good luck with your game!




* Hosted for free by InvisionFree