// FinezyjneOknoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FinezyjneOkno.h"
#include "FinezyjneOknoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CFinezyjneOknoDlg dialog




CFinezyjneOknoDlg::CFinezyjneOknoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFinezyjneOknoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	czyKlawiszMyszyWcisniety = false;
}

void CFinezyjneOknoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CFinezyjneOknoDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_NCPAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()


// CFinezyjneOknoDlg message handlers

BOOL CFinezyjneOknoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	RECT rect;
	this->GetWindowRect(&rect);
	rect.right = rect.left + 134 ;
	rect.bottom = rect.top + 156 ;
	this->MoveWindow(&rect);

	// TODO: Add extra initialization here
	maska = (HBITMAP)LoadImage(NULL,L"mask.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

	if(!maska) return FALSE;
	HRGN rgnZrodlo = CreateRectRgn(0,0,0,0);
	HRGN rgn;
	BITMAP bitmap;

	GetObject(maska,sizeof(BITMAP),&bitmap);

	BYTE bityNaPixel = bitmap.bmBitsPixel >> 3;
	BYTE * bits = new BYTE[bitmap.bmWidth * bitmap.bmHeight * bityNaPixel];

	GetBitmapBits(maska,bitmap.bmWidth * bitmap.bmHeight * bityNaPixel,bits);

	int przezPixel = *(int*)bits;
	przezPixel <<= 32 - bitmap.bmBitsPixel;

	int x,pixel;
	for(int i=0;i<bitmap.bmHeight;i++)
	{
		x = -1;
		for(int j=0;j<bitmap.bmWidth;j++)
		{
			pixel = *(int*)(bits+(i*bitmap.bmWidth+j)*bityNaPixel) << (32 - bitmap.bmBitsPixel);
			if(pixel != przezPixel)
			{
				if(x < 0)
				{
					x = j;
				}
				else if( j == (bitmap.bmWidth -1))
				{
					rgn = CreateRectRgn(x,i,j,i+1);
					CombineRgn(rgnZrodlo,rgnZrodlo,rgn,RGN_OR);
					x = -1;
				}
			}
			else if(x >=0)
			{
				rgn = CreateRectRgn(x,i,j,i+1);
				CombineRgn(rgnZrodlo,rgnZrodlo,rgn,RGN_OR);
				x = -1;
			}
		}
	}

	delete bits;
	SetWindowRgn(rgnZrodlo,TRUE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFinezyjneOknoDlg::OnPaint()
{
	CBitmap bitmap;
	bitmap.Attach(maska);

	CClientDC dc(this);
	CDC bmDC;
	bmDC.CreateCompatibleDC(&dc);
	bmDC.SelectObject(&bitmap);
	dc.BitBlt(0,0,134,156,&bmDC,0,0,SRCCOPY);
	bmDC.DeleteDC();
	bitmap.Detach();

    //CDialog::OnPaint();
	
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFinezyjneOknoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CFinezyjneOknoDlg::OnNcPaint()
{
	// TODO: Add your message handler code here
	// Do not call CDialog::OnNcPaint() for painting messages

}

void CFinezyjneOknoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	this->czyKlawiszMyszyWcisniety = true;
	this->klawiszMyszyX = point.x;
	this->klawiszMyszyY = point.y;
	//CDialog::OnLButtonDown(nFlags, point);
}

void CFinezyjneOknoDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	this->czyKlawiszMyszyWcisniety = false;
	//CDialog::OnLButtonUp(nFlags, point);
}

void CFinezyjneOknoDlg::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(czyKlawiszMyszyWcisniety)
	{
		int dx =  point.x - klawiszMyszyX;
		int dy =  point.y - klawiszMyszyY;
		RECT rect;
		this->GetWindowRect(&rect);
		rect.left += dx;
		rect.top += dy;
		rect.right = rect.left + 134 ;
		rect.bottom = rect.top + 156 ;
		this->MoveWindow(&rect);
	}
	//CDialog::OnMouseMove(nFlags, point);
}
