www.pudn.com > CADimage.rar > MainWindow.cpp, change:2005-09-06,size:20379b


// MainWindow.cpp: implementation of the CMainWindow class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "MainWindow.h" 
#include <memory.h> 
#include "resource.h" 
#include <stdio.h> 
#include <math.h> 
 
// 
//  Functions for the mouse position definition 
// 
void Ort(LPFPOINT point) 
{ 
  float len = (float)sqrt(point->x*point->x + point->y*point->y + point->z*point->z); 
  point->x = point->x / len; 
  point->y = point->y / len; 
  point->z = point->z / len; 
} 
 
void MoveToPosition(LPFPOINT point, const FPOINT pos, const int direction) 
{   
  point->x = point->x + direction * pos.x; 
  point->y = point->y + direction * pos.y; 
  point->z = point->z + direction * pos.z; 
} 
 
////////////////////////////////////////////////////////////////////// 
// Static members 
////////////////////////////////////////////////////////////////////// 
#ifndef CS_STATIC_DLL 
	static CADLAYER CADLayer; 
	static CADLAYERCOUNT CADLayerCount; 
	static CADLAYERVISIBLE CADLayerVisible; 
	static CADVISIBLE CADVisible;	 
	static CLOSECAD CloseCAD; 
	static CREATECAD CreateCAD; 
	static CADLAYOUT CADLayout; 
	static CADLAYOUTNAME CADLayoutName; 
	static CADLAYOUTSCOUNT CADLayoutsCount; 
	static CADLAYOUTVISIBLE CADLayoutVisible; 
	static CURRENTLAYOUTCAD CurrentLayoutCAD; 
	static DEFAULTLAYOUTINDEX DefaultLayoutIndex; 
	static DRAWCADEX DrawCADEx; 
	static DRAWCADTOJPEG DrawCADtoJpeg; 
	static GETBOXCAD GetBoxCAD; 
	static GETEXTENTSCAD GetExtentsCAD; 
	static GETIS3DCAD GetIs3dCAD; 
	static GETPOINTCAD GetPointCAD;	 
	static RESETDRAWINGBOXCAD ResetDrawingBoxCAD; 
	static SETDRAWINGBOXCAD SetDrawingBoxCAD; 
	static SETPROCESSMESSAGESCAD SetProcessMessagesCAD; 
	static SETROTATECAD SetRotateCAD;	 
#endif 
 
bool CMainWindow::IsAppChangingList; 
HWND CMainWindow::hwndLayersDlg; 
//HWND CMainWindow::hwndComboBox; 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
CMainWindow::CMainWindow(LPSTR szClassName, WNDPROC WndProc, HINSTANCE hInst, 
                 HICON hIcon, HICON hCursor, LPSTR lpszMenuName, 
                 HBRUSH color, UINT style): 
