hi i am written the window application in win32 for the dynamic graph.
after minimizing and maximizing the window the previous graph will not retain so please help me.. here is my code in win32
Code:
#include <windows.h>
#include <stdio.h>
#include <time.h>
#define IDC_STATIC_EDIT 1000
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
void Line_paint(void);
int DrawChart(HDC htemp);
HWND HWND_G,hwnd_DESK;
WPARAM wParam;
HDC hDC_AMEL,m_MemDC;
//HBITMAP hBitmap;
static HBITMAP hBitmap = NULL,h_MemDC=NULL;
static int xValue=50,yValue=50;
static int firstx=50,firsty=200,secondx=50,secondy=200;
BITMAPINFOHEADER BitmapInfoHeader;
BITMAP Bm;
byte *BmpData;
BITMAPINFO pbmi;
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH/*LTGRAY_BRUSH*/);
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION |
WS_SYSMENU |WS_DLGFRAME, /* default window */
300, /* Windows decides the position */
150, /* where the window ends up on the screen */
600, /* The programs width */
300, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
hwnd_DESK=CreateWindowEx (0, /* Extended possibilites for variation */
TEXT("STATIC"), /* Classname */
NULL, /* Title Text */
WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN , /* default window */
40, /* Windows decides the position */
30, /* where the window ends up on the screen */
400, /* The programs width */
200, /* and height in pixels */
hwnd, /* The window is a child-window to desktop */
(HMENU)IDC_STATIC_EDIT, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
ShowWindow (hwnd_DESK, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_GETMINMAXINFO:
{
LPMINMAXINFO lpMMI;
break;
}
case WM_MOVE:
{
RECT RClient;
InvalidateRect(hwnd_DESK,&RClient,FALSE);
}
break;
case WM_ERASEBKGND:
{
/* RECT RClient;
HDC htemp,htemp1;
htemp = GetDC(hwnd_DESK);
GetClientRect(hwnd_DESK, &RClient);
int targetWidth,targetHeight;
targetWidth = RClient.right - RClient.left;
targetHeight = RClient.bottom - RClient.top;
BitBlt(htemp,0,0,targetWidth,targetHeight,hDC_AMEL,4,0,SRCCOPY);
*/
return 1;
}
break;
case WM_PAINT:
{
static int i=10, inc;
HDC htemp,htemp1;
RECT RClient;
HBITMAP hBitmap;
HBRUSH brCross;
htemp = GetDC(hwnd_DESK);
GetClientRect(hwnd_DESK, &RClient);
int targetWidth,targetHeight;
targetWidth = RClient.right - RClient.left;
targetHeight = RClient.bottom - RClient.top;
if(hDC_AMEL==NULL)
{
hDC_AMEL=CreateCompatibleDC(hDC_AMEL);
GetClipBox((HDC)wParam, &RClient);
targetWidth = RClient.right - RClient.left;
targetHeight = RClient.bottom - RClient.top;
hBitmap=CreateCompatibleBitmap(hDC_AMEL,targetWidth,targetHeight);
SelectObject(hDC_AMEL,hBitmap);
// draw the scale...
SetBkColor(hDC_AMEL,RGB(0,0,0));
brCross= CreateHatchBrush(HS_CROSS, RGB(0, 128, 0));
FillRect(hDC_AMEL,&RClient,brCross);
SelectObject(hDC_AMEL,brCross);
// BitBlt(hDC_AMEL,0,0,targetWidth,targetHeight,hDC_AMEL,0,0,SRCCOPY);
Line_paint();
InvalidateRect(hwnd_DESK,&RClient,FALSE);
}
if(hDC_AMEL!=NULL)
{
RClient.left = RClient.right-4;
SetBkColor(htemp,RGB(0,0,0));
brCross= CreateHatchBrush(HS_HORIZONTAL, RGB(0, 128, 0));
FillRect(htemp,&RClient,brCross);
static BOOL bDrawVerticle = FALSE;
bDrawVerticle = !bDrawVerticle;
// Draw Verticle lines only every two steps
if (bDrawVerticle)
{SetBkColor(htemp,RGB(0,0,0));
HPEN Oldpen = CreatePen(PS_SOLID, 1, RGB(0,128,0));
Oldpen = (HPEN)(SelectObject(htemp, Oldpen));
MoveToEx(htemp,RClient.right-2,RClient.top, NULL);
LineTo(htemp,RClient.right-2,RClient.bottom);
SelectObject(htemp,Oldpen);
// line drawing...
DrawChart(htemp);
}
BitBlt(htemp,0,0,targetWidth,targetHeight,hDC_AMEL,4,0,SRCCOPY);
if(!IsIconic(hwnd))
{
SendMessage(hwnd_DESK,WM_ICONERASEBKGND, (WPARAM)hDC_AMEL, 0);
Line_paint();
BitBlt(htemp,0,0,targetWidth,targetHeight,(HDC)&m_MemDC,0,0,SRCCOPY);
}
m_MemDC=CreateCompatibleDC(hDC_AMEL);
GetClipBox((HDC)wParam, &RClient);
targetWidth = RClient.right - RClient.left;
targetHeight = RClient.bottom - RClient.top;
hBitmap=CreateCompatibleBitmap(m_MemDC,targetWidth,targetHeight);
SelectObject(m_MemDC,hBitmap);
UpdateWindow(GetDlgItem(hwnd,IDC_STATIC_EDIT));
Sleep(500);
}
GetObject(brCross , sizeof(BITMAP),(LPVOID)&brCross);
SelectObject(hDC_AMEL,brCross);
DeleteObject(brCross);
DeleteDC(hDC_AMEL);
ReleaseDC( hwnd_DESK, hDC_AMEL ) ;
InvalidateRect(hwnd_DESK,&RClient,FALSE);
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
void Line_paint(void)
{
HDC htemp;
RECT RClient;
// HBITMAP hBitmap;
HBRUSH brCross;
static int i=0;
int targetWidth,targetHeight;
GetClientRect(hwnd_DESK, &RClient);
hDC_AMEL = GetDC(hwnd_DESK);
targetWidth = RClient.right - RClient.left;
targetHeight = RClient.bottom - RClient.top;
HPEN hBluePen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0) );
PAINTSTRUCT ps;
hDC_AMEL = BeginPaint(hwnd_DESK, &ps);
hBluePen = static_cast<HPEN>(SelectObject(hDC_AMEL, hBluePen));
if (hDC_AMEL!=NULL)
{
htemp=CreateCompatibleDC(hDC_AMEL);
GetClipBox((HDC)wParam, &RClient);
targetWidth = RClient.right - RClient.left;
targetHeight = RClient.bottom - RClient.top;
hBitmap=CreateCompatibleBitmap(hDC_AMEL,targetWidth,targetHeight);
SelectObject(hDC_AMEL,hBitmap);
SetBkColor(hDC_AMEL,RGB(0,0,0));
brCross= CreateHatchBrush(HS_CROSS, RGB(0, 128, 0));
FillRect(hDC_AMEL,&RClient,brCross);
SelectObject(htemp,brCross);
// BitBlt(hDC_AMEL,0,0,targetWidth,targetHeight,hDC_AMEL,0,0,SRCCOPY);
}
m_MemDC=CreateCompatibleDC(hDC_AMEL);
GetClipBox((HDC)wParam, &RClient);
targetWidth = RClient.right - RClient.left;
targetHeight = RClient.bottom - RClient.top;
hBitmap=CreateCompatibleBitmap(m_MemDC,targetWidth,targetHeight);
SelectObject(m_MemDC,hBitmap);
GetObject(hBitmap , sizeof(BITMAP),(LPVOID)&hBitmap);
SelectObject(hDC_AMEL, hBitmap);
DeleteObject(hBitmap);
DeleteDC(hDC_AMEL);
}
int DrawChart(HDC htemp)
{
// create a blue pen object
HPEN hBluePen = CreatePen(PS_SOLID, 3, RGB(0, 0, 255) );
static int inc,Height=0;
static PAINTSTRUCT ps;
// associate the pen with the DC
hBluePen = static_cast<HPEN>(SelectObject(htemp, hBluePen));
// draw someting to the DC
RECT RClient;
int targetWidth,targetHeight;
GetClientRect(hwnd_DESK, &RClient);
hDC_AMEL = GetDC(hwnd_DESK);
targetWidth = RClient.right - RClient.left;
targetHeight = RClient.bottom - RClient.top;
if (GetClientRect(hwnd_DESK, &RClient))
{
int array[]={80,100,39,10,15,68,30,40,13,45,10,5,30,4,15,20,49,0,40,9,80,80,100,39,10,15,68,30,40,13,45,10,5,30,4,15,20,49,0,40,9,80,80,100,39,10,15,68,30,40,13,45,10,5,30,4,15,20,49,0,40,9,80,100,39,10,15,68,30,40,13,45,10,5,30,4,15,20,49,0,40,9,80,100,39,10,15,68,30,40,13,45,10,5,30,4,15,20,49,0,40,9,80,100,39,10,15,68,30,40,13,45,10,5,30,4,15,20,49,0,40,9};
int TEMP;
int midx,midy;
HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0) );
hRedPen = static_cast<HPEN>(SelectObject(htemp, hRedPen));
// for(inc=0;inc<30;inc++)
do
{
xValue=xValue-3;
yValue=yValue-3;
TEMP= Height+ array[inc];
printf("x1= %d \t y1= %d \t x2=%d \t y2=%d \n",xValue,250,yValue,TEMP);
midx=(xValue+yValue)/2;
midy=(200+TEMP)/2;
secondx=midx;
secondy=midy;
HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(0,255,0) );
hRedPen = static_cast<HPEN>(SelectObject(htemp, hRedPen));
if (!MoveToEx(htemp,targetWidth-8, firsty, NULL) ||!LineTo(htemp, targetWidth,secondy))
{
MessageBox(NULL,"Paint error","ERROR",0);
}
hRedPen = CreatePen(PS_SOLID, 5, RGB(0,255,0) );
hRedPen = static_cast<HPEN>(SelectObject(htemp, hRedPen));
/*
if (!MoveToEx(htemp,midx, midy, NULL) ||!LineTo(htemp, midx,midy))
{
MessageBox(NULL,"Paint error","ERROR",0);
} */
xValue=xValue+10;
yValue=yValue+10;
firstx=midx;
firsty=midy;
inc++;
printf("the inc value %d \n",inc);
if(inc==80)
inc=0;
} while (inc < 10 || inc > 80);
}
// free the blue pen object
DeleteObject(SelectObject(htemp, hBluePen));
// release the DC
EndPaint(hwnd_DESK, &ps);
return 0;
}