Thread: how i create a window whith all it's elements

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    5

    Post how i create a window whith all it's elements

    hi. every one, I just create a window. but it's emty and blank without any file menu,Edit menu, how can i make a window with all elements. i need help for this . and thank you.

    my code was like this


    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    
    char szWinName[] = "MyWin"; /* name of window class */
    
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
                       LPSTR lpszArgs, int nWinMode)
    {
      HWND hwnd;
      MSG msg;
      WNDCLASSEX wcl;
    
      /* Define a window class. */
      wcl.cbSize = sizeof(WNDCLASSEX);
    
      wcl.hInstance = hThisInst; /* handle to this instance */
      wcl.lpszClassName = szWinName; /* window class name */
      wcl.lpfnWndProc = WindowFunc; /* window function */
      wcl.style = 0; /* default style */
    
      wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* large icon */
      wcl.hIconSm = LoadIcon(NULL, IDI_WINLOGO); /* small icon */
      wcl.hCursor = LoadCursor(NULL, IDC_ARROW); /* cursor style */
    
      wcl.lpszMenuName = NULL; /* no menu */
      wcl.cbClsExtra = 0; /* no extra */
      wcl.cbWndExtra = 0; /* information needed */
    
      /* Make the window background white. */
      wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    
      /* Register the window class. */
      if(!RegisterClassEx(&wcl)) return 0;
    
      /* Now that a window class has been registered, a window
         can be created. */
      hwnd = CreateWindow(
        szWinName, /* name of window class */
        "Windows NT Skeleton", /* title */
        WS_OVERLAPPEDWINDOW, /* window style - normal */
        CW_USEDEFAULT, /* X coordinate - let Windows decide */
        CW_USEDEFAULT, /* Y coordinate - let Windows decide */
        CW_USEDEFAULT, /* width - let Windows decide */
        CW_USEDEFAULT, /* height - let Windows decide */
        HWND_DESKTOP, /* no parent window */
        NULL,
        hThisInst, /* handle of this instance of the program */
        NULL /* no additional arguments */
      );
    
      /* Display the window. */
      ShowWindow(hwnd, nWinMode);
      UpdateWindow(hwnd);
    
      /* Create the message loop. */
      while(GetMessage(&msg, NULL, 0, 0))
      {
        TranslateMessage(&msg); /* allow use of keyboard */
        DispatchMessage(&msg); /* return control to Windows NT */
      }
      return msg.wParam;
    }
    
    /* This function is called by Windows NT and is passed
       messages from the message queue.
    */
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,
                                WPARAM wParam, LPARAM lParam)
    {
      switch(message) {
        case WM_DESTROY: /* terminate the program */
          PostQuitMessage(0);
          break;
        default:
          /* Let Windows NT process any messages not specified in
             the preceding switch statement. */
          return DefWindowProc(hwnd, message, wParam, lParam);
      }
      return 0;
    }

  2. #2
    Banned SniperSAS's Avatar
    Join Date
    Aug 2005
    Posts
    175
    there are two ways to go about doing this

    1) use a resource file

    first start a new header file for you project called "resource.h" or something
    and put this code in:
    Code:
    #include <windows.h>
    #define IDM_MENU 101
    #define ID_FILE_EXIT 102
    #define ID_EDIT_COPY 103
    #define ID_HELP_ABOUT 104
    IDM_MENU is the identifier of our menu

    everything else will be the unique IDs our widnow will use to identify the buttons in our menu

    next put a resource file in your project and pop this code in:
    Code:
    #include "resource.h"
    IDM_MENU MENU
    BEGIN
        POPUP "&File"
        {
            MENUITEM "E&xit", ID_FILE_EXIT
        }
    
        POPUP "&Edit"
        {
            MENUITEM "Copy",ID_EDIT_COPY
        POPUP "&Help"
        {
            MENUITEM "&About", ID_HELP_ABOUT
        }
    END
    you use that syntax in a resource file to actually make your menu, so remember it or save it, hopefully it is pretty straight forward though
    the MENU declares the whole menu bar on the top of the window

    the POPUP is a button on the menu

    MENUITEMs are the buttons on the drop down menus that appear when you click on one of the menu options(POPUPs)

    now we have to attach our menu to the window, so include resource.h in your .cpp file and change

    Code:
    wcl.lpszMenuName = NULL; /* no menu */
    to

    Code:
    wcl.lpszMenuName = MAKEINTRESOURCE(IDM_MENU); /* there is now a menu */
    compile and run that ........ homes

    2) use HMENU

    first make a resource.h header file like we did in the last one but this time you don't need to define the IDM_MENU ID

    this way doesn't use a resource file, instead storing our menu in a variable of type HMENU

    put this code at the top of your source file:
    Code:
    HMENU hMenu, hSubMenu;
    hMenu will be used to store the menu, and hSubMenu will be used to create the individual options

    anyway, go to your windows process and add WM_CREATE to the message switch

    under the WM_CREATE put the following code:
    Code:
    hMenu = CreateMenu();
    
    hSubMenu = CreatePopupMenu();
    AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
    AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
    
    hSubMenu = CreatePopupMenu();
    AppendMenu(hSubMenu, MF_STRING, ID_EDIT_COPY, "&Copy");
    AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Edit");
    
    hSubMenu = CreatePopupMenu();
    AppendMenu(hSubMenu, MF_STRING, ID_HELP_ABOUT, "&About");
    AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");
    again pretty straightforward, but to explain it let's dissect this

    Code:
    hMenu = CreateMenu();
    creates the menu

    Code:
    hSubMenu = CreatePopupMenu();
    create hSubMenu as a poup menu and clear out anything that may have been in it

    Code:
    AppendMenu(hSubMenu, MF_STRING, ID_EDIT_COPY, "&Exit");
    Add an "exit" button to hSubMenu

    Code:
    AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
    Add hSubMenu to hMenu under the label "File"

    and then we are just repeating it for each popup menu

    Lastly, to attach the menu to our window

    Code:
    SetMenu(hwnd, hMenu);
    if it doesn't work tell me and i will fix what i did wrong but i think i remembered everything right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a child window
    By rakan in forum Windows Programming
    Replies: 2
    Last Post: 01-23-2007, 03:22 PM
  2. Adding buttons, edit boxes, etc to the window
    By rainmanddw in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2006, 03:07 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. input/output
    By dogbert234 in forum Windows Programming
    Replies: 11
    Last Post: 01-26-2005, 06:57 AM
  5. Create a window from a dialogbox ?
    By peter in forum Windows Programming
    Replies: 7
    Last Post: 09-09-2001, 07:42 AM