View Full Version: My code doesn't work!

C++ Learning Community > win32 api C++ programming > My code doesn't work!


Title: My code doesn't work!
Description: Problem with menus


Dragon - July 9, 2003 09:33 PM (GMT)
I've been trying to learn how to create menus, and I've been successful in creating JUST menus. But when I try to create edit controls and stuff, the program runs the quits! Can you help me figure out what is wrong with it?

QUOTE
#include <windows.h>

#define ID_FILE_SAVEAS 0001
#define ID_HELP_ABOUT 0002

const char g_szClassName[] = "MainWnd";

LRESULT CALLBACK WndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
  switch(Message)
  {
case WM_CREATE:
  HWND hEditFname, hEditText;
      HMENU hMenu, hSubMenu;
  HICON hIcon, hIconSm;
 
  /* Menu */
  hMenu = CreateMenu();

      hSubMenu = CreatePopupMenu();
      AppendMenu(hSubMenu, MF_STRING, ID_FILE_SAVEAS, "S&ave As");
  AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");

  hSubMenu = CreatePopupMenu();
  AppendMenu(hSubMenu, MF_STRING, ID_HELP_ABOUT, "A&bout");
  AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");

      SetMenu(hwnd, hMenu);
     
      /* Edit */
      hEditFname = CreateWindow(
                  "EDIT",
                  "File name",
                  WS_CHILD|WS_VISIBLE|WS_BORDER,
                  5,
                  15,
                  100,
                  20,
                  hwnd,
                  (HMENU)1,
                  ((LPCREATESTRUCT)lParam)->hInstance,
                  NULL);
      break;
case WM_COMMAND:
  switch(LOWORD(wParam))
  {
  case ID_FILE_SAVEAS:
    PostMessage(hwnd, WM_CLOSE, 0, 0);
    break;
  case ID_HELP_ABOUT:
          MessageBox(hwnd, "You clicked Go!", "Woo!", MB_OK);
    break;
  }
  break;
  case WM_CLOSE:
  DestroyWindow(hwnd);
  break;
  case WM_DESTROY:
  PostQuitMessage(0);
  break;
  default:
      return DefWindowProc(hwnd, Message, wParam, lParam);
    }
  return 0;
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
  HWND hwnd;
  MSG messages;
  WNDCLASSEX wincl;
 
  wincl.cbSize = sizeof(WNDCLASSEX);
  wincl.cbClsExtra = 0;
  wincl.cbWndExtra = 0;
  wincl.style = 0;
  wincl.hInstance = hInstance;
  wincl.lpszClassName = g_szClassName;
  wincl.lpszMenuName = NULL;
  wincl.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
  wincl.hIcon = LoadIcon(NULL,IDI_APPLICATION);
  wincl.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
  wincl.hCursor = LoadCursor(NULL,IDC_ARROW);
  wincl.lpfnWndProc = WndProc;
 
  if (!RegisterClassEx(&wincl))
    return 0;
 
  hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Text Writer",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        180,
        220,
        NULL,
        NULL,
        hInstance,
        NULL);
 
  ShowWindow(hwnd,nCmdShow);
 
  while (GetMessage(&messages,NULL,0,0))
  {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
  }
  return messages.wParam;
}

dr voodoo - July 10, 2003 08:28 AM (GMT)
I see the following errors:
1.in WM_CREATE you declare vars so you need a scope!
2.The ID of your EDIT is identic with the one of the Exit function in your menu. If you create or click an EDIT it posts a Message just like buttons.

Dragon - July 10, 2003 04:03 PM (GMT)
Thanks.




* Hosted for free by InvisionFree