Teen Programmers Unite  
 

 

Return to forum top

Windows API

Posted by Faisal [send private reply] at April 22, 2002, 08:05:27 PM

I'm tired of programming dos programs.. I want to move to something like the windows API. I've been looking at the codeing for the API and it doesnt look easy. What is the best approch for learning how to program in the windows API in C++?

Posted by CodeRed [send private reply] at April 22, 2002, 09:09:38 PM

Get a good book, or check gamedev

Posted by CodeRed [send private reply] at April 22, 2002, 09:14:07 PM


//////////////////////////////////////
// A Simple Window Tutorial

////////////////////////////
// By Jesse King
// 11/13/99
////////////

/////////////
// #includes
// You will use this for all of your Windows applications
#include <windows.h>

////////////
// #defines
#ifndef  WIN32_LEAN_AND_MEAN      // This cleans out rarely used stuff
#define  WIN32_LEAN_AND_MEAN
#endif
#define  MAX_LOADSTRING  100 

///////////////////
// Global Variables
HINSTANCE      hInst;                                   // the Instance
TCHAR          szWindowClass[] = "I'M A TITLE!!!" ;     // Title Bar

//////////////////////////////////////////////////
// Prototypes: beforehand declaration of functions
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);

///////////////////////////////////////
// WinMain() is what applies everthing
int APIENTRY WinMain(HINSTANCE hInstance,
  HINSTANCE hPrevInstance,
  LPSTR lpCmdLine,
  int nCmdShow)
{
   MSG msg;

   /////////////////////////
   // Set up the windows class
   MyRegisterClass(hInstance);

   /////////////////////////
   // Create the window. If it doesn't work, return FALSE.
   if (!InitInstance (hInstance, nCmdShow)) 
      return FALSE;

   ////////////////////////
   // This is a good place to use accelerations and whatnot,
   // you should load them in the program from the resources (the whatever.rc)
   ////////////////////////

   ////////////////////////
   // The main message loop is run under here,
   // it pretty much explains itself. It just gets and handelse messages.
   while (GetMessage(&msg, NULL, 0, 0)) 
   {
      if (!TranslateAccelerator(msg.hwnd, 0, &msg)) 
      {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
      }
   }

   return msg.wParam;
}

/////////////////////
// the function MyRegisterClass()
// this is used to create a WIN32 application,
// if you have Microsoft Visual C++ and the documentation
// it explains this really well. All the wc.xxxxxxx are variables in the
// WNDCLASSEX structure, it took me a while to figure it out, just look at 
// help files and tutorials, it's easier then it seems.
ATOM MyRegisterClass(HINSTANCE hInstance)
{
   //////////////
   // This allows you to use the structure
   // WNDCLASSEX as wc.whatevervariable
   WNDCLASSEX wc ;
   wc.cbSize         = sizeof(WNDCLASSEX) ;
   //////////////
   // Class Style described here.
   wc.style        = CS_HREDRAW | CS_VREDRAW ;
   //////////////
   // This is the functions that is called when a message is sent to the window (like a PAINT msg).
   wc.lpfnWndProc        = (WNDPROC)WndProc ;
   ///////////////
   // This is a simple window, so this isn't needed.
   wc.cbClsExtra        = 0 ;
   ///////////////
   // Same as above
   wc.cbWndExtra        = 0 ;
   ///////////////
   // The instance of the window.
   wc.hInstance        = hInstance ;
   ///////////////
   // A declaration of the icon, in the resources, or from file, but
   // this one is from the resources.
   wc.hIcon        = 0 ;
   ///////////////
   // Loads the pointer, or cursor your window application will use
   wc.hCursor        = 0 ;
   ///////////////
   // The background color can change through color_window+whatever
   wc.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1) ;
   ///////////////
   // Name of the menu system, but we don't have a menu here
   // because this is a simple window.
   wc.lpszMenuName    = 0 ;
   ///////////////
   // The title of the used windows classes, or
   // the title of the program.
   wc.lpszClassName    = szWindowClass ;
   ///////////////
   // This is the small icon, like when your windows options is to list all the
   // icons, then it will display the small icons, this is the declaration of the 
   // icon used for this occasion
   wc.hIconSm        = 0 ;

   ///////////////
   // This returns a registered structure,
   // which is the windows classes, to the window.
   return RegisterClassEx(&wc) ;
}