CWindow(szClassName, WndProc, hInst, hIcon, hCursor, lpszMenuName, color, style) 
{	 
	hInstance = hInst; 
	hwndStatusBar = hwndToolBar = hwndComboBox = hwndLayersDlg = NULL; 
	curWait = LoadCursor(NULL, MAKEINTRESOURCE(IDC_WAIT));	 
	int r = GetLastError(); 
	curHand = LoadCursor(hInstance, "HAND"); 
	curTarget = LoadCursor(hInstance, "TARGET"); 
	curDefault = hCursor; 
	SetCursor(curDefault); 
	ScaleRect.x = -1; 
	fKoef = 1; 
    colorBgrnd = RGB(255,255,255); 
	oldPoint.x = 0; 
	oldPoint.y = 0; 
	offset.x = 0; 
	offset.y = TOOLBAR_SIZE; 
	nScale = 0; 
	drag = false; 
	DrwMode = 0; 
    brushBackground = WndClass.hbrBackground;	 
	CADImage = 0; 
#ifndef CS_STATIC_DLL  
	CADDLL = LoadLibrary("CADImage.dll"); 
	if (CADDLL) 
	{ 
		CADLayer = (CADLAYER) GetProcAddress(CADDLL, "CADLayer"); 
	    CADLayerCount = (CADLAYERCOUNT) GetProcAddress(CADDLL, "CADLayerCount"); 
	    CADLayerVisible = (CADLAYERVISIBLE) GetProcAddress(CADDLL, "CADLayerVisible"); 
	    CADVisible = (CADVISIBLE) GetProcAddress(CADDLL, "CADVisible"); 
		CreateCAD = (CREATECAD) GetProcAddress(CADDLL, "CreateCAD"); 
		CloseCAD  = (CLOSECAD)  GetProcAddress(CADDLL, "CloseCAD"); 
		CADLayout = (CADLAYOUT) GetProcAddress(CADDLL, "CADLayout"); 
		CADLayoutName = (CADLAYOUTNAME) GetProcAddress(CADDLL, "CADLayoutName"); 
		CADLayoutsCount = (CADLAYOUTSCOUNT) GetProcAddress(CADDLL, "CADLayoutsCount"); 
		CADLayoutVisible = (CADLAYOUTVISIBLE) GetProcAddress(CADDLL, "CADLayoutVisible"); 
		CurrentLayoutCAD = (CURRENTLAYOUTCAD) GetProcAddress(CADDLL, "CurrentLayoutCAD"); 
		DefaultLayoutIndex = (DEFAULTLAYOUTINDEX) GetProcAddress(CADDLL, "DefaultLayoutIndex"); 
		DrawCADEx = (DRAWCADEX) GetProcAddress(CADDLL, "DrawCADEx"); 
		DrawCADtoJpeg = (DRAWCADTOJPEG) GetProcAddress(CADDLL, "DrawCADtoJpeg"); 
		GetBoxCAD = (GETBOXCAD) GetProcAddress(CADDLL, "GetBoxCAD"); 
        GetExtentsCAD = (GETEXTENTSCAD) GetProcAddress(CADDLL, "GetExtentsCAD"); 
		GetIs3dCAD = (GETIS3DCAD) GetProcAddress(CADDLL, "GetIs3dCAD"); 
		GetPointCAD = (GETPOINTCAD) GetProcAddress(CADDLL, "GetPointCAD");		 
		ResetDrawingBoxCAD = (RESETDRAWINGBOXCAD) GetProcAddress(CADDLL, "ResetDrawingBoxCAD");          
		SetDrawingBoxCAD = (SETDRAWINGBOXCAD) GetProcAddress(CADDLL, "SetDrawingBoxCAD"); 
		SetProcessMessagesCAD = (SETPROCESSMESSAGESCAD)  GetProcAddress(CADDLL, "SetProcessMessagesCAD"); 
		SetRotateCAD = (SETROTATECAD) GetProcAddress(CADDLL, "SetRotateCAD"); 
	} 
	else 
	{ 
		CADLayer = NULL; 
	    CADLayerCount = NULL; 
	    CADLayerVisible = NULL; 
	    CADVisible = NULL;				 
		CloseCAD = NULL; 
		CreateCAD = NULL; 
		CADLayout = NULL; 
		CADLayoutName = NULL; 
		CADLayoutsCount = NULL; 
		CADLayoutVisible = NULL; 
		CurrentLayoutCAD = NULL; 
		DefaultLayoutIndex = NULL; 
		DrawCADEx = NULL; 
		DrawCADtoJpeg = NULL; 
		GetBoxCAD = NULL; 
		GetIs3dCAD = NULL; 
		GetPointCAD = NULL; 
		GetExtentsCAD = NULL; 
		ResetDrawingBoxCAD = NULL; 
		SetDrawingBoxCAD = NULL; 
        SetProcessMessagesCAD = NULL; 
		SetRotateCAD = NULL; 
		MessageBox(0, "CADImage.dll not loaded!", "Error", 0); 
	}     
#endif	 
} 
 
