Davs... har lavet et fikst lille tegne program.. men jeg har et problem... alt Grafikken forsvinder når vinduet kommer ud af fokus eller bliver resized... så jeg ville lige høre hvordan man kan gemmer den grafik der allerede er blevet lavet så jeg senere kan kalde det.. så når grafikken forsvinder så kan jeg kalde den grafikken igen så det ikke forsvinder alligevel...
I Windows er man nød til selv at huske hvad man har tegnet og så tegne det hele igen ved WM_PAINT, f.ex:
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <string.h>
#include <vector>
HINSTANCE hInst; // Instance handle
HWND hwndMain; //Main window handle
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
std::vector< POINT >Points;
void DrawRect(HDC dc, POINT P)
{
MoveToEx(dc, P.x - 10, P.y - 10, NULL);
LineTo(dc, P.x + 10, P.y - 10);
LineTo(dc, P.x + 10, P.y + 10);
LineTo(dc, P.x - 10, P.y + 10);
LineTo(dc, P.x - 10, P.y - 10);
}
static BOOL InitApplication(void)
{
WNDCLASS wc;
memset(&wc,0,sizeof(WNDCLASS));
wc.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ;
wc.lpfnWndProc = (WNDPROC)MainWndProc;
wc.hInstance = hInst;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "ng39WndClass";
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
if (!RegisterClass(&wc))
return 0;
return 1;
}
HWND Createng39WndClassWnd(void)
{
return CreateWindow("ng39WndClass","ng39",
WS_MINIMIZEBOX|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_MAXIMIZEBOX|WS_CAPTION|WS_BORDER|WS_SYSMENU|WS_THICKFRAME,
CW_USEDEFAULT,0,CW_USEDEFAULT,0,
NULL,
NULL,
hInst,
NULL);
}
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch (msg)
{
case WM_SIZE:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
{
POINT Point;
HDC dc = GetDC(hwnd);
Point.x = GET_X_LPARAM(lParam);
Point.y = GET_Y_LPARAM(lParam);
Points.push_back(Point);
DrawRect(dc, Point);
ReleaseDC(hwnd, dc);
}
break;
case WM_PAINT:
{
PAINTSTRUCT PaintStruct;
HDC dc = BeginPaint(hwnd, &PaintStruct);
std::vector<POINT>::iterator x;
for(x = Points.begin(); x != Points.end(); ++x)
DrawRect(dc, *x);
EndPaint(hwnd, &PaintStruct);
}
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
MSG msg;
hInst = hInstance;
if (!InitApplication())
return 0;
if ((hwndMain = Createng39WndClassWnd()) == (HWND)0)
return 0;
ShowWindow(hwndMain,SW_SHOW);
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}