Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Региональный поиск метод квадрантного дерева / src / MainFrm
.cpp// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "QTree.h"
#include "QTreeView.h"
#include "QTreeDoc.h"
#include <string.h>
#include "DlgListPoint.h"
#include "DlgListFoundingPoints.h"
#include "DlgRect.h"
#include "GenerateDialog.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int mouseX,mouseY;
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_MOUSEMOVE()
ON_NOTIFY(UDN_DELTAPOS,IDC_SPIN1,OnDeltaPos1)
ON_NOTIFY(UDN_DELTAPOS,IDC_SPIN2,OnDeltaPos2)
ON_NOTIFY(UDN_DELTAPOS,IDC_SPIN3,OnDeltaPos3)
ON_NOTIFY(UDN_DELTAPOS,IDC_SPIN4,OnDeltaPos4)
ON_UPDATE_COMMAND_UI(ID_XYMOUSE,OnUpdateXYMouse)
ON_COMMAND(ID_EDIT_SETRECTANGLE, OnEditSetrectangle)
ON_COMMAND(ID_VIEW_LISTOFTHEFOUNDINGPOINTS, OnViewListofthefoundingpoints)
ON_BN_CLICKED(IDC_CHECKAUTO, OnCheckAutoBuildQTree)
ON_COMMAND(ID_RUN_RUNNEWQTREE, OnRunRunnewqtree)
ON_COMMAND(ID_RUN_RUNFOUNDING, OnRunRunfounding)
ON_UPDATE_COMMAND_UI(ID_RUN_RUNFOUNDING, OnUpdateRunRunfounding)
ON_COMMAND(ID_BUILDBYSTEPS, OnBuildbysteps)
ON_UPDATE_COMMAND_UI(ID_BUILDLEFT, OnUpdateBuildleft)
ON_UPDATE_COMMAND_UI(ID_BUILDRIGHT, OnUpdateBuildright)
ON_COMMAND(ID_FINDBYSTEPS, OnFindbysteps)
ON_UPDATE_COMMAND_UI(ID_FINDLEFT, OnUpdateFindleft)
ON_UPDATE_COMMAND_UI(ID_FINDRIGHT, OnUpdateFindright)
ON_UPDATE_COMMAND_UI(ID_EDIT_CHANGELISTOFPOINTS, OnUpdateEditChangelistofpoints)
ON_UPDATE_COMMAND_UI(ID_RUN_RUNNEWQTREE, OnUpdateRunRunnewqtree)
ON_UPDATE_COMMAND_UI(ID_VIEW_LISTOFTHEFOUNDINGPOINTS, OnUpdateViewListofthefoundingpoints)
ON_UPDATE_COMMAND_UI(ID_EDIT_SETRECTANGLE, OnUpdateEditSetrectangle)
ON_UPDATE_COMMAND_UI(ID_BUILDBYSTEPS, OnUpdateBuildbysteps)
ON_UPDATE_COMMAND_UI(ID_FINDBYSTEPS, OnUpdateFindbysteps)
ON_COMMAND(ID_BUILDLEFT, OnBuildleft)
ON_COMMAND(ID_BUILDRIGHT, OnBuildright)
ON_COMMAND(ID_FINDLEFT, OnFindleft)
ON_COMMAND(ID_FINDRIGHT, OnFindright)
ON_COMMAND(ID_RUN_GENERATERANDOMPOINTS, OnRunGeneraterandompoints)
ON_UPDATE_COMMAND_UI(ID_RUN_GENERATERANDOMPOINTS, OnUpdateRunGeneraterandompoints)
ON_COMMAND(ID_BUILDRIGHTMULT, OnBuildrightmult)
ON_UPDATE_COMMAND_UI(ID_BUILDRIGHTMULT, OnUpdateBuildrightmult)
ON_WM_TIMER()
ON_COMMAND(ID_FINDRIGHTMULT, OnFindrightmult)
ON_UPDATE_COMMAND_UI(ID_FINDRIGHTMULT, OnUpdateFindrightmult)
ON_COMMAND(ID_FILE_SAVEASBMP, OnFileSaveasbmp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_XYMOUSE
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_TOOLTIPS | CBRS_FLYBY) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1;
}
m_wndToolBar.SetButtonStyle(4,TBBS_CHECKBOX);
m_wndToolBar.SetButtonStyle(10,TBBS_CHECKBOX);
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create statusbar\n");
return -1;
}
if (!m_wndDlgBar.Create(this,IDD_DLGBAR,
CBRS_RIGHT|CBRS_FLYBY|CBRS_TOOLTIPS,
IDD_DLGBAR))
{
TRACE0("Failed to create dialogbar\n");
return -1;
}
CButton *check=(CButton*)m_wndDlgBar.GetDlgItem(IDC_CHECKAUTO);
check->SetCheck(0);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
//отображаем окно во весь экран
int dx=GetSystemMetrics(SM_CXSCREEN);
int dy=GetSystemMetrics(SM_CYSCREEN);
cs.x=0;
cs.y=0;
cs.cx=dx;
cs.cy=dy-40;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnMouseMove(UINT nFlags, CPoint point)
{
mouseX=point.x;
mouseY=point.y;
CFrameWnd::OnMouseMove(nFlags, point);
}
void CMainFrame::OnUpdateXYMouse(CCmdUI* pCmdUI)
{
std::ostrstream ss;
ss << "X=" << mouseX << ",Y=" << mouseY << std::ends;
pCmdUI->SetText(ss.str());
}
void CMainFrame::OnDeltaPos1(NMHDR* pNMHDR,LRESULT* pResult)
{
NM_UPDOWN *pNMUpDown=(NM_UPDOWN*)pNMHDR;
if (pNMUpDown->iDelta==-1)
pNMUpDown->iDelta=pNMUpDown->iPos/-2;
else
pNMUpDown->iDelta=pNMUpDown->iPos;
*pResult=FALSE;
int tmp=pNMUpDown->iPos+pNMUpDown->iDelta;
//получаем указатель на объект "представление"
CQTreeView *pView=(CQTreeView*)GetActiveView();
if (tmp<pView->m_NumCells)
{
MessageBox("Size of the grid must >= number of the cells!",
"Error");
*pResult=TRUE;
}
else
if (tmp<=pView->m_maxSizeGrid)
{
pView->m_SizeGrid=tmp;
//отменяем режим демонстрации дерева вследствии
//необходимости его изменения
pView->m_ShowQTree=FALSE;
//удаляем все точки предыдущего поиска
while(pView->listPointsInRect.Lenght()>0)
{
pView->listPointsInRect.First();
pView->listPointsInRect.Remove();
}
CQTreeDoc *pDoc=pView->GetDocument();
//обновляем данные документа
pDoc->m_SizeGrid=tmp;
pDoc->SetModifiedFlag();
pView->Invalidate();
}
}
void CMainFrame::OnDeltaPos2(NMHDR* pNMHDR,LRESULT* pResult)
{
NM_UPDOWN *pNMUpDown=(NM_UPDOWN*)pNMHDR;
if (pNMUpDown->iDelta==-1)
pNMUpDown->iDelta=pNMUpDown->iPos/-2;
else
pNMUpDown->iDelta=pNMUpDown->iPos;
*pResult=FALSE;
int tmp=pNMUpDown->iPos+pNMUpDown->iDelta;
//получаем указатель на объект "представление"
CQTreeView *pView=(CQTreeView*)GetActiveView();
if (tmp>pView->m_SizeGrid)
{
MessageBox("Number of the cells must <= size of the grid!",
"Error");
*pResult=TRUE;
}
else
if (tmp<=pView->m_maxNumCells)
{
pView->m_NumCells=tmp;
//отменяем режим демонстрации дерева вследствии
//необходимости его изменения
pView->m_ShowQTree=FALSE;
//удаляем все точки предыдущего поиска
while(pView->listPointsInRect.Lenght()>0)
{
pView->listPointsInRect.First();
pView->listPointsInRect.Remove();
}
CQTreeDoc *pDoc=pView->GetDocument();
//обновляем данные документа
pDoc->m_NumCells=tmp;
pDoc->SetModifiedFlag();
pView->Invalidate();
}
}
void CMainFrame::OnDeltaPos3(NMHDR* pNMHDR,LRESULT* pResult)
{
NM_UPDOWN *pNMUpDown=(NM_UPDOWN*)pNMHDR;
*pResult=FALSE;
int tmp=pNMUpDown->iPos+pNMUpDown->iDelta;
if (tmp==0) *pResult=TRUE;
else
{
//получаем указатель на объект "представление"
CQTreeView *pView=(CQTreeView*)GetActiveView();
pView->M=tmp;
//отменяем режим демонстрации дерева вследствии
//необходимости его изменения
pView->m_ShowQTree=FALSE;
//удаляем все точки предыдущего поиска
while(pView->listPointsInRect.Lenght()>0)
{
pView->listPointsInRect.First();
pView->listPointsInRect.Remove();
}
CQTreeDoc *pDoc=pView->GetDocument();
//обновляем данные документа
pDoc->M=tmp;
pDoc->SetModifiedFlag();
pView->Invalidate();
}
}
void CMainFrame::OnDeltaPos4(NMHDR* pNMHDR,LRESULT* pResult)
{
NM_UPDOWN *pNMUpDown=(NM_UPDOWN*)pNMHDR;
*pResult=FALSE;
int tmp=pNMUpDown->iPos+pNMUpDown->iDelta;
if (tmp==0) *pResult=TRUE;
else
{
//получаем указатель на объект "представление"
CQTreeView *pView=(CQTreeView*)GetActiveView();
pView->D=tmp;
//отменяем режим демонстрации дерева вследствии
//необходимости его изменения
pView->m_ShowQTree=FALSE;
//удаляем все точки предыдущего поиска
while(pView->listPointsInRect.Lenght()>0)
{
pView->listPointsInRect.First();
pView->listPointsInRect.Remove();
}
CQTreeDoc *pDoc=pView->GetDocument();
//обновляем данные документа
pDoc->D=tmp;
pDoc->SetModifiedFlag();
pView->Invalidate();
}
}
void CMainFrame::OnEditSetrectangle()
{
CDlgRect dlg;
dlg.DoModal();
}
void CMainFrame::OnViewListofthefoundingpoints()
{
CDlgListFoundingPoints dlg;
dlg.DoModal();
}
void CMainFrame::OnCheckAutoBuildQTree()
{
//получаем указатель на объект "представление"
CQTreeView *pView=(CQTreeView*)GetActiveView();
CButton *check=(CButton*)m_wndDlgBar.GetDlgItem(IDC_CHECKAUTO);
if (check->GetCheck()==0)
pView->m_CheckAutoBuildQTree=FALSE;
else
pView->m_CheckAutoBuildQTree=TRUE;
pView->Invalidate();
}
void CMainFrame::OnRunRunnewqtree()
{
//получаем указатель на объект "представление"
CQTreeView *pView=(CQTreeView*)GetActiveView();
//устанавливаем признак обновленного дерева
pView->m_RunBuildQTree=TRUE;
pView->m_ShowQTree=TRUE;
pView->Invalidate();
}
void CMainFrame::OnRunRunfounding()
{
//получаем указатель на объект "представление"
CQTreeView *pView=(CQTreeView*)GetActiveView();
//указываем на проведегие поиска по области
pView->m_RunFounding=TRUE;
pView->Invalidate();
}
void CMainFrame::OnUpdateRunRunfounding(CCmdUI* pCmdUI)
{
//получаем указатель на объект "представление"
CQTreeView *pView=(CQTreeView*)GetActiveView();
if((pView->m_BuildQTreeBySteps)||(pView->m_FindBySteps))
pCmdUI->Enable(FALSE);
else
{
if (pView->m_ShowQTree || pView->m_CheckAutoBuildQTree)
pCmdUI->Enable();
else
pCmdUI->Enable(FALSE);
}
}
void CMainFrame::OnBuildbysteps()
{
//получаем указатель на объект "представление"
CQTreeView *pView=(CQTreeView*)GetActiveView();
if(pView->m_BuildQTreeBySteps)
{
pView->m_BuildQTreeBySteps=FALSE;
CButton *check=(CButton*)m_wndDlgBar.GetDlgItem(IDC_CHECKAUTO);
check->EnableWindow();
CSpinButtonCtrl *pSpin;
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN1);
pSpin->EnableWindow();
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN2);
pSpin->EnableWindow();
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN3);
pSpin->EnableWindow();
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN4);
pSpin->EnableWindow();
}
else
{
pView->m_BuildQTreeBySteps=TRUE;
pView->maxIndexBuildBySteps=0;
CButton *check=(CButton*)m_wndDlgBar.GetDlgItem(IDC_CHECKAUTO);
check->EnableWindow(FALSE);
CSpinButtonCtrl *pSpin;
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN1);
pSpin->EnableWindow(FALSE);
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN2);
pSpin->EnableWindow(FALSE);
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN3);
pSpin->EnableWindow(FALSE);
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN4);
pSpin->EnableWindow(FALSE);
}
pView->Invalidate();
}
void CMainFrame::OnUpdateBuildleft(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if(pView->m_BuildQTreeBySteps)
pCmdUI->Enable();
else
pCmdUI->Enable(FALSE);
}
void CMainFrame::OnUpdateBuildright(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if(pView->m_BuildQTreeBySteps)
pCmdUI->Enable();
else
pCmdUI->Enable(FALSE);
}
void CMainFrame::OnFindbysteps()
{
//получаем указатель на объект "представление"
CQTreeView *pView=(CQTreeView*)GetActiveView();
if(pView->m_FindBySteps)
{
pView->m_FindBySteps=FALSE;
while (pView->listPointsInRect.Lenght()>0)
{
pView->listPointsInRect.First();
pView->listPointsInRect.Remove();
}
CButton *check=(CButton*)m_wndDlgBar.GetDlgItem(IDC_CHECKAUTO);
check->EnableWindow();
CSpinButtonCtrl *pSpin;
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN1);
pSpin->EnableWindow();
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN2);
pSpin->EnableWindow();
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN3);
pSpin->EnableWindow();
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN4);
pSpin->EnableWindow();
}
else
{
pView->m_FindBySteps=TRUE;
pView->maxIndexFindBySteps=0;
CButton *check=(CButton*)m_wndDlgBar.GetDlgItem(IDC_CHECKAUTO);
check->EnableWindow(FALSE);
CSpinButtonCtrl *pSpin;
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN1);
pSpin->EnableWindow(FALSE);
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN2);
pSpin->EnableWindow(FALSE);
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN3);
pSpin->EnableWindow(FALSE);
pSpin=(CSpinButtonCtrl*)m_wndDlgBar.GetDlgItem(IDC_SPIN4);
pSpin->EnableWindow(FALSE);
}
pView->Invalidate();
}
void CMainFrame::OnUpdateFindleft(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if(pView->m_FindBySteps)
pCmdUI->Enable();
else
pCmdUI->Enable(FALSE);
}
void CMainFrame::OnUpdateFindright(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if(pView->m_FindBySteps)
pCmdUI->Enable();
else
pCmdUI->Enable(FALSE);
}
void CMainFrame::OnUpdateEditChangelistofpoints(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if((pView->m_BuildQTreeBySteps)||(pView->m_FindBySteps))
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable();
}
void CMainFrame::OnUpdateRunRunnewqtree(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if((pView->m_BuildQTreeBySteps)||(pView->m_FindBySteps))
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable();
}
void CMainFrame::OnUpdateViewListofthefoundingpoints(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if((pView->m_BuildQTreeBySteps)||(pView->m_FindBySteps))
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable();
}
void CMainFrame::OnUpdateEditSetrectangle(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if((pView->m_BuildQTreeBySteps)||(pView->m_FindBySteps))
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable();
}
void CMainFrame::OnUpdateBuildbysteps(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if (pView->m_ShowQTree || pView->m_CheckAutoBuildQTree)
{
if (!pView->m_FindBySteps)
pCmdUI->Enable();
else
pCmdUI->Enable(FALSE);
}
else
pCmdUI->Enable(TRUE);// DF: was false
}
void CMainFrame::OnUpdateFindbysteps(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if (pView->m_ShowQTree || pView->m_CheckAutoBuildQTree)
{
if (!pView->m_BuildQTreeBySteps)
pCmdUI->Enable();
else
pCmdUI->Enable(FALSE);
}
else
pCmdUI->Enable(FALSE);
}
void CMainFrame::OnBuildleft()
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
pView->maxIndexBuildBySteps--;
if (pView->maxIndexBuildBySteps<0)
pView->maxIndexBuildBySteps=0;
pView->Invalidate();
}
void CMainFrame::OnBuildright()
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
pView->maxIndexBuildBySteps++;
pView->Invalidate();
}
void CMainFrame::OnFindleft()
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
pView->maxIndexFindBySteps--;
if (pView->maxIndexFindBySteps<0)
pView->maxIndexFindBySteps=0;
pView->Invalidate();
}
void CMainFrame::OnFindright()
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
pView->maxIndexFindBySteps++;
pView->Invalidate();
}
void CMainFrame::OnRunGeneraterandompoints()
{
CGenerateDialog dlg;
dlg.DoModal();
CQTreeView *pView=(CQTreeView*)GetActiveView();
POINTTYPE p1,p2;
srand((unsigned)time(NULL));
for(int i=1;i<=dlg.number;i++)
{
p1=rand()%(pView->m_SizeGrid);
p2=rand()%(pView->m_SizeGrid);
pView->listPoints.Append(ELEMENT(p1,p2));
}
pView->Invalidate();
}
void CMainFrame::OnUpdateRunGeneraterandompoints(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if((pView->m_BuildQTreeBySteps)||(pView->m_FindBySteps))
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable();
}
void CMainFrame::OnBuildrightmult()
{
SetTimer(0,500,NULL);
CQTreeView *pView=(CQTreeView*)GetActiveView();
pView->MaxBQTree=FALSE;
pView->maxIndexBuildBySteps=0;
}
void CMainFrame::OnUpdateBuildrightmult(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if(pView->m_BuildQTreeBySteps)
pCmdUI->Enable();
else
pCmdUI->Enable(FALSE);
}
void CMainFrame::OnTimer(UINT nIDEvent)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
switch (nIDEvent)
{
case 0: {
CMainFrame::OnBuildright();
if ((pView->m_BuildQTreeBySteps==FALSE)||(pView->MaxBQTree))
{
KillTimer(0);
pView->maxIndexBuildBySteps=-1;
}
break;
}
case 1: {
CMainFrame::OnFindright();
if ((pView->m_FindBySteps==FALSE)||(pView->MaxFind))
{
KillTimer(1);
pView->maxIndexFindBySteps=-1;
}
break;
}
}
CFrameWnd::OnTimer(nIDEvent);
}
void CMainFrame::OnFindrightmult()
{
SetTimer(1,500,NULL);
CQTreeView *pView=(CQTreeView*)GetActiveView();
pView->MaxFind=FALSE;
pView->maxIndexBuildBySteps=0;
}
void CMainFrame::OnUpdateFindrightmult(CCmdUI* pCmdUI)
{
CQTreeView *pView=(CQTreeView*)GetActiveView();
if(pView->m_FindBySteps)
pCmdUI->Enable();
else
pCmdUI->Enable(FALSE);
}
HANDLE DDBToDIB( CBitmap& bitmap, DWORD dwCompression, CPalette* pPal )
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDIB;
HANDLE handle;
HDC hDC;
HPALETTE hPal;
ASSERT( bitmap.GetSafeHandle() );
// The function has no arg for bitfields
if( dwCompression == BI_BITFIELDS )
return NULL;
// If a palette has not been supplied use defaul palette
hPal = (HPALETTE) pPal->GetSafeHandle();
if (hPal==NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
// Get bitmap information
//bitmap.GetObject(sizeof(bm),(LPSTR)&bm);
bitmap.GetBitmap(&bm);
// Initialize the bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = bm.bmPlanes;//1;
bi.biBitCount = bm.bmBitsPixel;//bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// Compute the size of the infoheader and the color table
int nColors = (1 << bi.biBitCount);
if( nColors > 256 )
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD);
// We need a device context to get the DIB from
hDC = GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC);
// Allocate enough memory to hold bitmapinfoheader and color table
hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
if (!hDIB){
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)hDIB;
*lpbi = bi;
// Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
bi = *lpbi;
// If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight;
// If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if (dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
}
// Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
hDIB = handle;
else{
GlobalFree(hDIB);
// Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}
// Get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDIB;
// FINALLY get the DIB
BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
0L, // Start scan line
(DWORD)bi.biHeight, // # of scan lines
(LPBYTE)lpbi // address for bitmap bits
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // address of bitmapinfo
(DWORD)DIB_RGB_COLORS); // Use RGB for color table
if( !bGotBits )
{
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return hDIB;
}
BOOL WriteDIB( LPCTSTR szFile, HANDLE hDIB)
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;
if (!hDIB)
return FALSE;
CFile file;
if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
return FALSE;
lpbi = (LPBITMAPINFOHEADER)hDIB;
int nColors = 1 << lpbi->biBitCount;
if( nColors > 256 )
nColors = 0;
// Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD));
// Write the file header
file.Write( &hdr, sizeof(hdr) );
// Write the DIB header and the bits
file.Write( lpbi, GlobalSize(hDIB) );
return TRUE;
}
BOOL WriteWindowToDIB( LPCTSTR szFile, CRect rect, CWnd *pWnd)
{
CBitmap bitmap;
CWindowDC dc(pWnd);
CDC memDC;
;
memDC.CreateCompatibleDC(&dc);
bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() );
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
memDC.BitBlt(0,0, rect.Width(),rect.Height(), &dc, rect.left+2, rect.top+2, SRCCOPY);
// Create logical palette if device support a palette
CPalette pal;
if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
{
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;
pLP->palNumEntries =
GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry );
// Create the palette
pal.CreatePalette( pLP );
delete[] pLP;
}
memDC.SelectObject(pOldBitmap);
// Convert the bitmap to a DIB
HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, &pal );
if( hDIB == NULL )
return FALSE;
// Write it to file
WriteDIB( szFile, hDIB );
// Free the memory allocated by DDBToDIB for the DIB
GlobalFree( hDIB );
return TRUE;
}
void CMainFrame::OnFileSaveasbmp()
{
CFileDialog dlg(FALSE, ".bmp", "screenshot.bmp",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"Windows bitmap (*.bmp)|*.bmp|All files (*.*)|*.*||");
dlg.DoModal();
CRect rec(0,0,0,0);
CQTreeView *pView=(CQTreeView*)GetActiveView();
pView->GetClientRect(&rec);
WriteWindowToDIB(dlg.GetPathName(),rec,pView);
}
Соседние файлы в папке src