/////////////////////
// the function InitInstance(HANDLE, int)
// creates the main window or parent window, and 
// describes the style (such as WS_OVERLAPPEDWINDOW, WS means 
// Windows Style) and many other characteristics of the 
// Parent Window.

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd ;
   hInst = hInstance ;              // Store instance handle on global hInst
   hWnd = CreateWindow( szWindowClass,
                        0, 
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,
                        0, 
                        CW_USEDEFAULT, 
                        0, 
                        0, 
                        0, 
                        hInstance, 
                        0) ;

   /////////////////////////
   // If the window isn't made right
   // it won't work...
   if (!hWnd)
   {
      return FALSE ;
   }
   //////////////////////////
   // expains itself, nCmdShow 
   // means that its a small window
   // not maximized
   ShowWindow(hWnd, nCmdShow) ;
   UpdateWindow(hWnd) ;

   return TRUE ;
}

////////////////////////////////////////////////////
// The function WndProc(HWND, unsigned, WORD, LONG)
// processes the window, it does whatever you want 
// the window to do.
//
// WndProc()'s purpose is processing messages for the main window.
//
// ********************* BELOW ************************
// 
//
// case**WM_MESSAGE    - Use the keyboard for the exiting the program,
// I set it up as ESCAPE Exits the program (or VK_ESCAPE does).
// 
// case**WM_PAINT        - Paints the main window, you can blit bitmaps
// and such, but this is a simple window, so I painted it like
// that
//
// case**WM_DESTROY    - Destroys the window when you push
// the X on the upper right of the window
////////////////////////////////////////////////////

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   PAINTSTRUCT ps;
   HDC             hdc;
    
   LoadString(hInst, 0, 0, MAX_LOADSTRING) ;
   //////////////////////////
   // This is a nice simple loop
   // that lets you input and get
   // output to and from the window
   switch (message) 
   {
      case WM_KEYDOWN:
         switch(wParam)
         {
            case VK_ESCAPE:
               PostQuitMessage(0) ;
               break ;
         }

      case WM_PAINT:
         hdc = BeginPaint(hWnd, &ps) ;
         RECT rt ;
         GetClientRect(hWnd, &rt) ;
         EndPaint(hWnd, &ps) ;
         break ;

      case WM_DESTROY:
         PostQuitMessage(1) ;
         break ;

      default:
         return DefWindowProc(hWnd, message, wParam, lParam) ;
   }
   ////////////////////////////////
   // Return so it doesn't complain
   return 0 ;
}

Posted by CodeRed [send private reply] at April 22, 2002, 09:17:38 PM

There are a lot of comments in that, it really isn't as complicated as it looks. The biggest thing you have to get used to is the message pump, that LRESULT CALLBACK function and the translateMessage and dispatchMessage functions

Posted by Faisal [send private reply] at April 22, 2002, 10:32:08 PM

codered: how do I compile it?

any suggested books?

Posted by buzgub [send private reply] at April 23, 2002, 01:23:50 AM

http://www.winprog.org/tutorial/ may well help you out.

Posted by CodeRed [send private reply] at April 24, 2002, 08:19:12 AM

Why can't you? What errors are you getting? It's not my code so don't blame me. In MSVCPP6 make a new win32 project, not win32 console project, that might be your problem.

Posted by Faisal [send private reply] at April 24, 2002, 12:13:35 PM

thanks

Posted by Faisal [send private reply] at April 25, 2002, 12:07:30 PM

I got an error box when the program loaded. it said "send error report". it must have a bug, or it could be windows again

Posted by CViper [send private reply] at April 25, 2002, 12:18:24 PM

Just a little note:
You'll have to #define WIN32_LEAN_AND_MEAN *before* including <windows.h>. Otherwise it won't have any effect.

I think it excludes MFC from the <windows.h>, but I'm not entirly sure about that...

Posted by CodeRed [send private reply] at April 27, 2002, 11:07:21 PM

I just compiled this VC++ 6 and it works perfectly fine as is.

Posted by CodeRed [send private reply] at April 27, 2002, 11:08:07 PM

Oh, I was talking to faisal not CViper, what he said about WIN32_LEAN_AND_MEAN is true

Posted by Mike_L [send private reply] at April 29, 2002, 07:30:49 PM

Faisal, there are lots of good tutorials on the Internet. Google is your friend. I also have some example programs that I made when I was learning the Win32 API. You can find them at http://tamale.net/Windows.html

Figure out your compiler and get the `Hello' program to work. Then move on to HeyWindows.

-Mike_L

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.