CMainWindow::~CMainWindow() 
{ 
#ifndef CS_STATIC_DLL 
	if (CADDLL) 
		FreeLibrary(CADDLL); 
#endif	 
} 
 
void CMainWindow::Load() 
{ 
#ifndef CS_STATIC_DLL 
	if (!CADDLL) return; 
#endif 
	char FileName [255] = ""; 
	if (!this->GetFile (this->hWnd, FileName, "CAD files (dwg, dxf, plt, hpg, hpgl)\0*.dwg;*.dxf;*.plt;*.hpg;*.hpgl\0", true)) 
		return;	 
	if (CADImage) 
		CloseCAD(CADImage); 
	CADImage = 0; 
    bRotated3D = false; 
	bIsRotated = false; 
	bIsDrawingBox = false;		 
#ifndef CS_STATIC_DLL 
	if (CADDLL) 
#endif 
	{			 
		SetCursor(curWait);		 
		CADImage = CreateCAD(hWnd, FileName); 
		RecalculateExtents(); 
		SetCursor(curDefault); 
		SetScale(100); 
		SetTextToStatusBar(FileName);		 
		FillLayersList(); 
		DoCreateComboBox(hInstance); 
		RePaint(); 
	} 
} 
 
void CMainWindow::RecalculateExtents() 
{ 
	oldPoint.x = 0; 
	oldPoint.y = 0; 
	offset.x = 0; 
	offset.y = TOOLBAR_SIZE; 
	GetExtentsCAD(CADImage, &frectExtentsCAD); 
	//fAbsHeight = frectExtentsCAD.Points.Top - frectExtentsCAD.Points.Bottom; 
	//fAbsWidth  = frectExtentsCAD.Points.Right - frectExtentsCAD.Points.Left; 
	GetBoxCAD(CADImage, &fAbsWidth, &fAbsHeight);		 
	fKoef = fAbsHeight / fAbsWidth; 
	ScaleRect.x = -1; 
} 
 
void CMainWindow::Draw() 
{ 
	CADDRAW CADDraw;     
	PAINTSTRUCT Paint; 
	HDC hDC;		 
 
	hDC = BeginPaint(hWnd, &Paint);	 
	 
	SetBgrndColor(colorBgrnd); 
 
	if ((CADImage)  
#ifndef CS_STATIC_DLL 
		&&  
		(DrawCADEx) 
#endif 
		) 
	{		 
        if (fAbsHeight > 0)// when loading the CAD file 
		{		 
			RECT Rect;			 
			sgFloat scale = (sgFloat) nScale / 100.0; 
			GetClientRect(hWnd, &Rect); 
			Rect.left = int(Rect.left  * scale) + offset.x; 
			Rect.right = int(Rect.right * scale) + offset.x; 
			Rect.top = int(Rect.top * scale) + offset.y;			 
			Rect.bottom = int(Rect.top  + (Rect.right - Rect.left) * fKoef);						 
			ScaleRect.x = fAbsWidth / (Rect.right - Rect.left); 
			ScaleRect.y = fAbsHeight / (Rect.bottom - Rect.top); 
			memset(&CADDraw, 0, sizeof(CADDRAW)); 
			CADDraw.Size = sizeof(CADDRAW);			 
			CADDraw.DrawMode = DrwMode; 
			CADDraw.DC = hDC; 
			CADDraw.R = Rect; 
			 
			DrawCADEx(CADImage, &CADDraw);			 
		}		 
	} 
	EndPaint(hWnd, &Paint);	 
} 
 
void CMainWindow::LButtonDown(POINTS Point) 
{ 
	if (CADImage)  
	{ 
		drag = true; 
		oldPoint = Point; 
		SetCursor(curHand);		 
		SetCapture(hWnd); 
	} 
} 
 
void CMainWindow::LButtonUp(POINTS Point) 
{ 
	drag = false; 
	SetCursor(curDefault);	 
	ReleaseCapture(); 
} 
 
