//程序名:文本时钟/TextClock //作者wisepragma //主页:http://blog.csdn.net/wisepragma //taskkill /im textclock.exe //cl textclock.cpp /W3 //start textclock.exe #include <windows.h> #include "tchar.h" #include <stdio.h> #pragma comment(lib,"user32.lib") #pragma comment(lib,"kernel32.lib") #pragma comment(lib,"gdi32.lib") //#define TEXTCOLOR (RGB(189,85,178)) //紫 #define TEXTCOLOR (RGB(202,202,202))//白灰 bool bAdjustWindow=TRUE; TCHAR * ClsName =TEXT("class_TextClock"); TCHAR * WndName = TEXT("window_TextClock"); TCHAR * lpszDayOfWeek[]= { TEXT("Sunday"),TEXT("Monday"),TEXT("Tuesday"),TEXT("Wednesday"),TEXT("Thursday"), TEXT("Friday"), TEXT("Saturday") }; TCHAR * lpszMonthOfYear[]= { TEXT("January"), TEXT("February"), TEXT("March"), TEXT("April"), TEXT("May"), TEXT("June"), TEXT("July"), TEXT("August"), TEXT("September"), TEXT("October"), TEXT("November"), TEXT("December")}; LRESULT CALLBACK MainWndProc(HWND hwnd, UINT Msg,WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,LPSTR lpCmdLine, int nCmdShow) { HANDLE hMutex=CreateMutex(NULL,FALSE,TEXT("OnlyOneInstance_TextClock")); if(hMutex==NULL || ERROR_ALREADY_EXISTS==GetLastError())return 0; WNDCLASSEX m_WndClsEx; m_WndClsEx.cbSize = sizeof(WNDCLASSEX); m_WndClsEx.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS; m_WndClsEx.lpfnWndProc = MainWndProc; m_WndClsEx.cbClsExtra = 0; m_WndClsEx.cbWndExtra = 0; m_WndClsEx.hInstance = hInstance; m_WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION); m_WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW); m_WndClsEx.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); m_WndClsEx.lpszMenuName = NULL; m_WndClsEx.lpszClassName = ClsName; m_WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&m_WndClsEx); HWND hwnd=hwnd = CreateWindowEx (WS_EX_TOPMOST|WS_EX_TOOLWINDOW, ClsName,WndName,WS_POPUPWINDOW, 0,0,1,1, NULL, NULL, hInstance, NULL); ShowWindow (hwnd,SW_SHOWNORMAL) ; UpdateWindow (hwnd) ; MSG Msg; while( GetMessage(&Msg, NULL, 0, 0) ) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return 0; } LRESULT CALLBACK MainWndProc(HWND hwnd, UINT Msg,WPARAM wParam, LPARAM lParam) { static SYSTEMTIME sysTime; static TCHAR buffer[100]; static HDC hdc; static PAINTSTRUCT ps; static LOGFONT lf; static HBRUSH hbr; static HRGN hrgn; static HFONT hf; static TEXTMETRIC tm; switch(Msg) { case WM_TIMER: InvalidateRect(hwnd,NULL,FALSE); return 0; case WM_CREATE: SetTimer(hwnd,WM_NULL,100,NULL); return 0; case WM_LBUTTONDOWN: if( GetKeyState(VK_CONTROL)&0x80000000 )SendMessage(hwnd,WM_NCLBUTTONDOWN,HTCAPTION,lParam); return 0; case WM_PAINT: { hdc=BeginPaint(hwnd,&ps); GetTextMetrics(hdc,&tm); memset(&lf,0,sizeof(lf)); lf.lfHeight=(tm.tmHeight+tm.tmExternalLeading)* 4; lf.lfWidth=tm.tmAveCharWidth*3; lf.lfWeight=FW_HEAVY; //lf.lfItalic=TRUE; //lf.lfUnderline=FALSE; TCHAR *FontFaceName=TEXT("Monotype Corsiva");//TEXT("Monotype Corsiva")TEXT("Lucida Console"))TEXT("Fixedsys")TEXT("Verdana"))TEXT("DotumChe")TEXT("Arial Black"))TEXT("幼圆") lstrcpy(lf.lfFaceName,FontFaceName); memset(&sysTime,0,sizeof(sysTime)); GetLocalTime(&sysTime); TCHAR *timeFormat=TEXT("%s %02ld,%ld[%s]%02ld:%02ld.%02ld"); wsprintf(buffer,timeFormat, lpszMonthOfYear[sysTime.wMonth-1]/*January在序号0*/,sysTime.wDay,sysTime.wYear, lpszDayOfWeek[sysTime.wDayOfWeek]/*Sunday在序号0*/, sysTime.wHour,sysTime.wMinute,sysTime.wSecond); hf=CreateFontIndirect(&lf);//+font(创建) SelectObject(hdc,hf);// =font(使用) RECT rectDesktop; SystemParametersInfo(SPI_GETWORKAREA,0,&rectDesktop,0); SIZE czStr; GetTextExtentPoint32(hdc,buffer,lstrlen(buffer),&czStr);//根据字符串字体取得显示的宽度和亮度,然后用于首次显示调整窗体位置和大小 if(bAdjustWindow) { MoveWindow( hwnd, GetSystemMetrics(SM_CXFULLSCREEN)-czStr.cx-25,//微调 rectDesktop.bottom-czStr.cy,//rectDesktop.bottom是系统状态栏最高处的纵坐标 czStr.cx+20, czStr.cy, TRUE); bAdjustWindow=false; } SetBkMode(hdc,TRANSPARENT); BeginPath(hdc); TextOut(hdc,0,0,buffer,lstrlen(buffer)); EndPath(hdc); hrgn=PathToRegion(hdc);// +hrgn hbr=CreateSolidBrush(TEXTCOLOR);//+Brush FillRgn(hdc,hrgn,hbr);//=Brush SetWindowRgn(hwnd,hrgn,TRUE);//=-hrgn(使用并删除) DeleteObject(hbr);//-Brush DeleteObject(hf);//-font(删除) EndPaint(hwnd,&ps); } return 0; case WM_KEYDOWN: if(wParam==VK_ESCAPE) PostQuitMessage(WM_QUIT); return 0; case WM_CLOSE: case WM_DESTROY: PostQuitMessage(WM_QUIT); return 0; } return DefWindowProc(hwnd, Msg, wParam, lParam); }
//程序名:文本时钟/TextClock //作者wisepragma //主页:http://blog.csdn.net/wisepragma //taskkill /im TextClock.exe //cl TextClock.CPP /W3 //start TextClock.exe #include <windows.h> #include "tchar.h" #include <stdio.h> #pragma comment(lib,"user32.lib") #pragma comment(lib,"kernel32.lib") #pragma comment(lib,"gdi32.lib") #define TEXTCOLOR (RGB(189,85,178)) bool bAdjustWindow=TRUE; TCHAR * ClsName =TEXT("class_TextClock"); TCHAR * WndName = TEXT("window_TextClock"); TCHAR * lpszDayOfWeek[]= { TEXT("Sunday"),TEXT("Monday"),TEXT("Tuesday"),TEXT("Wednesday"),TEXT("Thursday"), TEXT("Friday"), TEXT("Saturday") }; LRESULT CALLBACK MainWndProc(HWND hwnd, UINT Msg,WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,LPSTR lpCmdLine, int nCmdShow) { HANDLE hMutex=CreateMutex(NULL,FALSE,TEXT("OnlyOneInstance_TextClock")); if(hMutex==NULL || ERROR_ALREADY_EXISTS==GetLastError())return 0; WNDCLASSEX m_WndClsEx; m_WndClsEx.cbSize = sizeof(WNDCLASSEX); m_WndClsEx.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS; m_WndClsEx.lpfnWndProc = MainWndProc; m_WndClsEx.cbClsExtra = 0; m_WndClsEx.cbWndExtra = 0; m_WndClsEx.hInstance = hInstance; m_WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION); m_WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW); m_WndClsEx.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); m_WndClsEx.lpszMenuName = NULL; m_WndClsEx.lpszClassName = ClsName; m_WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&m_WndClsEx); HWND hwnd=hwnd = CreateWindowEx (WS_EX_TOPMOST|WS_EX_TOOLWINDOW, ClsName,WndName,WS_POPUPWINDOW, 0,0,1,1, NULL, NULL, hInstance, NULL); ShowWindow (hwnd,SW_SHOWNORMAL) ; UpdateWindow (hwnd) ; MSG Msg; while( GetMessage(&Msg, NULL, 0, 0) ) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return 0; } LRESULT CALLBACK MainWndProc(HWND hwnd, UINT Msg,WPARAM wParam, LPARAM lParam) { static SYSTEMTIME sysTime; static TCHAR buffer[100],*timeFormat=TEXT("%ld.%02ld.%02ld[%s]%02ld:%02ld.%02ld"); static HDC hdc; static PAINTSTRUCT ps; static LOGFONT lf; static HBRUSH hbr; static HRGN hrgn; static HFONT hf; static TEXTMETRIC tm; switch(Msg) { case WM_TIMER: InvalidateRect(hwnd,NULL,FALSE); return 0; case WM_CREATE: SetTimer(hwnd,WM_NULL,100,NULL); return 0; case WM_LBUTTONDOWN: SendMessage(hwnd,WM_NCLBUTTONDOWN,HTCAPTION,lParam); return 0; case WM_PAINT: { hdc=BeginPaint(hwnd,&ps); GetTextMetrics(hdc,&tm); memset(&lf,0,sizeof(lf)); lf.lfHeight=(tm.tmHeight+tm.tmExternalLeading)*2; lf.lfWidth=tm.tmAveCharWidth*2; lf.lfWeight=FW_HEAVY; //lf.lfItalic=TRUE; //lf.lfUnderline=FALSE; TCHAR *FontFaceName=TEXT("Fixedsys");//TEXT("Lucida Console"))TEXT("Fixedsys")TEXT("Verdana"))TEXT("DotumChe")TEXT("Arial Black"))TEXT("幼圆") lstrcpy(lf.lfFaceName,FontFaceName); memset(&sysTime,0,sizeof(sysTime)); GetLocalTime(&sysTime); wsprintf(buffer,timeFormat, sysTime.wYear,sysTime.wMonth,sysTime.wDay, lpszDayOfWeek[sysTime.wDayOfWeek], sysTime.wHour,sysTime.wMinute,sysTime.wSecond); hf=CreateFontIndirect(&lf);//+font(创建) SelectObject(hdc,hf);// =font(使用) RECT rectDesktop; SystemParametersInfo(SPI_GETWORKAREA,0,&rectDesktop,0); SIZE czStr; GetTextExtentPoint32(hdc,buffer,lstrlen(buffer),&czStr);//根据字符串字体取得显示的宽度和亮度,然后用于首次显示调整窗体位置和大小 if(bAdjustWindow) { MoveWindow( hwnd, GetSystemMetrics(SM_CXFULLSCREEN)-czStr.cx-10,//+10和下面的-10是微调 rectDesktop.bottom-czStr.cy,//rectDesktop.bottom是系统状态栏最高处的纵坐标 czStr.cx+10, czStr.cy, TRUE); bAdjustWindow=false; } SetBkMode(hdc,TRANSPARENT); BeginPath(hdc); TextOut(hdc,0,0,buffer,_tcslen(buffer)); EndPath(hdc); hrgn=PathToRegion(hdc);// +hrgn hbr=CreateSolidBrush(TEXTCOLOR);//+Brush FillRgn(hdc,hrgn,hbr);//=Brush SetWindowRgn(hwnd,hrgn,TRUE);//=-hrgn(使用并删除) DeleteObject(hbr);//-Brush DeleteObject(hf);//-font(删除) EndPaint(hwnd,&ps); } return 0; case WM_KEYDOWN: if(wParam==VK_ESCAPE) PostQuitMessage(WM_QUIT); return 0; case WM_CLOSE: case WM_DESTROY: PostQuitMessage(WM_QUIT); return 0; } return DefWindowProc(hwnd, Msg, wParam, lParam); }