1、Transparent CLOCK/*- CLOCK.C - Analog Clock Program (c) Charles Petzold, 1998 -*/#define _WIN32_WINNT 0x0500#include #include #include resource.htypedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags); lpfnSetLayeredWindowAttributes SetLayeredWind
2、owAttributesW;#define ID_TIMER 1#define TWOPI (2 * 3.14159)LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;LRESULT CALLBACK SubClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);BOOL MakeWndTrans(HWND hWnd, int nAlpha);LRESULT CALLBACK HookCallWndProc(int nCode, WPARAM wParam, L
3、PARAM lParam);HINSTANCE hInst;TCHAR szAppName = TEXT (Clock) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)/ static TCHAR szAppName = TEXT (Clock) ; HWND hwnd; MSG msg; WNDCLASS wndclass ; HMODULE hUser32; HRGN WndRGN; int iCapHeight; wndclass.style
4、= 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.lpszMe
5、nuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass) MessageBox (NULL, TEXT (Program requires Windows NT!), szAppName, MB_ICONERROR) ; return 0 ; hInst = hInstance; iCapHeight = GetSystemMetrics(SM_CYCAPTION); hwnd = CreateWindowEx (WS_EX_LAYERED | WS_EX_TOPMOST, szApp
6、Name, TEXT (Transparent Clock), 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 = (lpfnSetLaye
7、redWindowAttributes)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 (&m
8、sg, NULL, 0, 0) TranslateMessage (&msg) ; DispatchMessage (&msg) ; return msg.wParam ;void SetIsotropic (HDC hdc, int cxClient, int cyClient) SetMapMode (hdc, MM_ISOTROPIC) ; SetWindowExtEx (hdc, 1000, 1000, NULL) ; SetViewportExtEx (hdc, cxClient / 2, -cyClient / 2, NULL) ; SetViewportOrgEx (hdc, c
9、xClient / 2, cyClient / 2, NULL) ;void RotatePoint (POINT pt, int iNum, int iAngle) int i ; POINT ptTemp ; for (i = 0 ; i iNum ; i+) ptTemp.x = (int) (pti.x * cos (TWOPI * iAngle / 360) + pti.y * sin (TWOPI * iAngle / 360) ; ptTemp.y = (int) (pti.y * cos (TWOPI * iAngle / 360) - pti.x * sin (TWOPI *
10、 iAngle / 360) ; pti = ptTemp ; void DrawClock (HDC hdc, HBRUSH hBrush) int iAngle ; POINT pt3 ; for (iAngle = 0 ; iAngle wHour * 30) % 360 + pst-wMinute / 2 ; iAngle1 = pst-wMinute * 6 ; iAngle2 = pst-wSecond * 6 ; memcpy (ptTemp, pt, sizeof (pt) ; for (i = fChange ? 0 : 2 ; i 3 ; i+) RotatePoint (
11、ptTempi, 5, iAnglei) ; Polyline (hdc, ptTempi, 5) ; LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) static int cxClient, cyClient ; static SYSTEMTIME stPrevious ; BOOL fChange ; HDC hdc ; PAINTSTRUCT ps ; SYSTEMTIME st ; static HHOOK hHookID; POINT p; static HMENU hM
12、enu; static HPEN hPen; static HBRUSH hBrush; RECT rect; switch (message) case WM_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=C
13、reateSolidBrush(RGB(255,0,0); SetTimer (hwnd, ID_TIMER, 1000, NULL) ; GetLocalTime (&st) ; stPrevious = st ; return 0 ; case WM_SIZE : cxClient = LOWORD (lParam) ; cyClient = HIWORD (lParam) ; return 0 ; case WM_RBUTTONUP: case WM_NCRBUTTONUP: p.x = LOWORD(lParam); p.y = HIWORD(lParam);/ ClientToScr
14、een( hwnd , &p ); TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, p.x, p.y, 0, hwnd, NULL); return 0; case WM_NCHITTEST: GetClientRect(hwnd ,&rect ); p.x = LOWORD(lParam); p.y = HIWORD(lParam); ScreenToClient( hwnd , &p ); if( PtInRect(&rect, p ) return HTCAPTION; else return DefWindowProc (hwnd, message, wP
15、aram, lParam) ; break; case WM_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 (
16、hdc, hPen) ; DrawHands (hdc, &st, TRUE) ; ReleaseDC (hwnd, hdc) ; stPrevious = st ; return 0 ; case WM_PAINT : hdc = BeginPaint (hwnd, &ps) ; SetIsotropic (hdc, cxClient, cyClient) ; DrawClock (hdc, hBrush) ; DrawHands (hdc, &stPrevious, TRUE) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_COMMAND: swi
17、tch(LOWORD(wParam) case IDM_APP_EXIT: SendMessage(hwnd, WM_CLOSE, 0 ,0); return 0; break; case WM_DESTROY : if(hHookID!=0) UnhookWindowsHookEx(hHookID); DestroyMenu(hMenu); DeleteObject(hPen); DeleteObject(hBrush); KillTimer (hwnd, ID_TIMER) ; PostQuitMessage (0) ; return 0 ; return DefWindowProc (h
18、wnd, message, wParam, lParam) ;BOOL MakeWndTrans(HWND hWnd, int nAlpha) HMODULE hUser32; long ret; hUser32 = GetModuleHandle(TEXT(user32.dll); SetLayeredWindowAttributesW = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32,SetLayeredWindowAttributes); SetLayeredWindowAttributesW(hWnd, 0, 80, LW
19、A_ALPHA); FreeLibrary(hUser32); ret = SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED); SetLayeredWindowAttributesW(hWnd, 0, (BYTE)nAlpha, LWA_ALPHA); return TRUE;LRESULT CALLBACK SubClassWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) LONG lRet = (LONG)
20、 GetProp(hWnd, TEXT(OldWndProc); /LONG dwStyle; switch(uMsg) case WM_CREATE: MakeWndTrans(hWnd, 80); break; case WM_DESTROY: RemoveProp(hWnd, TEXT(OldWndProc); SetWindowLong(hWnd, GWL_WNDPROC, lRet); break; return CallWindowProc(WNDPROC)lRet, hWnd, uMsg, wParam, lParam);LRESULT CALLBACK HookCallWndP
21、roc(int nCode, WPARAM wParam, LPARAM lParam) CWPSTRUCT cwps; LONG lRet; if( nCode = HC_ACTION ) CopyMemory(&cwps, (LPVOID)lParam, sizeof(CWPSTRUCT); switch(cwps.message) case WM_CREATE: TCHAR szClass128; GetClassName(cwps.hwnd, szClass, 127 * sizeof(TCHAR);/should fix here if(lstrcmpi(szClass, TEXT(#32768)=0) lRet = SetWindowLong(cwps.hwnd, GWL_WNDPROC, (LONG)SubClassWndProc); SetProp(cwps.hwnd, TEXT(OldWndProc), (HANDLE)lRet); break; return CallNextHookEx(HHOOK)WH_CALLWNDPROC, nCode, wParam, lParam);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1