void CMainWindow::MouseMove(POINTS Point) 
{ 
	if (drag) 
	{ 
		SetCursor(curHand); 
		offset.x = offset.x + Point.x - oldPoint.x; 
		offset.y = offset.y + Point.y - oldPoint.y;  
		oldPoint = Point; 
		RePaint(); 
	} 
	else if (CADImage)  
	{   
		SetCursor(curTarget); 
		DoMousePosition(Point);		 
	} 
} 
 
 
void CMainWindow::SetScale(int scale) 
{ 
	HMENU hMenu; 
	if ((nScale != scale) && (CADImage)) 
	{ 
		hMenu = GetMenu(hWnd); 
		if (nScale) 
			CheckMenuItem(hMenu, ID_SCALE + nScale, MF_BYCOMMAND | MF_UNCHECKED); 
		nScale = scale; 
		CheckMenuItem(hMenu, ID_SCALE + scale, MF_BYCOMMAND | MF_CHECKED); 
		RePaint(); 
	} 
} 
 
void CMainWindow::SaveAs() 
{ 
	CADDRAW CADDraw;	 
    float AbsWidht, AbsHeight, scale; 
	double Koef; 
		 
 
	if ((!CADImage)  
#ifndef CS_STATIC_DLL 
		|| (!CADDLL) 
#endif 
		)  
		return;		  
	char FileName [255] = ""; 
	 
	if (!this->GetFile (this->hWnd, FileName, "JPEG image\0*.jpg", false)) 
		return;	 
	memset(&CADDraw, 0, sizeof(CADDRAW));	 
	CADDraw.Size = sizeof(CADDraw); 
	scale = (float) nScale / 100; 
	GetBoxCAD(CADImage, &AbsWidht, &AbsHeight);	 
	GetClientRect(hWnd, &CADDraw.R); 
    if (AbsHeight != -1) 
	{     
		Koef = AbsHeight / AbsWidht; 
		CADDraw.R.top = 0; 
		CADDraw.R.left = 0; 
		CADDraw.R.bottom = int(CADDraw.R.bottom * scale); 
		CADDraw.R.right = int(CADDraw.R.right * scale); 
		CADDraw.R.bottom = int(CADDraw.R.top  + CADDraw.R.right * Koef);			  
		CADDraw.DrawMode = DrwMode; 
		HANDLE Hnd = DrawCADtoJpeg(CADImage, &CADDraw);		 
		if (Hnd)  
		{ 
			DWORD Size = GlobalSize(Hnd); 
			void *P = GlobalLock(Hnd); 
			HANDLE FHnd = CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_READ, 
				NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
			if (FHnd)  
			{ 
				DWORD Wrt; 
				WriteFile(FHnd, P, Size, &Wrt, NULL); 
				CloseHandle(FHnd);			 
			} 
			GlobalUnlock(Hnd);     
			GlobalFree(Hnd);	 
			 
		} 
	} 
 
} 
 
void CMainWindow::ChangeView(BYTE View) 
{ 
	HMENU hMenu; 
 
	if ((DrwMode != View) && (CADImage)) 
	{ 
		hMenu = GetMenu(hWnd);		 
		CheckMenuItem(hMenu, ID_VIEW + DrwMode + 1, MF_BYCOMMAND | MF_UNCHECKED); 
		DrwMode = View; 
		CheckMenuItem(hMenu, ID_VIEW + View + 1, MF_BYCOMMAND | MF_CHECKED); 
		RePaint(); 
	} 
} 
 
void CMainWindow::DoCreateStatusBar(HWND hwndParent, HINSTANCE hInst)  
{      
    ::InitCommonControls();  
 
    hwndStatusBar = CreateWindowEx(  
        0,                         
        STATUSCLASSNAME,           
        (LPCTSTR) NULL,            
        WS_CHILD | WS_BORDER, 
        0, 0, 0, 0,                
        hwndParent,                
        NULL,                      
        hInst,                     
        NULL);                     
 
	SplitStatusBar();     
	SetTextToStatusBar("Demo"); 	 
	ShowWindow(hwndStatusBar, SW_SHOWNORMAL); 
} 
 
