Skip to content

Commit

Permalink
[#83] Axes and charting (Part 19: Added command to take a screenshot …
Browse files Browse the repository at this point in the history
…of a chart)
  • Loading branch information
tomas-nestorovic committed Nov 9, 2024
1 parent 512990b commit 14cdfc9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions Main/res/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ BEGIN
MENUITEM "Show percentile...\tCtrl+4", ID_NUMBER
MENUITEM SEPARATOR
MENUITEM "Snap to nearest item", ID_ALIGN
MENUITEM "Save screenshot as...", ID_FILE_SAVE_AS
MENUITEM SEPARATOR
MENUITEM "Close\tEsc", IDCANCEL
END
Expand Down
50 changes: 50 additions & 0 deletions Main/src/Charting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,41 @@ namespace Charting
return snapped.graphics ? &snapped : nullptr;
}

namespace Screenshot
{
struct TParams sealed{
const CString fileName;
CChartView &cv;

TParams(const CString &fileName,CChartView &cv)
: fileName(fileName) , cv(cv) {
}
};

static UINT AFX_CDECL Thread(PVOID pCancelableAction){
// thread to take a screenshot of current state of Chart
CBackgroundActionCancelable &bac=*(PBackgroundActionCancelable)pCancelableAction;
TParams &p=*(TParams *)bac.GetParams();
CChartView &cv=p.cv;
const CClientDC dc(&cv);
const Utils::TClientRect rc(cv);
const CRect rcEmf( 0, 0, // see MSDN "Creating an Enhanced Metafile" article
rc.Width()*100*::GetDeviceCaps(dc,HORZSIZE)/::GetDeviceCaps(dc,HORZRES),
rc.Height()*100*::GetDeviceCaps(dc,VERTSIZE)/::GetDeviceCaps(dc,VERTRES)
);
if (const HDC dcMem=::CreateEnhMetaFile( dc, p.fileName, rcEmf, nullptr )){
cv.painter.Draw( dcMem, rc, bac );
if (const HENHMETAFILE h=::CloseEnhMetaFile(dcMem))
if (::DeleteEnhMetaFile(h))
if (bac.Cancelled){
::DeleteFile(p.fileName);
return ERROR_CANCELLED;
}else
return bac.TerminateWithSuccess();
}
return bac.TerminateWithError( ::GetLastError() );
}
}
bool CChartView::CDisplayInfo::OnCmdMsg(CChartView &cv,UINT nID,int nCode,PVOID pExtra){
// command processing
switch (nCode){
Expand All @@ -279,6 +314,8 @@ namespace Charting
pCmdUi->SetCheck( containsSnappableGraphics && snapToNearestItem );
return true;
}
case ID_FILE_SAVE_AS:
return true;
}
break;
}
Expand All @@ -290,6 +327,19 @@ namespace Charting
snapped.graphics=nullptr;
cv.Invalidate();
return true;
case ID_FILE_SAVE_AS:{
// save screenshot as
const Screenshot::TParams sp(
Utils::DoPromptSingleTypeFileName( _T(""), _T("Windows Metafile (*.wmf)|*.wmf|"), 0 ),
cv
);
if (sp.fileName.IsEmpty())
return true;
if (const TStdWinError err=CBackgroundActionCancelable( Screenshot::Thread, &sp, THREAD_PRIORITY_ABOVE_NORMAL ).Perform())
if (err!=ERROR_CANCELLED)
Utils::Information( _T("Screenshot creation failed"), err );
return true;
}
}
break;
}
Expand Down

0 comments on commit 14cdfc9

Please sign in to comment.