Skip navigation

Basic windows starter

// Win32

#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow )
{
  AllocConsole() ;
  AttachConsole( GetCurrentProcessId() ) ;
  freopen( "CON", "w", stdout ) ;
  puts( "************** Program start **************" ) ;
  WNDCLASSEX wcx = { 0 };
  wcx.cbSize = sizeof( WNDCLASSEX ) ;
  wcx.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
  wcx.hCursor = LoadCursor( NULL, IDC_ARROW );
  wcx.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  wcx.hInstance = hInstance;
  wcx.lpfnWndProc = WndProc;
  wcx.lpszClassName = TEXT("BasicWindow");
  wcx.style = CS_HREDRAW | CS_VREDRAW;
  RegisterClassEx( &wcx );
  
  HWND hwnd = CreateWindowEx(
    0,
    TEXT("BasicWindow"),
    TEXT("Window title!"),
    WS_OVERLAPPEDWINDOW,
    10, 10,
    200, 200,
    NULL, NULL,
    hInstance, NULL );

  ShowWindow(hwnd, iCmdShow );
  UpdateWindow(hwnd);

  MSG msg;
  while( GetMessage( &msg, NULL, 0, 0 ) )
  {
    TranslateMessage( &msg );
    DispatchMessage( &msg );
  }

  return msg.wParam ;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{
  switch( message )
  {
  case WM_CREATE:
    {
      // Stuff to do when the window is created
    }
    return 0;
    
  case WM_PAINT:
    {
      // we would place our Windows painting code here.
      HDC hdc;
      PAINTSTRUCT ps;
      hdc = BeginPaint( hwnd, &ps );

      // draw a circle and a 2 squares
      Ellipse( hdc, 20, 20, 160, 160 );
      Rectangle( hdc, 50, 50, 90, 90 );
      Rectangle( hdc, 100, 50, 140, 90 );

      EndPaint( hwnd, &ps );
    }
    return 0;

  case WM_KEYDOWN:
    {
      if( wparam == VK_ESCAPE )
      {
        PostQuitMessage( 0 ) ;
        return 0 ;
      }
    }
    break ;

  case WM_DESTROY:
    {
      PostQuitMessage( 0 ) ;
    }
    return 0;
  }

  return DefWindowProc( hwnd, message, wparam, lparam );
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: