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


#define appname "GetDCExTest"

LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  HDC hdc;
  char buf[20];

  switch (uMsg) {
  case WM_PAINT:
    hdc = GetDCEx(hwnd, NULL, DCX_WINDOW | DCX_INTERSECTRGN);
    ReleaseDC(hwnd,hdc);
    printf("1 hdc: %x\n", (unsigned int)hdc);
	hdc = GetDC(hwnd);
    ReleaseDC(hwnd,hdc);
    printf("2 hdc: %x\n", (unsigned int)hdc);
    ExitProcess(0);
  }

  DefWindowProc(hwnd,uMsg,wParam,lParam);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {

  MSG msg;
  WNDCLASS wc;
  HWND hWnd;

  wc.style = 0;
  wc.lpfnWndProc = WinProc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = hInstance;
  wc.hIcon = LoadIcon( hInstance, "GetDCEx Test" );
  wc.hCursor = LoadCursor( 0, IDI_APPLICATION );
  wc.hbrBackground = GetStockObject( BLACK_BRUSH );
  wc.lpszMenuName = NULL;
  wc.lpszClassName = appname;

  if (!RegisterClass(&wc)) ExitProcess(1);

  hWnd = CreateWindow( appname, appname,
                       WS_VISIBLE | WS_BORDER | WS_CAPTION,
                       CW_USEDEFAULT, CW_USEDEFAULT, 
                       CW_USEDEFAULT, CW_USEDEFAULT,
                       0, 0, hInstance, NULL );
  if (!hWnd) ExitProcess(1);

  ShowWindow(hWnd, nCmdShow );
  UpdateWindow(hWnd);
  while( GetMessage(&msg, 0, 0, 0) ) {
    TranslateMessage(&msg);  
    DispatchMessage(&msg);
  }
  return msg.wParam;
}