void CMainWindow::SetTextToStatusBar(LPSTR str, int part) 
{ 
	SendMessage(hwndStatusBar, SB_SETTEXT, part, (LPARAM) str);  
} 
 
 
void CMainWindow::DoCreateToolBar(HWND hwndParent, HINSTANCE hInst)  
{	 
    TBADDBITMAP tbAddBitMap[QUANTITY_OF_BUTTONS];	 
    TBBUTTON tbButton[QUANTITY_OF_BUTTONS]; 
 
    ::InitCommonControls();  
    hwndToolBar = ::CreateWindowEx(  
        0,       
        TOOLBARCLASSNAME, 
        (LPCTSTR) NULL,   
        CCS_TOP | WS_CHILD,			  
        0, 0, 0, 0,         
        hwndParent,     
        NULL, 
        hInst,          
        NULL);          
     
    ::SendMessage(hwndToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON) , (LPARAM)0 ); 
	 
	 
	for (int i=0; i QUANTITY_OF_BUTTONS; i++)  
		tbAddBitMap[i].hInst = hInst; 
	tbAddBitMap[0].nID = IDB_BMORBITUPX; 
	tbAddBitMap[1].nID = IDB_BMORBITDOWNX; 
	tbAddBitMap[2].nID = IDB_BMORBITUPY; 
	tbAddBitMap[3].nID = IDB_BMORBITDOWNY; 
	tbAddBitMap[4].nID = IDB_BMORBITUPZ; 
	tbAddBitMap[5].nID = IDB_BMORBITDOWNZ; 
	tbAddBitMap[6].nID = IDB_BMOPTIONSLAYERS; 
	tbAddBitMap[7].nID = IDB_BMDRAWINGBOX; 
	tbButton[0].idCommand = IDM_ORBITUPX; 
	tbButton[1].idCommand = IDM_ORBITDOWNX; 
	tbButton[2].idCommand = IDM_ORBITUPY; 
	tbButton[3].idCommand = IDM_ORBITDOWNY; 
	tbButton[4].idCommand = IDM_ORBITUPZ; 
	tbButton[5].idCommand = IDM_ORBITDOWNZ; 
	tbButton[6].idCommand = IDM_OPTIONSLAYERS; 
	tbButton[7].idCommand = IDM_OPTIONSDRAWINGBOX; 
	::SendMessage(hwndToolBar, TB_SETBITMAPSIZE, (WPARAM) 0 , (LPARAM) MAKELONG(TOOLBAR_BUTTON_SIZE, TOOLBAR_BUTTON_SIZE));	 
     
	for (i=0; i QUANTITY_OF_BUTTONS; i++)  
	{ 
		tbButton[i].iBitmap = i; 
		tbButton[i].fsState = TBSTATE_ENABLED; 
	    tbButton[i].fsStyle = TBSTYLE_AUTOSIZE | TBSTYLE_BUTTON; 
	    tbButton[i].dwData = 0; 
		tbButton[i].iString = 0;		 
		::SendMessage(hwndToolBar, TB_ADDBITMAP, (WPARAM) 0 , (LPARAM)&tbAddBitMap[i]);		 
	} 
 
    ::SendMessage(hwndToolBar, TB_SETBUTTONSIZE, (WPARAM) 0 , (LPARAM) MAKELONG(TOOLBAR_BUTTON_SIZE, TOOLBAR_BUTTON_SIZE)); 
	::SendMessage(hwndToolBar, TB_ADDBUTTONS , (WPARAM)i , (LPARAM)&tbButton); 		 
	::SendMessage(hwndToolBar, TB_AUTOSIZE,   (WPARAM)0 , (LPARAM)0 ); 
	 
	::ShowWindow(hwndToolBar, SW_SHOWNORMAL);     
} 
 
