Navigation:  »No topics above this level«

DPlot Output Destination

Print this Topic Previous pageReturn to chapter overviewNext page

By default all calls to DPLOTLIB functions result in graphics operations being performed within a document window owned by DPlot. But if you would prefer that a plot be drawn within a window owned by your own application, there are several options. In all cases before sending any data to DPlot you should first call DPlot_Start with a non-zero Hide parameter so that dplot.exe (or dplotjr.exe) will be hidden. You should also take care to shut down DPlot (if it was not already running) when your application is terminated. Listed below are three different methods of directing DPlot's output to your own application's window. The examples below are for C, but should be easily understandable for other languages.

Plot to a bitmap, caller responsible for drawing the bitmap
See, for example, the btest2 and ctest2 (VB6 and C) example programs.

// Global data:

int DocNum=0;

// Global variable initialized to (HBITMAP)NULL at startup,
// and, if non-null, deleted at exit with

// DeleteObject(hbmp);

HBITMAP        hbmp;

// Dimensions of the bitmap we'll retrieve from DPlot

int cxBitmap, cyBitmap;

RECT rcFrame;

.

.

// At startup:

// Indicates whether DPlot was already active when this

// application attempted to start it

int DPlotWasActive;

DPlot_Start(1,&DPlotWasActive);

// Desired size of bitmap retrieved from DPlot:

GetWindowRect(GetDlgItem(hwnd,DLG_PICTUREFRAME),&rcFrame);

// Allow room for frame border

InflateRect(&rcFrame,-3,-3);

MapWindowPoints(NULL,hwnd,(LPPOINT)&rcFrame,2);

cxBitmap = rcFrame.right-rcFrame.left+1;

cyBitmap = rcFrame.bottom-rcFrame.top+1;

.

.

// Plot something

// Be sure to delete previously-created bitmaps, or 

// you'll quickly run out of memory

if(hbmp)

{

       DeleteObject(hbmp);

       hbmp = 0;

}

// Though DPlot is hidden, don't forget that any documents you create are still

// there, maintained by DPlot.

if(DocNum) dPlot_Command(DocNum, "[FileClose()]");

DocNum = DPlot_Plot(&DPlot,x,y,szCommands);

// (or DocNum = DPlot_Plot8(&DPlot,x,y,szCommands);

if(DocNum)

{

       hbmp = dPlot_GetBitmapEx(DocNum, cxBitmap, cyBitmap, &DPM);

       SendDlgItemMessage(hwnd,DLG_PICTUREFRAME,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hbmp);

}

 

Plot to a metafile, caller responsible for drawing the metafile

See, for example, the btest4 and ctest4 (VB6 and C) example programs.

// Global data:

int DocNum=0;

HENHMETAFILE hemf;

RECT rcFrame;.

.

.

// At startup:

int DPlotWasActive;

DPlot_Start(1,&DPlotWasActive);

GetWindowRect(GetDlgItem(hwnd,DLG_PICTUREFRAME),&rcFrame);

InflateRect(&rcFrame,-3,-3);

MapWindowPoints(NULL,hwnd,(LPPOINT)&rcFrame,2);

// Size of the metafile to retrieve, in inches. The actual size specified isn't critical 

// *unless* we want font sizes in our output to exactly match what we specify. What IS

// fairly important is that the aspect ratio matches the aspect ratio of the output.

SizeX  = 5.0f;

SizeY  = (SizeX * (rcFrame.bottom-rcFrame.top+1))/(rcFrame.right-rcFrame.left+1);.

.

.

// Plot something

if(hemf) 

{

       DeleteEnhMetaFile(hemf);

       hemf = (HENHMETAFILE)NULL;

}

DocNum = DPlot_Plot(&DPlot,x,y,szExec);

hemf = DPlot_GetEnhMetaFile(DocNum,SizeX,SizeY);

dPlot_Command(DocNum,"[FileClose()]");

InvalidateRect(hwnd,&rcFrame,TRUE);

.

.

case WM_PAINT:

{

       HDC hdc;

       PAINTSTRUCT        ps;

       hdc = BeginPaint(hwnd,&ps);

       FillRect(hdc,&rcFrame,GetStockObject(WHITE_BRUSH));

       if(hemf != NULL) PlayEnhMetaFile(hdc,hemf,&rcFrame);

       EndPaint(hwnd,&ps);

}

return FALSE;

 

Plot directly to caller's window

See, for example, the btest6 and ctest6 (VB6 and C) example programs.

// Global data:

int DocNum=0;

int DPlotWasActive;

RECT rcFrame;

HWND hwndFrame;

WNDPROC lpFrameProc;

.

.

// At startup:

DPlot_Start(1,&DPlotWasActive);

hwndFrame = GetDlgItem(hwnd,DLG_PICTUREFRAME);

GetWindowRect(hwndFrame,&rcFrame);

InflateRect(&rcFrame,-3,-3);

MapWindowPoints(NULL,hwnd,(LPPOINT)&rcFrame,2);

// Subclass the picture frame window for the case of this

// app being deactivated, then activated. (Not needed if

// this application cannot be deactivated.)

lpFrameProc = (WNDPROC)GetWindowLong(hwndFrame,GWL_WNDPROC);

SetWindowLong(hwndFrame, GWL_WNDPROC, (LONG)PictureFrameWndProc );

.

.

// Plot something

sprintf(szExec,"[SetOutputWindow(%d,%d,%d,%d,%d)]",

       hwnd,rcFrame.left,rcFrame.top,rcFrame.right,rcFrame.bottom);

if(DocNum) dPlot_Command(DocNum, "[FileClose()]");

DocNum = DPlot_Plot(&DPlot,x,y,szExec);

// Output will be drawn directly to rcFrame. The only gotcha

// we need to be concerned with is repainting if this app

// is covered/uncovered by another window.

.

.

// The following takes care of the calling application being

// deactivated and covered by another window, then activated.

// You CAN respond to WM_PAINT within the main window procedure,

// but doing so will have the nasty side effect of redrawing the

// picture frame AFTER WM_PAINT is processed, which negates what

// we're doing here:

BOOL CALLBACK PictureFrameWndProc( HWND hwnd, unsigned msg, UINT wparam, LONG lparam )

{

       if((msg == WM_PAINT) && (DocNum != 0))

       {

               // Let dialog procedure paint the frame

               CallWindowProc(lpFrameProc,hwnd,msg,wparam,lparam);

               // And THEN redraw the bitmap

               DPlot_Command(DocNum,"[Repaint()]");

               return 0;

       }

       else

               return CallWindowProc(lpFrameProc,hwnd,msg,wparam,lparam);

}

In general drawing directly to the caller's window is mostly useful for real-time applications that produce new data at a fast framerate. For other applications the first method (retrieving a bitmap and drawing the bitmap within the caller) or second (identical, but using metafiles rather than bitmaps) is more efficient than drawing directly to the caller's window and results in fewer synchronization problems.

 

 

 

 


Page url: http://www.dplot.com/lib/index.htm?dplot_output_destination.htm