I'm following a tutorial on Win32, and as I compile the examples, I'm also kinda messing with the code to experiment with different concepts.

Here is a little ditty i worked with:

main.cpp

#include <windows.h>
#include <iostream>
#include "prototypes.h"
#include "defines.h"


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



int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
			LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;
  WNDCLASS wc = {0};
  wc.lpszClassName = "Menu" ;
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0, IDC_ARROW);

  RegisterClass(&wc);
  CreateWindow( wc.lpszClassName, "Menu",
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                100, 100, 200, 150, 0, 0, hInstance, 0);

  while( GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
  switch(msg)
  {
      case WM_CREATE:
          AddMenus(hwnd);
          break;

      case WM_COMMAND:
          switch(LOWORD(wParam)) {
              case IDM_FILE_NEW:
              std::cout << "LOL\n";
              break;

              case IDM_FILE_OPEN:
                  Beep(900, 100);
                  break;

              case IDM_FILE_QUIT:
                  SendMessage(hwnd, WM_CLOSE, 0, 0);
                  break;
           }
           break;

      case WM_DESTROY:
          PostQuitMessage(0);
          break;

  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}

addmenu.cpp

#include "prototypes.h"
#include <windows.h>
#include "defines.h"

void AddMenus(HWND hwnd) {
  HMENU hMenubar;
  HMENU hMenu;

  hMenubar = CreateMenu();
  hMenu = CreateMenu();

  AppendMenu(hMenu, MF_STRING, IDM_FILE_NEW, "&New");
  AppendMenu(hMenu, MF_STRING, IDM_FILE_OPEN, "&Open");
  AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
  AppendMenu(hMenu, MF_STRING, IDM_FILE_QUIT, "&Quit");

  AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hMenu, "&File");
  SetMenu(hwnd, hMenubar);
}

prototypes.h

#include <windows.h>

#ifndef PROTOTYPES_H_INCLUDED
#define PROTOTYPES_H_INCLUDED

void AddMenus(HWND hwnd);

#endif // PROTOTYPES_H_INCLUDED

defines.h

#ifndef DEFINES_H_INCLUDED
#define DEFINES_H_INCLUDED

#define IDM_FILE_NEW 1
#define IDM_FILE_OPEN 2
#define IDM_FILE_QUIT 3


#endif // DEFINES_H_INCLUDED

I was just wondering how well this is organized. I put the function(s) in one place, the defines in their own file; and i just wanna know how good/bad this is. If there are better methods of organization, could you explain them????

Some companies require it to be a certain way but if you want to know the way that is most often used it is; all of the defines and classes and functions in a header, say foo.h, and then all of its implementation in a cpp file, foo.cpp.

This article has been dead for over six months. Start a new discussion instead.