void CMainWindow::DoCreateComboBox(HINSTANCE hInst)  
{	 
	if (!hwndToolBar) return; 
	if (CADImage)  
	{ 
		char LayoutName[100]; 
		int i, defaultLayout, Count;		 
		Count = CADLayoutsCount(CADImage); 
		defaultLayout = DefaultLayoutIndex(CADImage); 
		if (hwndComboBox)  
			DestroyWindow(hwndComboBox);		 
		hwndComboBox = ::CreateWindowEx(  
			0,       
			"COMBOBOX", 
			(LPCTSTR) NULL,   
			CBS_DROPDOWNLIST | WS_CHILD,			  
			(TOOLBAR_BUTTON_SIZE+8)*QUANTITY_OF_BUTTONS, 4, 120, 40+COMBOBOX_ITEM_SIZE*Count,         
			hwndToolBar,     
			NULL, 
			hInst,          
			NULL);		 
	    for (i= 0; i  Count; ++i) 
	    { 
//			CADLayoutName(CADImage, i, LayoutName, 100);			 
			::SendMessage(hwndComboBox, (UINT) CB_ADDSTRING, 0, (LPARAM) (LPCTSTR) LayoutName);			 
		} 
		::SendMessage(hwndComboBox, (UINT) CB_SETCURSEL, (WPARAM) defaultLayout, 0); 
		RePaint(); 
		::ShowWindow(hwndComboBox, SW_SHOWNORMAL); 
	}	 
} 
 
bool CMainWindow::SetCurrentLayout() 
{ 
    int lindex; 
    lindex = ::SendMessage(hwndComboBox, (UINT) CB_GETCURSEL, 0, 0); 
	ResetDrawingBox(); 
    CurrentLayoutCAD(CADImage, lindex, TRUE); 
	RecalculateExtents(); 
	SetScale(100); 
	RePaint(); 
	return true; 
} 
 
bool CMainWindow::Is3D() 
{ 
	int is3d; 
	GetIs3dCAD(CADImage, &is3d); 
	if (bRotated3D || (is3d == 1) ) 
		return true; 
	return false; 
} 
 
void CMainWindow::DoMousePosition(POINTS PointOnScr) 
{	 
	if ((!CADImage) || (ScaleRect.x  0 )) return; 
	FPOINT mousePt, newmousePt, pCenter, pX, pY;		 
	char str[36];	 
	if (Is3D()) 
	{ 
		SetTextToStatusBar("Is 3D drawing", 1); 
		return; 
	} 
	mousePt.x = frectExtentsCAD.Points.Left + (PointOnScr.x - offset.x) * ScaleRect.x; 
	mousePt.y = frectExtentsCAD.Points.Top  - (PointOnScr.y - offset.y) * ScaleRect.y; 
	pCenter.x = 0.5 * (frectExtentsCAD.Points.Right + frectExtentsCAD.Points.Left); 
	pCenter.y = 0.5 * (frectExtentsCAD.Points.Top + frectExtentsCAD.Points.Bottom); 
    pCenter.z = 0.5 * (frectExtentsCAD.Points.Z2 + frectExtentsCAD.Points.Z1); 
	pX.x = 1; pX.y = 0;	pX.z = 0; 
	pY.x = 0; pY.y = 1;	pY.z = 0;		 
	GetPointCAD(CADImage, &pX); 
	GetPointCAD(CADImage, &pY); 
	MoveToPosition(&mousePt, pCenter, -1); 
	newmousePt.x = mousePt.x * pX.x + mousePt.y * pX.y; 
    newmousePt.y = mousePt.x * pY.x + mousePt.y * pY.y; 
	MoveToPosition(&newmousePt, pCenter, +1); 
	sprintf(str, "(%6.6f, %6.6f)\0", newmousePt.x, newmousePt.y); 
		 
	SetTextToStatusBar(str, 1); 
} 
 
