René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Using the DibSection class

The following program intents to demonstrate how some of these classes can be used to programm graphics on windows.
#define UNICODE
#include <windows.h>
#include <process.h>

#include "DibSection.h"
#include "Dc.h"

class GraphicRectangle : public DibSection {
public:
  GraphicRectangle(
    HWND parent,
    int x,
    int y,
    int width,
    int height);

  void Draw();

private:

  static LRESULT CALLBACK GR_WndProc(
    HWND   hWnd,
    UINT   msg,
    WPARAM wParam,
    LPARAM lParam );

  HWND hwnd_;

  static bool registered_;
};

bool GraphicRectangle::registered_=false;

LRESULT CALLBACK GraphicRectangle::GR_WndProc(
    HWND   hWnd,
    UINT   msg,
    WPARAM wParam,
    LPARAM lParam ) {

  GraphicRectangle* gr=  (GraphicRectangle*) GetWindowLong(hWnd,GWL_USERDATA);

  switch( msg ) {
    case WM_PAINT: {
      PAINTSTRUCT ps;
      Dc dc(BeginPaint( hWnd, &ps ));
      gr->BitBltTo(dc,0,0);
      EndPaint( hWnd, &ps );
    }
    break;

/*    case WM_CREATE: {
      gr->hwnd_=hWnd;
    } */

    case WM_DESTROY:
      ;

    break;

    default:
      return DefWindowProc( hWnd, msg, wParam, lParam);
  } 
  return 0;
}


GraphicRectangle::GraphicRectangle(
    HWND parent,
    int x,
    int y,
    int width,
    int height) {

  if (! registered_) {
    WNDCLASSEXW wce;

    wce.cbSize        = sizeof(wce);
    wce.style         = CS_VREDRAW | CS_HREDRAW; 
    wce.lpfnWndProc   = (WNDPROC) GR_WndProc; 
    wce.cbClsExtra    = 0; 
    wce.cbWndExtra    = 0; 
    wce.hInstance     = 0;// hInstance; 
    wce.hIcon         = LoadIcon(0, IDI_APPLICATION); 
    wce.hCursor       = LoadCursor(0, IDC_ARROW); 
    wce.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
    wce.lpszMenuName  = 0;
    wce.lpszClassName = L"GraphicRectangle",
    wce.hIconSm       = 0;
 
    if (!RegisterClassExW(&wce)) throw "Could not register GraphicRectangle"; 
    registered_ = true;
  }
  
  HWND hWnd = CreateWindowExW(
    0, 
    L"GraphicRectangle",
    0,
    WS_CHILD | WS_VISIBLE ,
    x,y,width,height,
    parent,
    NULL,
    0,
    0 
  );

  hwnd_=hWnd;

  if (!hWnd) throw "Could not create window for GraphicRectangle";

  SetWindowLongW(hWnd,GWL_USERDATA ,(long)this);

  Size(width,height);
}


void GraphicRectangle::Draw() {
  InvalidateRect(hwnd_,0,false);

}



LRESULT CALLBACK WndProc(
    HWND   hWnd,
    UINT   msg,
    WPARAM wParam,
    LPARAM lParam ) {

  switch( msg ) {
    case WM_DESTROY:
      PostQuitMessage(0);
    break;

    default:
      return DefWindowProc( hWnd, msg, wParam, lParam);
  } 
  return 0;
}


int const nofH =  3;
int const nofV =  5;
int const w    = 40;
int const h    = 30;
int const gap  = 10;

unsigned int __stdcall TF(void* param) {
  GraphicRectangle* gr = (GraphicRectangle*) param;

  srand((int) gr);

  int i=0;
  while (1) {
    gr->PixelAt(rand()%(w-1),rand()%(h-1),rand());
    if (i>100) {
      gr->Draw();
      i=0;
    }
    else {
      i++;
    }
  }
}

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

  WNDCLASSEXW wce;

  wce.cbSize        = sizeof(wce);
  wce.style         = CS_VREDRAW | CS_HREDRAW; 
  wce.lpfnWndProc   = (WNDPROC) WndProc; 
  wce.cbClsExtra    = 0; 
  wce.cbWndExtra    = 0; 
  wce.hInstance     = hInstance; 
  wce.hIcon         = LoadIcon  (0, IDI_APPLICATION); 
  wce.hCursor       = LoadCursor(0, IDC_ARROW); 
  wce.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
  wce.lpszMenuName  = 0;
  wce.lpszClassName = L"ADPWinClass",
  wce.hIconSm       = 0;
 
  if (!RegisterClassExW(&wce)) return 0; 
  

  HWND hWnd = CreateWindowExW(
    0,          // Ex Styles
    L"ADPWinClass",
    L"ADP GmbH",
     WS_CAPTION | WS_SYSMENU,
     20, 20, 20 + (w+gap) * nofH, 20 + (h+gap) * nofV,
     NULL,           // Parent Window
     NULL,           // Menu, or windows id if child
     hInstance,      // 
     NULL            // Pointer to window specific data
  );

  ShowWindow( hWnd, nCmdShow );

  for (int a=0;a<nofH;a++) {
    for (int b=0;b<nofV;b++) {
      GraphicRectangle* gr =new GraphicRectangle(hWnd,
        20+a*(w+gap),
        20+b*(h+gap),
        w,
        h);

      _beginthreadex(0,0,TF,(void*) gr,0,0);
    }
  }

  MSG msg;
  int r;
  while ((r = GetMessageW(&msg, NULL, 0, 0 )) != 0) { 
    if (r == -1) {
      ;  // Error!
    }
    else {
      TranslateMessage(&msg); 
      DispatchMessageW(&msg); 
    }
  } 

  // The application's return value
  return msg.wParam;
};