home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1999 November
/
Chip_1999-11_cd.bin
/
ctenari
/
Novak
/
ZOOM.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1999-08-30
|
15KB
|
521 lines
/*
TOTO JSOU ZDROJOVE TEXTY K VERZI 1.1
DIRECT DRAW ZOOM V 1.1
AUTOR: Ondrej Novak
ROK: 1999
MESIC: Unor
KONTAKT: xnovako1@cs.felk.cvut.cz
Tento program muze byt volne siren a modifikovan. Spada do PUBLIC DOMAIN.
Zdrojove texty maji zaroven slouzit jako priklad vyuziti DirectX.
Autor prosi vsechny, kteri vyuziji tento program, nebo zdrojak ke svemu
programovani, aby mi napsali elektronicky dopis. Budu velice potesen. Dekuji.
*/
#include <process.h>
#include <windows.h>
#include <stdio.h>
#include <dd/ddraw.h>
#include <assert.h>
#define lpszAppName "DirectDraw ZOOM" //Jmeno a titulni radek
#define lpszTitle "DirectDraw ZOOM"
HWND hMainWnd; //Handle hlavniho okna
HINSTANCE hInst; //Handle instance
short WinX,WinY; //Pozice klentske oblasti
LPDIRECTDRAWSURFACE lpprim,lptemp; //ukazatele na primarni surface a pracovni
LPDIRECTDRAW lpdd; //surface a na objekt directdraw
LPDIRECTDRAWCLIPPER lpclip;
int sizeX,sizeY; //Velikost desktopu
HPEN penblack,penwhite; //definice cerneho a bileho pera
HBRUSH brtransp; //defince transparetniho stetce
#define HIGHSPEED 20 //rychlost casovace v ms za provozu
#define SLOWSPEED 1000 //rychlost casovace behem uspani v ms.
char hided=0; //jednicka znamena, ze krizek je skryt
int zoom=4; //zvetseni.
static timerid; //identifikator casovace.
int movewin=0;
static int sysmem=0;
#define IDC_ZOOM1 (WM_USER+1) //zpravy systemoveo menu
#define IDC_ZOOM2 (WM_USER+2)
#define IDC_ZOOM3 (WM_USER+3)
#define IDC_ZOOM4 (WM_USER+4)
#define IDC_ZOOM6 (WM_USER+6)
#define IDC_ZOOM8 (WM_USER+8)
#define IDC_ZOOM12 (WM_USER+12)
#define IDC_ZOOM16 (WM_USER+16)
#define IDC_HIDEKZ (WM_USER+17)
#define IDC_MAILME (WM_USER+18)
#define IDC_MOVEW (WM_USER+19)
#define IDC_SYSMEM (WM_USER+20)
LPDIRECTDRAWSURFACE CloneSurface(LPDIRECTDRAWSURFACE surf,HWND hWnd)
{
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE clone;
RECT rect;
if (lpprim->IsLost()) lpprim->Restore();
GetClientRect(hWnd,&rect);
assert(surf!=NULL);
ddsd.dwSize=sizeof(ddsd);
surf->GetSurfaceDesc(&ddsd);
ddsd.dwFlags=DDSD_CAPS |DDSD_HEIGHT |DDSD_WIDTH |DDSD_PIXELFORMAT;
ddsd.dwHeight=rect.bottom-rect.top;
ddsd.dwWidth=rect.right-rect.left;
ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN | (sysmem?DDSCAPS_SYSTEMMEMORY:0);
if (lpdd->CreateSurface(&ddsd,(LPDIRECTDRAWSURFACE *)&clone,NULL)!=DD_OK) return NULL;
return clone;
}
/*
LPDIRECTDRAWSURFACE CloneSurface(LPDIRECTDRAWSURFACE surf)
{
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE clone;
if (surf->IsLost()) surf->Restore();
assert(surf!=NULL);
ddsd.dwSize=sizeof(ddsd);
surf->GetSurfaceDesc(&ddsd);
ddsd.dwFlags=DDSD_CAPS |DDSD_HEIGHT |DDSD_WIDTH |DDSD_PIXELFORMAT;
ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
if (lpdd->CreateSurface(&ddsd,(LPDIRECTDRAWSURFACE *)&clone,NULL)!=DD_OK) return NULL;
return clone;
}
*/
void InitDirect()
{
HRESULT res;
DDSURFACEDESC sdesc;
res=DirectDrawCreate(NULL,&lpdd,NULL);
assert(res==DD_OK);
res=lpdd->SetCooperativeLevel(hMainWnd,DDSCL_NORMAL);
assert(res==DD_OK);
sdesc.dwSize=sizeof(sdesc);
sdesc.dwFlags=DDSD_CAPS;
sdesc.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE;
res=lpdd->CreateSurface(&sdesc,&lpprim,NULL);
assert(res==DD_OK);
lpprim->GetSurfaceDesc(&sdesc);
sizeX=sdesc.dwWidth;
sizeY=sdesc.dwHeight;
DirectDrawCreateClipper(0,&lpclip,NULL);
lpclip->SetHWnd(0,hMainWnd);
}
void DrawCross(int x,int y)
{
HDC hdc;
POINT p;
int sz1=2*zoom,sz2=6*zoom;
x+=zoom/2;
y+=zoom/2;
lptemp->GetDC(&hdc);
SelectObject(hdc,penwhite);
SelectObject(hdc,brtransp);
// Rectangle(hdc,x-4,y-4,x+4,y+4);
MoveToEx(hdc,x-sz1,y,&p);LineTo(hdc,x-sz2,y);
MoveToEx(hdc,x+sz1,y,&p);LineTo(hdc,x+sz2,y);
MoveToEx(hdc,x,y-sz1,&p);LineTo(hdc,x,y-sz2);
MoveToEx(hdc,x,y+sz1,&p);LineTo(hdc,x,y+sz2);
SelectObject(hdc,penblack);
MoveToEx(hdc,x-sz1,y+1,&p);LineTo(hdc,x-sz2,y+1);
MoveToEx(hdc,x+sz1,y+1,&p);LineTo(hdc,x+sz2,y+1);
MoveToEx(hdc,x+1,y-sz1,&p);LineTo(hdc,x+1,y-sz2);
MoveToEx(hdc,x+1,y+sz1,&p);LineTo(hdc,x+1,y+sz2);
MoveToEx(hdc,x-sz1,y-1,&p);LineTo(hdc,x-sz2,y-1);
MoveToEx(hdc,x+sz1,y-1,&p);LineTo(hdc,x+sz2,y-1);
MoveToEx(hdc,x-1,y-sz1,&p);LineTo(hdc,x-1,y-sz2);
MoveToEx(hdc,x-1,y+sz1,&p);LineTo(hdc,x-1,y+sz2);
/* MoveToEx(hdc,x-3,y-3,&p);LineTo(hdc,x-5,y-5);
MoveToEx(hdc,x-3,y+3,&p);LineTo(hdc,x-5,y+5);
MoveToEx(hdc,x+3,y-3,&p);LineTo(hdc,x+5,y-5);
MoveToEx(hdc,x+3,y+3,&p);LineTo(hdc,x+5,y+5);*/
// Rectangle(hdc,x-5,y-5,x+5,y+5);
lptemp->ReleaseDC(hdc);
}
void Uhni(int x,int y)
{
RECT r,r2;
int nx,ny;
int midx,midy;
GetClientRect(hMainWnd,&r);
r2=r;
r.left+=5+WinX;
r.right+=-5+WinX;
r.top+=5+WinY;
r.bottom+=-5
+WinY;
if (x>=r.left && x<r.right && y>=r.top && y<r.bottom)
{
midx=r.left+r.right>>1;
midy=r.top+r.bottom>>1;
midx=x-midx;
midy=y-midy;
/*if (midx<0 && midx<-abs(midy)) nx=x,ny=WinX;
else if (midx>0 && midx>abs(midy)) nx=x-r2.right,ny=WinY;
else if (midy<0 && midy<-abs(midx)) nx=WinX,ny=y;
else if (midy>0 && midy>abs(midx)) nx=WinX,ny=y-r2.bottom;*/
if (midx>0 || midy>0) nx=x+50,ny=y+50;else nx=x-r2.right-50,ny=y-r2.bottom-50;
if (nx<0) nx=sizeX-r2.right;
if (ny<0) ny=sizeY-r2.bottom;
if (nx+r2.right>sizeX) nx=0;
if (ny+r2.bottom>sizeY) ny=0;
SetWindowPos(hMainWnd,NULL,nx,ny,0,0,SWP_NOSIZE|SWP_NOZORDER);
}
}
void ShowZoomedWindow(int x,int y)
{
RECT src,trg,trg2,trg3;
HRESULT res;
if (lptemp==NULL) lptemp=CloneSurface(lpprim,hMainWnd);
if (lptemp==NULL) return;
GetClientRect(hMainWnd,&trg);
src=trg;
trg2=trg;
trg3=trg;
src.right/=zoom;
src.bottom/=zoom;
trg.left+=WinX;
trg.top+=WinY;
trg.right+=WinX;
trg.bottom+=WinY;
src.left=x-src.right/2;
src.top=y-src.bottom/2;
src.right=x+src.right/2;
src.bottom=y+src.bottom/2;
/* trg.left=max(trg.left,0);
trg.right=min(trg.right,sizeX);
trg.top=max(trg.top,0);
trg.bottom=min(trg.bottom,sizeY);*/
if (src.left<0)
{
trg2.left-=zoom*src.left;
src.left=0;
}
if (src.top<0)
{
trg2.top-=zoom*src.top;
src.top=0;
}
if (src.right>=sizeX)
{
trg2.right+=(sizeX-src.right)*zoom;
src.right=sizeX;
}
if (src.bottom>=sizeY)
{
trg2.bottom+=(sizeY-src.bottom)*zoom;
src.bottom=sizeY;
}
if (lptemp->IsLost) lptemp->Restore();
if (lpprim->IsLost) lpprim->Restore();
res=lptemp->Blt(&trg2,lpprim,&src,DDBLT_WAIT,NULL);
if (!hided) DrawCross(trg3.left+trg3.right>>1,trg3.top+trg3.bottom>>1);
lpprim->SetClipper(lpclip);
res=lpprim->Blt(&trg,lptemp,&trg3,DDBLT_WAIT,NULL);
if (res!=DDERR_INVALIDCLIPLIST) lpclip->SetHWnd(0,hMainWnd);
lpprim->SetClipper(NULL);
}
void CheckByZoom()
{
HMENU hmn;
char ch,uchk;
ch=MF_BYCOMMAND | MF_CHECKED;
uchk=MF_BYCOMMAND | MF_UNCHECKED;
hmn=GetSystemMenu(hMainWnd,0);
CheckMenuItem(hmn,IDC_ZOOM2,uchk);
CheckMenuItem(hmn,IDC_ZOOM3,uchk);
CheckMenuItem(hmn,IDC_ZOOM4,uchk);
CheckMenuItem(hmn,IDC_ZOOM6,uchk);
CheckMenuItem(hmn,IDC_ZOOM8,uchk);
CheckMenuItem(hmn,IDC_ZOOM12,uchk);
CheckMenuItem(hmn,IDC_ZOOM16,uchk);
CheckMenuItem(hmn,WM_USER+zoom,ch);
CheckMenuItem(hmn,IDC_HIDEKZ,hided?ch:uchk);
CheckMenuItem(hmn,IDC_MOVEW,movewin?ch:uchk);
CheckMenuItem(hmn,IDC_SYSMEM,sysmem?ch:uchk);
}
char *GetIniName()
{
char szBuff[1000];
char *c,*d,*e;
GetModuleFileName(hInst,szBuff,sizeof(szBuff));
c=strrchr(szBuff,'.');
d=strrchr(szBuff,'\\');
if (c==NULL || c<d) e=strchr(szBuff,0);else e=c;
strcpy(e,".INI");
c=(char *)malloc(strlen(szBuff)+1);
strcpy(c,szBuff);
return c;
}
void SaveIniFile()
{
char *ini;
FILE *f;
WINDOWPLACEMENT r;
ini=GetIniName();
f=fopen(ini,"w");
free(ini);
if (f==NULL) return;
r.length=sizeof(r);
GetWindowPlacement(hMainWnd,&r);
fprintf(f,"%d %d %d %d %d %d %d %d\n",
r.rcNormalPosition.left,
r.rcNormalPosition.top,
r.rcNormalPosition.right,
r.rcNormalPosition.bottom,zoom,hided,movewin,sysmem);
fclose(f);
}
void LoadIniFile()
{
char *ini;
FILE *f;
RECT r;
int i;
ini=GetIniName();
f=fopen(ini,"r");
free(ini);
if (f==NULL) return;
fscanf(f,"%d %d %d %d %d %d %d %d\n",&r.left,&r.top,&r.right,&r.bottom,&zoom,&i,&movewin,&sysmem);
hided=i;
SetWindowPos(hMainWnd,NULL,r.left,r.top,r.right-r.left,r.bottom-r.top,SWP_NOZORDER);
CheckByZoom();
fclose(f);
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch(uMsg)
{
case WM_CREATE:
{
LOGBRUSH bb;
InitDirect();
timerid=SetTimer(hMainWnd,1,50,NULL);
penblack=CreatePen(PS_SOLID,0,0);
penwhite=CreatePen(PS_SOLID,0,0xffffff);
bb.lbStyle=BS_NULL;
bb.lbColor=0;
brtransp=CreateBrushIndirect(&bb);
}
break;
case WM_DESTROY:
{
SaveIniFile();
PostQuitMessage(0);
KillTimer(hMainWnd,timerid);
lpprim->Release();
lptemp->Release();
lpdd->Release();
DeleteObject(penblack);
DeleteObject(penwhite);
DeleteObject(brtransp);
break;
}
case WM_MOVE:
{
WinX=(short)LOWORD(lParam);
WinY=(short)HIWORD(lParam);
break;
}
case WM_SIZE:
if (lptemp!=NULL)
{
lptemp->Release();
lptemp=NULL;
lpclip->SetHWnd(0,hMainWnd);
}
break;
case WM_SYSCOMMAND:
if (LOWORD(wParam)<=IDC_ZOOM16)
{
zoom=LOWORD(wParam)-IDC_ZOOM1+1;
CheckByZoom();
}
if (LOWORD(wParam)==IDC_HIDEKZ)
{
hided=!hided;
CheckByZoom();
}
if (LOWORD(wParam)==IDC_MOVEW)
{
movewin=!movewin;
CheckByZoom();
}
if (LOWORD(wParam)==IDC_SYSMEM)
{
sysmem=!sysmem;
lptemp->Release();
lptemp=NULL;
CheckByZoom();
}
if (LOWORD(wParam)==IDC_MAILME)
{
char szBuff[1000];
char *c;
GetWindowsDirectory(szBuff,sizeof(szBuff));
c=strchr(szBuff,0);
if (c[-1]=='\\') c--;
strcpy(c,"\\explorer.exe");
spawnl(_P_NOWAIT,szBuff,szBuff,"mailto:xnovako1@cs.felk.cvut.cz",NULL);
}
else return DefWindowProc(hWnd,uMsg,wParam,lParam);
break;
case WM_USER:ShowZoomedWindow(wParam,lParam);
if (movewin) Uhni(wParam,lParam);
break;
default:return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return TRUE;
}
void CreateSystemMenu()
{
HMENU hmn;
hmn=GetSystemMenu(hMainWnd,0);
AppendMenu(hmn,MFT_SEPARATOR,0,"");
AppendMenu(hmn,MFT_STRING,IDC_ZOOM2,"Zoom 2x");
AppendMenu(hmn,MFT_STRING,IDC_ZOOM3,"Zoom 3x");
AppendMenu(hmn,MFT_STRING,IDC_ZOOM4,"Zoom 4x");
AppendMenu(hmn,MFT_STRING,IDC_ZOOM6,"Zoom 6x");
AppendMenu(hmn,MFT_STRING,IDC_ZOOM8,"Zoom 8x");
AppendMenu(hmn,MFT_STRING,IDC_ZOOM12,"Zoom 12x");
AppendMenu(hmn,MFT_STRING,IDC_ZOOM16,"Zoom 16x");
AppendMenu(hmn,MFT_SEPARATOR,0,"");
AppendMenu(hmn,MFT_STRING,IDC_HIDEKZ,"Schovej kurzor");
AppendMenu(hmn,MFT_STRING,IDC_MOVEW,"Nep°ekß₧ej");
AppendMenu(hmn,MFT_STRING,IDC_SYSMEM,"Pomalß videokarta");
AppendMenu(hmn,MFT_SEPARATOR,0,"");
AppendMenu(hmn,MFT_STRING,IDC_MAILME," Autor: Ond°ej Novßk");
AppendMenu(hmn,MFT_STRING,IDC_MAILME," E-mail: xnovako1@cs.felk.cvut.cz");
}
int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wc;
HWND hWnd;
POINT lpt;
int counter=0,cnt2=0,slow=0;
if (hPrevInstance == NULL)
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( hInstance, lpszAppName );
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = lpszAppName;
wc.lpszClassName = lpszAppName;
if ( !RegisterClass( &wc ) )
{
MessageBox(NULL,"Unable to register CLASS",NULL,MB_OK);
return 0;
}
hWnd = CreateWindow( lpszAppName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
200, 200,
NULL,
NULL,
hInstance,
NULL
);
if ( !hWnd )
{
MessageBox(NULL,"Unable to create main window",NULL,MB_OK);
return 0;
}
hMainWnd=hWnd;
hInst = hInstance;
CreateSystemMenu();
CheckByZoom();
LoadIniFile();
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
while( GetMessage( &msg, NULL, 0, 0) )
{
if (msg.message==WM_TIMER) //refresh obsahu klentske oblasti
{
char p=!IsIconic(hMainWnd) && (lpt.x!=msg.pt.x || lpt.y!=msg.pt.y);
if ( p || (counter==20 && !slow)) //jen kdyz se neco hejblo
{
PostMessage(hMainWnd,WM_USER,msg.pt.x,msg.pt.y); //posli souradnice
lpt=msg.pt;
counter=0;
if (slow)
{
KillTimer(hMainWnd,timerid);
timerid=SetTimer(hMainWnd,1,HIGHSPEED,NULL);
slow=0;
}
if (p) cnt2=0;
}
else counter++,cnt2++;
if (cnt2>200)
{
KillTimer(hMainWnd,timerid);
timerid=SetTimer(hMainWnd,1,SLOWSPEED,NULL);
cnt2=0;slow=1;
}
}
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
return( msg.wParam );
}