void CMainWindow::SplitStatusBar(int nParts) 
{ 
	RECT rcClient;  
    HLOCAL hloc;  
    LPINT lpParts;  
    int i, nWidth;  
 
	 
    GetClientRect(hWnd, &rcClient);  
  
  
    hloc = LocalAlloc(LHND, sizeof(int) * nParts);  
    lpParts = (LPINT)LocalLock(hloc);  
  
 
    nWidth = rcClient.right / nParts;  
    for (i = 0; i  nParts; i++)  
	{  
        lpParts[i] = nWidth;  
        nWidth+= nWidth;  
    } 	 
	 
    ::SendMessage(hwndStatusBar, SB_SETPARTS, (WPARAM) nParts, (LPARAM) lpParts); 	 
 
    LocalUnlock(hloc);  
    LocalFree(hloc);    
} 
 
void CMainWindow::ReSize(WPARAM wParam,LPARAM lParam) 
{ 
	SplitStatusBar(); 
  	::SendMessage(hwndStatusBar, WM_SIZE, wParam, lParam); 
	::SendMessage(hwndToolBar, TB_AUTOSIZE, (WPARAM)0 , (LPARAM)0 ); 
} 
 
void CMainWindow::RotateCAD(const AXES axis, const float angle) 
{ 
	if (CADImage && !bIsDrawingBox)  
	{		 
		SetRotateCAD(CADImage, angle, int(axis)); 
		bIsRotated = true; 
		if (axis != axisZ) 
		  bRotated3D = true; 
		RePaint(); 
	} 
} 
 
void CMainWindow::SetBgrndColor(const COLORREF color) 
{ 
	colorBgrnd = color; 
	RECT rect; 
	GetClientRect(hWnd, &rect); 
	LOGBRUSH brush; 
	brush.lbStyle = BS_SOLID; 
	brush.lbColor = colorBgrnd; 
	brush.lbHatch = 0; 
	brushBackground = CreateBrushIndirect(&brush);	 
	FillRect(GetDC(hWnd), &rect, brushBackground); 
	RePaint(); 
} 
 
void CMainWindow::ShowLayersDlg(bool Visible) 
{		 
	if(hwndLayersDlg == NULL) 
		hwndLayersDlg = CreateDialog(hInstance, (LPCTSTR)IDD_LAYERS, hWnd, (DLGPROC) LayersDialogProc);	 
	if (Visible) 
	{ 
		ShowWindow(hwndLayersDlg, SW_SHOW); 
		SetFocus(hwndLayersDlg); 
	} 
	else 
	{ 
		ShowWindow(hwndLayersDlg, SW_HIDE); 
		SetFocus(hWnd); 
	} 
	 
} 
 
void CMainWindow::ResetDrawingBox() 
{ 
	if ((CADImage != NULL) && bIsDrawingBox && !bIsRotated) 
	{ 
		ResetDrawingBoxCAD(CADImage);		 
		RecalculateExtents(); 
		bIsDrawingBox = false; 
		RePaint();		 
	}		 
} 
 
void CMainWindow::SetDrawingBox() 
{	 
	if ((CADImage != NULL) && !bIsRotated) 
	{		 
		rectDrawingBox.Points.Left = (frectExtentsCAD.Points.Left + frectExtentsCAD.Points.Right)/2; 
		rectDrawingBox.Points.Top =  frectExtentsCAD.Points.Top; 
		rectDrawingBox.Points.Z1 = 0;   
		rectDrawingBox.Points.Right = frectExtentsCAD.Points.Right;  
		rectDrawingBox.Points.Bottom = frectExtentsCAD.Points.Bottom;  
		rectDrawingBox.Points.Z2 = 0; 		 
		SetDrawingBoxCAD(CADImage, &rectDrawingBox); 
		RecalculateExtents(); 
		bIsDrawingBox = true;		 
		RePaint(); 
	}	 
} 
 
