#include <windows.h>
#include <fstream.h>
#include <io.h>
#include <fcntl.h>
#ifdef __BORLANDC__
  #pragma argsused
#endif

extern HINSTANCE _hInstance;
 static const char tytul[] = "Edytor bitmap";
 static OPENFILENAME ofn;
 static CHOOSECOLOR kolor;
 int MAXWRITE=20;
 DWORD aktkolor=0x00ffffff;
 HANDLE bitmapa;
 HDC hdc;
 HDC hdcc;
 BITMAP bit_wymiar;
 RECT up;
  int xm,ym;
 LPCTSTR filtr[4];
 LPCTSTR dom_roz="txt";
 static char nazwa_zbioru[3 * 1024];


LONG APIENTRY GlownaFunkcja(
HWND hwnd,                /* window handle                   */
UINT message,             /* type of message                 */
UINT wParam,              /* additional information          */
LONG lParam)              /* additional information          */
{

    CHAR znak;

    CHAR bufor[32000];
    switch (message) {

      case WM_PAINT:
      {

              //hdc=GetDC(hwnd);
              PAINTSTRUCT ps;
              hdc=BeginPaint(hwnd,&ps);
              hdcc=CreateCompatibleDC(hdc);
              SelectObject(hdcc,bitmapa);
              //rysowanie po bitmapie
              SetPixel(hdcc,xm,ym,aktkolor);
              BitBlt(hdc,0,50,bit_wymiar.bmWidth,bit_wymiar.bmHeight,hdcc,0,0,SRCCOPY);
              EndPaint(hwnd,&ps);
              DeleteDC(hdcc);
                    }
              break;

        case WM_COMMAND:
        int zb_wyjscie;
        int zb_wejscie;

            switch (LOWORD(wParam)) {
              case 10:
               filtr[0]="Bitmapy";
               filtr[1]="*.BMP";
               filtr[2]="Wszystkie pliki";
               filtr[3]="*.*\0";
               nazwa_zbioru[0] = '\0';
               ZeroMemory(&ofn, sizeof(ofn));
               ofn.lStructSize = sizeof(ofn);
               ofn.hwndOwner = hwnd;
               ofn.lpstrFile = nazwa_zbioru;
               ofn.nMaxFile = sizeof(nazwa_zbioru);
               ofn.lpstrTitle = " Otwórz zbiór ";
               ofn.lpstrFilter=filtr[0];
               ofn.Flags =
               OFN_FILEMUSTEXIST |     // don't allow a new file name to be given
               OFN_EXPLORER |          // use an IE style selection dialog
               OFN_PATHMUSTEXIST |     // don't allow a new dir name to be given
               OFN_HIDEREADONLY;

              if (GetOpenFileName(&ofn)) {
                bitmapa=LoadImage(_hInstance,nazwa_zbioru,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
                GetObject(bitmapa,sizeof(bit_wymiar),&bit_wymiar);
              }
               break;

             case 20:
             static DWORD standard[16];
             ZeroMemory(&kolor, sizeof(CHOOSECOLOR));
             kolor.lStructSize=sizeof(CHOOSECOLOR);
     kolor.hwndOwner=hwnd;
     kolor.rgbResult=aktkolor;
     kolor.lpCustColors=(LPDWORD) standard;
     kolor.Flags=CC_FULLOPEN|CC_RGBINIT;
     if (ChooseColor(&kolor))
     aktkolor=kolor.rgbResult;

               break;


         case 30:
             SendMessage(hwnd,WM_DESTROY,0,0);
         break;
            }
 break;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;

            case WM_MOUSEMOVE:
            if (wParam==MK_LBUTTON){

            xm = LOWORD(lParam);
            ym = HIWORD(lParam);
            up.left=xm-4;
            up.top=ym-4;
            up.right=xm+4;
            up.bottom=ym+4;
            ym=ym-50;
      InvalidateRect(hwnd,&up,TRUE);
       UpdateWindow(hwnd);
            }
                         break;

        default:
            return DefWindowProc(hwnd, message, wParam, lParam);


}
return NULL;
}

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
  MSG msg;
HWND main_hwnd;
WNDCLASS glowne_okno; // holds data used to set up the window's class

 glowne_okno.style = CS_HREDRAW | CS_VREDRAW;
 glowne_okno.lpfnWndProc = GlownaFunkcja;
 glowne_okno.cbClsExtra = glowne_okno.cbWndExtra = 0;
 glowne_okno.hInstance = _hInstance;
 glowne_okno.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 glowne_okno.hCursor = LoadCursor(NULL, IDC_ARROW);
 glowne_okno.hbrBackground = (HBRUSH)COLOR_WINDOW;
 glowne_okno.lpszMenuName = NULL;
 glowne_okno.lpszClassName = tytul;

 if (RegisterClass(&glowne_okno))
   {
   main_hwnd = CreateWindow(tytul, tytul,
                WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
                CW_USEDEFAULT, CW_USEDEFAULT,
                CW_USEDEFAULT, CW_USEDEFAULT,
                HWND_DESKTOP, NULL, _hInstance, NULL);
   }
 else
   main_hwnd = NULL;

if (main_hwnd) // if window creation was successful
  {
  ShowWindow(main_hwnd, nCmdShow); // show the window
  UpdateWindow(main_hwnd);         // cause its contents to be painted
  CreateWindow("button", "Otwórz", WS_CHILD | WS_VISIBLE,
  5,10,50,30, main_hwnd, (HMENU)10, hInstance, NULL);
  CreateWindow("button", "Kolor", WS_CHILD | WS_VISIBLE,
  55,10,50,30, main_hwnd, (HMENU)20, hInstance, NULL);
  CreateWindow("button", "Koniec", WS_CHILD | WS_VISIBLE,
  405,10,50,30, main_hwnd, (HMENU)30, hInstance, NULL);

  while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
    TranslateMessage(&msg); // handle messages sent to
    DispatchMessage(&msg);  // program & its  windows
    }
  }

return msg.message;

}
