Transparent CLOCK.docx
《Transparent CLOCK.docx》由会员分享,可在线阅读,更多相关《Transparent CLOCK.docx(12页珍藏版)》请在冰豆网上搜索。
TransparentCLOCK
/*--------------------------------------
CLOCK.C--AnalogClockProgram
(c)CharlesPetzold,1998
--------------------------------------*/
#define_WIN32_WINNT0x0500
#include
#include
#include"resource.h"
typedefBOOL(WINAPI*lpfnSetLayeredWindowAttributes)(HWNDhWnd,COLORREFcrKey,BYTEbAlpha,DWORDdwFlags);
lpfnSetLayeredWindowAttributesSetLayeredWindowAttributesW;
#defineID_TIMER1
#defineTWOPI(2*3.14159)
LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);
LRESULTCALLBACKSubClassWndProc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam);
BOOLMakeWndTrans(HWNDhWnd,intnAlpha);
LRESULTCALLBACKHookCallWndProc(intnCode,WPARAMwParam,LPARAMlParam);
HINSTANCEhInst;
TCHARszAppName[]=TEXT("Clock");
intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,
PSTRszCmdLine,intiCmdShow)
{
//staticTCHARszAppName[]=TEXT("Clock");
HWNDhwnd;
MSGmsg;
WNDCLASSwndclass;
HMODULEhUser32;
HRGNWndRGN;
intiCapHeight;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=NULL;
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;
if(!
RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("ProgramrequiresWindowsNT!
"),
szAppName,MB_ICONERROR);
return0;
}
hInst=hInstance;
iCapHeight=GetSystemMetrics(SM_CYCAPTION);
hwnd=CreateWindowEx(WS_EX_LAYERED|WS_EX_TOPMOST,szAppName,TEXT("TransparentClock"),
WS_POPUP,
CW_USEDEFAULT,CW_USEDEFAULT,
200,200,
NULL,NULL,hInstance,NULL);
SetWindowLong(hwnd,GWL_EXSTYLE,GetWindowLong(hwnd,GWL_EXSTYLE)|WS_EX_LAYERED|WS_EX_TOPMOST);
hUser32=GetModuleHandle(TEXT("user32.dll"));
SetLayeredWindowAttributesW=(lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32,"SetLayeredWindowAttributes");
SetLayeredWindowAttributesW(hwnd,0,80,LWA_ALPHA);
FreeLibrary(hUser32);
WndRGN=CreateEllipticRgn(0,0,200,200);
SetWindowRgn(hwnd,WndRGN,FALSE);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
returnmsg.wParam;
}
voidSetIsotropic(HDChdc,intcxClient,intcyClient)
{
SetMapMode(hdc,MM_ISOTROPIC);
SetWindowExtEx(hdc,1000,1000,NULL);
SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL);
SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
}
voidRotatePoint(POINTpt[],intiNum,intiAngle)
{
inti;
POINTptTemp;
for(i=0;i{
ptTemp.x=(int)(pt[i].x*cos(TWOPI*iAngle/360)+
pt[i].y*sin(TWOPI*iAngle/360));
ptTemp.y=(int)(pt[i].y*cos(TWOPI*iAngle/360)-
pt[i].x*sin(TWOPI*iAngle/360));
pt[i]=ptTemp;
}
}
voidDrawClock(HDChdc,HBRUSHhBrush)
{
intiAngle;
POINTpt[3];
for(iAngle=0;iAngle<360;iAngle+=6)
{
pt[0].x=0;
pt[0].y=900;
RotatePoint(pt,1,iAngle);
pt[2].x=pt[2].y=iAngle%5?
33:
100;
pt[0].x-=pt[2].x/2;
pt[0].y-=pt[2].y/2;
pt[1].x=pt[0].x+pt[2].x;
pt[1].y=pt[0].y+pt[2].y;
SelectObject(hdc,hBrush);
Ellipse(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y);
}
}
voidDrawHands(HDChdc,SYSTEMTIME*pst,BOOLfChange)
{
staticPOINTpt[3][5]={0,-150,100,0,0,600,-100,0,0,-150,
0,-200,50,0,0,800,-50,0,0,-200,
0,0,0,0,0,0,0,0,0,800};
inti,iAngle[3];
POINTptTemp[3][5];
iAngle[0]=(pst->wHour*30)%360+pst->wMinute/2;
iAngle[1]=pst->wMinute*6;
iAngle[2]=pst->wSecond*6;
memcpy(ptTemp,pt,sizeof(pt));
for(i=fChange?
0:
2;i<3;i++)
{
RotatePoint(ptTemp[i],5,iAngle[i]);
Polyline(hdc,ptTemp[i],5);
}
}
LRESULTCALLBACKWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)
{
staticintcxClient,cyClient;
staticSYSTEMTIMEstPrevious;
BOOLfChange;
HDChdc;
PAINTSTRUCTps;
SYSTEMTIMEst;
staticHHOOKhHookID;
POINTp;
staticHMENUhMenu;
staticHPENhPen;
staticHBRUSHhBrush;
RECTrect;
switch(message)
{
caseWM_CREATE:
hHookID=SetWindowsHookEx(WH_CALLWNDPROC,HookCallWndProc,0,GetWindowThreadProcessId(hwnd,0));
hMenu=LoadMenu(hInst,szAppName);
hMenu=GetSubMenu(hMenu,0);
hPen=CreatePen(PS_SOLID,4,RGB(255,0,0));
hBrush=CreateSolidBrush(RGB(255,0,0));
SetTimer(hwnd,ID_TIMER,1000,NULL);
GetLocalTime(&st);
stPrevious=st;
return0;
caseWM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return0;
caseWM_RBUTTONUP:
caseWM_NCRBUTTONUP:
p.x=LOWORD(lParam);
p.y=HIWORD(lParam);
//ClientToScreen(hwnd,&p);
TrackPopupMenu(hMenu,TPM_RIGHTBUTTON,p.x,p.y,0,hwnd,NULL);
return0;
caseWM_NCHITTEST:
GetClientRect(hwnd,&rect);
p.x=LOWORD(lParam);
p.y=HIWORD(lParam);
ScreenToClient(hwnd,&p);
if(PtInRect(&rect,p))
returnHTCAPTION;
else
returnDefWindowProc(hwnd,message,wParam,lParam);
break;
caseWM_TIMER:
GetLocalTime(&st);
fChange=st.wHour!
=stPrevious.wHour||
st.wMinute!
=stPrevious.wMinute;
hdc=GetDC(hwnd);
SetIsotropic(hdc,cxClient,cyClient);
SelectObject(hdc,GetStockObject(WHITE_PEN));
DrawHands(hdc,&stPrevious,fChange);
SelectObject(hdc,hPen);
DrawHands(hdc,&st,TRUE);
ReleaseDC(hwnd,hdc);
stPrevious=st;
return0;
caseWM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SetIsotropic(hdc,cxClient,cyClient);
DrawClock(hdc,hBrush);
DrawHands(hdc,&stPrevious,TRUE);
EndPaint(hwnd,&ps);
return0;
caseWM_COMMAND:
switch(LOWORD(wParam))
{
caseIDM_APP_EXIT:
SendMessage(hwnd,WM_CLOSE,0,0);
return0;
}
break;
caseWM_DESTROY:
if(hHookID!
=0)
UnhookWindowsHookEx(hHookID);
DestroyMenu(hMenu);
DeleteObject(hPen);
DeleteObject(hBrush);
KillTimer(hwnd,ID_TIMER);
PostQuitMessage(0);
return0;
}
returnDefWindowProc(hwnd,message,wParam,lParam);
}
BOOLMakeWndTrans(HWNDhWnd,intnAlpha)
{
HMODULEhUser32;
longret;
hUser32=GetModuleHandle(TEXT("user32.dll"));
SetLayeredWindowAttributesW=(lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32,"SetLayeredWindowAttributes");
SetLayeredWindowAttributesW(hWnd,0,80,LWA_ALPHA);
FreeLibrary(hUser32);
ret=SetWindowLong(hWnd,GWL_EXSTYLE,GetWindowLong(hWnd,GWL_EXSTYLE)|WS_EX_LAYERED);
SetLayeredWindowAttributesW(hWnd,0,(BYTE)nAlpha,LWA_ALPHA);
returnTRUE;
}
LRESULTCALLBACKSubClassWndProc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam)
{
LONGlRet=(LONG)GetProp(hWnd,TEXT("OldWndProc"));
//LONGdwStyle;
switch(uMsg)
{
caseWM_CREATE:
MakeWndTrans(hWnd,80);
break;
caseWM_DESTROY:
RemoveProp(hWnd,TEXT("OldWndProc"));
SetWindowLong(hWnd,GWL_WNDPROC,lRet);
break;
}
returnCallWindowProc((WNDPROC)lRet,hWnd,uMsg,wParam,lParam);
}
LRESULTCALLBACKHookCallWndProc(intnCode,WPARAMwParam,LPARAMlParam)
{
CWPSTRUCTcwps;
LONGlRet;
if(nCode==HC_ACTION)
{
CopyMemory(&cwps,(LPVOID)lParam,sizeof(CWPSTRUCT));
switch(cwps.message)
{
caseWM_CREATE:
{
TCHARszClass[128];
GetClassName(cwps.hwnd,szClass,127*sizeof(TCHAR));//shouldfixhere
if(lstrcmpi(szClass,TEXT("#32768"))==0)
{
lRet=SetWindowLong(cwps.hwnd,GWL_WNDPROC,(LONG)SubClassWndProc);
SetProp(cwps.hwnd,TEXT("OldWndProc"),(HANDLE)lRet);
}
}
break;
}
}
returnCallNextHookEx((HHOOK)WH_CALLWNDPROC,nCode,wParam,lParam);
}