void CMainWindow::DestroyLayersDlg() 
{ 
	if (hwndLayersDlg != NULL) 
	{ 
		DestroyWindow(hwndLayersDlg); 
		hwndLayersDlg = NULL; 
	} 
} 
 
 
void CMainWindow::FillLayersList() 
{ 
	if (CADImage == NULL)  
		return; 
	DestroyLayersDlg(); 
	ShowLayersDlg(false); 
	int i, Count, Vis;		 
	LVCOLUMN lvCol; 
	LVITEM lvItem; 
	HANDLE hLayer; 
	HWND hwndList; 
	DXFDATA dxfData;	 
	Count = CADLayerCount(CADImage); 
	hwndList = GetDlgItem(hwndLayersDlg, IDC_LISTLAYERS);	 
	ListView_DeleteAllItems(hwndList); 
	ZeroMemory(&lvCol, sizeof(lvCol)); 
    // Initialize the columns 
	lvCol.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; 
    lvCol.fmt = LVCFMT_LEFT;  
    lvCol.cx = 235; 	 
    lvCol.pszText = "Layers names"; 	 
	ListView_InsertColumn(hwndList, 0, &lvCol); 
	ListView_SetItemCount(hwndList, Count); 
	IsAppChangingList = true; 
	ListView_SetExtendedListViewStyle(hwndList, LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES);	 
	for (i= 0; i Count; i++) 
	{		 
//		hLayer = CADLayer(CADImage, i, &dxfData); 
		Vis = (dxfData.Flags & 1) == 0; 
		ZeroMemory(&lvItem, sizeof(lvItem));  
		lvItem.mask = LVIF_TEXT | LVIF_PARAM;		 
		lvItem.iItem = i;  
		lvItem.iSubItem = 0;  
		lvItem.pszText = dxfData.Text; 
		lvItem.lParam = (LPARAM) hLayer;		 
		ListView_InsertItem(hwndList, &lvItem);		 
		ListView_SetItemState(hwndList, i, INDEXTOSTATEIMAGEMASK(UINT((Vis)+1)), LVIS_STATEIMAGEMASK); 
	}	 
	IsAppChangingList = false; 
} 
 
/*  
  LayersDialogProc 
   
 
*/ 
BOOL CALLBACK CMainWindow::LayersDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{	 
	if (hDlg != hwndLayersDlg) 
		return FALSE; 
	HWND hwndMainWnd = GetParent(hwndLayersDlg); 
 
	switch (message) 
	{ 
		case WM_INITDIALOG:			 
			return TRUE; 
 
		case WM_COMMAND:			 
			switch (LOWORD(wParam)) 
			{ 
				case IDOK: 
				case IDCANCEL: 
					CheckMenuItem(GetMenu(hwndMainWnd), IDM_OPTIONSLAYERS, MF_UNCHECKED); 
					ShowWindow(hwndLayersDlg, SW_HIDE);					 
					break; 
			} 
		case WM_MOVE: 
			InvalidateRect(hwndMainWnd, NULL, TRUE); 
			break;		 
		case WM_NOTIFY: 
			switch (wParam) 
			{	 
				case IDC_LISTLAYERS: 
					NMHDR nmHdr = *((LPNMHDR) lParam); 
					if (!IsAppChangingList && (nmHdr.code == LVN_ITEMCHANGED)) 
					{						 
						NMLISTVIEW nmListViewItem = *((LPNMLISTVIEW)lParam); 
						int Vis = (::SendMessage(GetDlgItem(hwndLayersDlg, IDC_LISTLAYERS), LVM_GETITEMSTATE, nmListViewItem.iItem, LVIS_STATEIMAGEMASK) >> 12) - 1; 
//						CADLayerVisible((HANDLE)nmListViewItem.lParam, Vis);						 
						InvalidateRect(hwndMainWnd, NULL, TRUE); 
					}					 
				break; 
			}		 
	} 
    return FALSE; 
}