時(shí)鐘小程序 v 1.0
////////////////////////////////////////////////////////////////////////////////
// 時(shí)間:22:05 2005-10-5
////////////////////////////////////////////////////////////////////////////////
主要程序文件
stdafx.h
stdafx.cpp
Resource.h
Clock.h
Clock.cpp
clockWnd.h
colokWnd.cpp
功能:
顯示當(dāng)前時(shí)間
界面有半透明效果
能鎖定到桌面,使之不能移動(dòng)
當(dāng)然也能解鎖,使之能夠移動(dòng)到你想要的地方
結(jié)束程序,退出
想到增加或改進(jìn)的地方:
去掉任務(wù)欄中的小窗口,改放到“通知區(qū)域”
增加透明度調(diào)節(jié)
增加新的界面風(fēng)格
將界面風(fēng)格部分與主程序分離
加入鬧鐘、倒計(jì)時(shí)、秒表等功能
程序效果
以下為源程序:
////////////////////////////////////////////////////////////////////////////////
// 程序員:黃江斌
// 功能:時(shí)鐘小程序 v1.0
// 時(shí)間:22:13 2005-10-5
// 最后修改時(shí)間:22:13 2005-10-5
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// stdafx.h : 標(biāo)準(zhǔn)系統(tǒng)包含文件的包含文件,
// 或是經(jīng)常使用但不常更改的
// 項(xiàng)目特定的包含文件
#pragma once
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // 從 Windows 標(biāo)頭中排除不常使用的資料
#endif
// 如果您必須使用下列所指定的平臺(tái)之前的平臺(tái),則修改下面的定義。
// 有關(guān)不同平臺(tái)的相應(yīng)值的最新信息,請(qǐng)參考 MSDN。
#ifndef WINVER // 允許使用 Windows 95 和 Windows NT 4 或更高版本的特定功能。
#define WINVER 0x0400 //為 Windows98 和 Windows 2000 及更新版本改變?yōu)檫m當(dāng)?shù)闹怠?br>#endif
#ifndef _WIN32_WINNT // 允許使用 Windows NT 4 或更高版本的特定功能。
#define _WIN32_WINNT 0x0400 //為 Windows98 和 Windows 2000 及更新版本改變?yōu)檫m當(dāng)?shù)闹怠?br>#endif
#ifndef _WIN32_WINDOWS // 允許使用 Windows 98 或更高版本的特定功能。
#define _WIN32_WINDOWS 0x0410 //為 Windows Me 及更新版本改變?yōu)檫m當(dāng)?shù)闹怠?br>#endif
#ifndef _WIN32_IE // 允許使用 IE 4.0 或更高版本的特定功能。
#define _WIN32_IE 0x0400 //為 IE 5.0 及更新版本改變?yōu)檫m當(dāng)?shù)闹怠?br>#endif
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // 某些 CString 構(gòu)造函數(shù)將是顯式的
// 關(guān)閉 MFC 對(duì)某些常見但經(jīng)常被安全忽略的警告消息的隱藏
#define _AFX_ALL_WARNINGS
#include <afxwin.h> // MFC 核心和標(biāo)準(zhǔn)組件
#include <afxext.h> // MFC 擴(kuò)展
#include <afxdisp.h> // MFC 自動(dòng)化類
#include <afxdtctl.h> // Internet Explorer 4 公共控件的 MFC 支持
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // Windows 公共控件的 MFC 支持
#endif // _AFX_NO_AFXCMN_SUPPORT
#include "gdiplus.h"
using namespace Gdiplus;
////////////////////////////////////////////////////////////////////////////////
// stdafx.cpp : 只包括標(biāo)準(zhǔn)包含文件的源文件
// clock.pch 將是預(yù)編譯頭
// stdafx.obj 將包含預(yù)編譯類型信息
#include "stdafx.h"
////////////////////////////////////////////////////////////////////////////////
// Resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by clock.rc
//
#define IDM_ABOUTBOX 0x0010
#define IDD_ABOUTBOX 100
#define IDS_ABOUTBOX 101
#define IDD_CLOCK_DIALOG 102
#define IDR_MAINFRAME 128
#define IDR_MENU1 129
#define IDR_OPTIONMENU 129
#define ID_LOCK 32773
#define ID_QUIT 32774
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 130
#define _APS_NEXT_COMMAND_VALUE 32775
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
////////////////////////////////////////////////////////////////////////////////
// ClockWnd.h
#pragma once
// CClockWnd
class CClockWnd : public CWnd
{
DECLARE_DYNAMIC(CClockWnd)
public:
CClockWnd();
virtual ~CClockWnd();
protected:
DECLARE_MESSAGE_MAP()
public:
//是否鎖定
int locked;
private:
//彈出菜單
CMenu menu;
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
afx_msg UINT OnNcHitTest(CPoint point);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnQuit();
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLock();
afx_msg void OnUpdateLock(CCmdUI *pCmdUI);
};
////////////////////////////////////////////////////////////////////////////////
// ClockWnd.cpp : 實(shí)現(xiàn)文件
//
#include "stdafx.h"
#include "clock.h"
#include "ClockWnd.h"
#include ".\clockwnd.h"
#include "time.h"
#include "math.h"
// CClockWnd
IMPLEMENT_DYNAMIC(CClockWnd, CWnd)
CClockWnd::CClockWnd()
{
//創(chuàng)建主窗口
char windowTitle[] = "ClockWindow";
CreateEx(
0 ,
::AfxRegisterWndClass(
0 ,
::LoadCursor( NULL , IDC_ARROW ) ,
NULL ,
NULL
) ,
windowTitle ,
WS_POPUP ,
0 , 0 , 0 , 0 ,
NULL ,
NULL
);
//設(shè)置變量
//鐘表面沒有鎖定
locked = false;
//程序計(jì)時(shí)開始
SetTimer( 1 , 1000 , NULL );
//載入菜單
menu.LoadMenu( IDR_OPTIONMENU );
}
CClockWnd::~CClockWnd()
{
}
BEGIN_MESSAGE_MAP(CClockWnd, CWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_NCHITTEST()
ON_WM_TIMER()
ON_COMMAND(ID_QUIT, OnQuit)
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_LOCK, OnLock)
ON_UPDATE_COMMAND_UI(ID_LOCK, OnUpdateLock)
END_MESSAGE_MAP()
// CClockWnd 消息處理程序
int CClockWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您專用的創(chuàng)建代碼
// 設(shè)置窗口大小
this->MoveWindow( 100 , 100 , 150 , 150 );
//設(shè)置為透明窗口類型
SetWindowLong( this->GetSafeHwnd(),GWL_EXSTYLE,
GetWindowLong( this->GetSafeHwnd() , GWL_EXSTYLE ) ^ 0x80000 );
HMODULE hUser32 = LoadLibrary("User32.DLL");
if( hUser32 )
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
//取得SetLayeredWindowAttributes函數(shù)指針
fun=(MYFUNC)GetProcAddress( hUser32, "SetLayeredWindowAttributes" );
if( fun )
fun( this->GetSafeHwnd() , 0 , 64 , 3 );
FreeLibrary( hUser32 );
}
return 0;
}
void CClockWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此處添加消息處理程序代碼
// 不為繪圖消息調(diào)用 CWnd::OnPaint()
Graphics graphics( dc.m_hDC );
graphics.Clear( Color( 255 , 0 , 0 , 0 ) );
int width = 150;
int height = 150;
float PI = 3.1415926;
//表面背景
HatchBrush faceBrush( (HatchStyle)9 , Color( 255 , 255 ,255 , 255 ) , Color( 170 , 255 , 255 , 255 ) );
graphics.FillPie( &faceBrush , 0 , 0 , width , width , 0 , 360 );
//表面邊緣
Pen edgePen( Color( 255 , 255 , 255 , 255 ) , 6 );
edgePen.SetAlignment( PenAlignmentInset );
graphics.DrawArc(&edgePen , 0 , 0 , width , height , 0 , 360 );
//表面刻度
Pen scaleB( Color( 255 , 255 , 255 , 255 ) , 5 );
Pen scaleL( Color( 255 , 255 , 255 , 255 ) , 3 );
for( int i = 0 ; i < 12 ; i++ )
{
float x1 = cos( (float)i * 30 * PI / 180.0 ) * 65 + width / 2;
float y1 = sin( (float)i * 30 * PI / 180.0 ) * 65 + height / 2;
if( i % 3 == 0 )
{
// 0 , 3 , 6 , 9 點(diǎn)用粗筆畫
float x2 = cos( (float)i * 30 * PI / 180.0 ) * 50 + width / 2;
float y2 = sin( (float)i * 30 * PI / 180.0 ) * 50 + height / 2;
graphics.DrawLine( &scaleB , x1 , y1 , x2 , y2 );
}
else
{
// 其它刻度
float x2 = cos( (float)i * 30 * PI / 180.0 ) * 55 + width / 2;
float y2 = sin( (float)i * 30 * PI / 180.0 ) * 55 + height / 2;
graphics.DrawLine( &scaleL , x1 , y1 , x2 , y2 );
}
}
//表面信息
WCHAR string1[256];
WCHAR string2[256];
wcscpy( string1 , L"zero" );
wcscpy( string2 , L"D" );
RectF layoutRect1( width / 2 - 40 , height / 2 - 40 , 80 , 30 );
RectF layoutRect2( width / 2 - 39 , height / 2 + 20 , 78 , 30 );
Font myFont( L"宋體" , 22 );
StringFormat format;
format.SetAlignment( StringAlignmentCenter );
format.SetLineAlignment( StringAlignmentCenter );
SolidBrush textBrush( Color( 255 , 255 , 255 , 255 ) );
graphics.DrawString(
string1 ,
wcslen( string1 ) ,
&myFont ,
layoutRect1 ,
&format ,
&textBrush
);
graphics.DrawString(
string2 ,
wcslen( string2 ) ,
&myFont ,
layoutRect2 ,
&format ,
&textBrush
);
//指針
graphics.SetSmoothingMode( SmoothingModeHighQuality );
time_t nowTime;
struct tm * pt;
time( &nowTime );
pt = localtime( &nowTime );
int hour = pt->tm_hour;
int minute = pt->tm_min;
int second = pt->tm_sec;
Pen hPen( Color( 255 , 255 , 255 , 255 ) , 4 );
hPen.SetAlignment( PenAlignmentCenter );
Pen mPen( Color( 255 , 255 , 255 , 255 ) , 3 );
mPen.SetAlignment( PenAlignmentCenter );
Pen sPen( Color( 255 , 255 , 255 , 255 ) , 2 );
sPen.SetAlignment( PenAlignmentCenter );
float x1 , y1;
x1 = cos( ( (double)second * 6 + 270 ) * PI / 180 ) * 50 + width / 2;
y1 = sin( ( (double)second * 6 + 270 ) * PI / 180 ) * 50 + height / 2;
graphics.DrawLine( &sPen , x1 , y1 , (float)width / 2 , (float)height / 2 );
x1 = cos( ( (double)minute * 6 + (double)second / 10.0 + 270 ) * PI / 180 ) * 40 + width / 2;
y1 = sin( ( (double)minute * 6 + (double)second / 10.0 + 270 ) * PI / 180 ) * 40 + height / 2;
graphics.DrawLine( &mPen , x1 , y1 , (float)width / 2 , (float)height / 2 );
x1 = cos( ( (double)hour * 30 + (double)minute * 30.0 / 60.0 + 270 ) * PI / 180 ) * 30 + width / 2;
y1 = sin( ( (double)hour * 30 + (double)minute * 30.0 / 60.0 + 270 ) * PI / 180 ) * 30 + height / 2;
graphics.DrawLine( &hPen , x1 , y1 , (float)width / 2 , (float)height / 2 );
}// end OnPaint()
UINT CClockWnd::OnNcHitTest(CPoint point)
{
// TODO: 在此添加消息處理程序代碼和/或調(diào)用默認(rèn)值
UINT nHitTest = CWnd::OnNcHitTest(point);
CRect rect;
GetClientRect( rect );
POINT p;
p.x = point.x;
p.y = point.y;
::ScreenToClient( this->m_hWnd , &p );
rect.SetRect( rect.left , rect.top , rect.right , ( rect.top + rect.bottom ) / 2 );
if( rect.PtInRect( CPoint( p.x , p.y ) ) && !locked )
return HTCAPTION;
else
return nHitTest;
}
void CClockWnd::OnTimer(UINT nIDEvent)
{
// TODO: 在此添加消息處理程序代碼和/或調(diào)用默認(rèn)值
Invalidate();
CWnd::OnTimer(nIDEvent);
}
void CClockWnd::OnQuit()
{
// TODO: 在此添加命令處理程序代碼
PostQuitMessage( 0 );
}
void CClockWnd::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息處理程序代碼和/或調(diào)用默認(rèn)值
POINT p;
p.x = point.x;
p.y = point.y;
::ClientToScreen( this->m_hWnd , &p );
menu.GetSubMenu( 0 )->TrackPopupMenu( 0 , p.x , p.y , this );
CWnd::OnRButtonDown(nFlags, point);
}
void CClockWnd::OnLock()
{
// TODO: 在此添加命令處理程序代碼
this->locked = !this->locked;
}
void CClockWnd::OnUpdateLock(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用戶界面處理程序代碼
if( this->locked )
pCmdUI->SetCheck();
else
pCmdUI->SetCheck( FALSE );
}
////////////////////////////////////////////////////////////////////////////////
// clock.h : PROJECT_NAME 應(yīng)用程序的主頭文件
//
#pragma once
#ifndef __AFXWIN_H__
#error 在包含用于 PCH 的此文件之前包含“stdafx.h”
#endif
#include "resource.h" // 主符號(hào)
// CclockApp:
// 有關(guān)此類的實(shí)現(xiàn),請(qǐng)參閱 clock.cpp
//
class CclockApp : public CWinApp
{
public:
CclockApp();
virtual ~CclockApp();
// 重寫
public:
virtual BOOL InitInstance();
// 實(shí)現(xiàn)
DECLARE_MESSAGE_MAP()
};
extern CclockApp theApp;
////////////////////////////////////////////////////////////////////////////////
// clock.cpp : 定義應(yīng)用程序的類行為。
//
#include "stdafx.h"
#include "clock.h"
#include "ClockWnd.h"
#include ".\clock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
ULONG_PTR gdiplusToken;
// CclockApp
BEGIN_MESSAGE_MAP(CclockApp, CWinApp)
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
// CclockApp 構(gòu)造
CclockApp::CclockApp()
{
// TODO: 在此處添加構(gòu)造代碼,
// 將所有重要的初始化放置在 InitInstance 中
}
CclockApp::~CclockApp()
{
// TODO: 在此處添加析構(gòu)代碼,
GdiplusShutdown( gdiplusToken );
}
// 唯一的一個(gè) CclockApp 對(duì)象
CclockApp theApp;
// CclockApp 初始化
BOOL CclockApp::InitInstance()
{
// 如果一個(gè)運(yùn)行在 Windows XP 上的應(yīng)用程序清單指定要
// 使用 ComCtl32.dll 版本 6 或更高版本來啟用可視化方式,
//則需要 InitCommonControls()。否則,將無法創(chuàng)建窗口。
InitCommonControls();
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup( &gdiplusToken , &gdiplusStartupInput , NULL );
CWinApp::InitInstance();
AfxEnableControlContainer();
// 標(biāo)準(zhǔn)初始化
// 如果未使用這些功能并希望減小
// 最終可執(zhí)行文件的大小,則應(yīng)移除下列
// 不需要的特定初始化例程
// 更改用于存儲(chǔ)設(shè)置的注冊(cè)表項(xiàng)
// TODO: 應(yīng)適當(dāng)修改該字符串,
// 例如修改為公司或組織名
SetRegistryKey(_T("黃江斌制作的時(shí)鐘小程序"));
this->m_pMainWnd = new CClockWnd();
this->m_pMainWnd->ShowWindow( this->m_nCmdShow );
this->m_pMainWnd->UpdateWindow();
//CclockDlg dlg;
//m_pMainWnd = &dlg;
//INT_PTR nResponse = dlg.DoModal();
//if (nResponse == IDOK)
//{
// // TODO: 在此放置處理何時(shí)用“確定”來關(guān)閉
// //對(duì)話框的代碼
//}
//else if (nResponse == IDCANCEL)
//{
// // TODO: 在此放置處理何時(shí)用“取消”來關(guān)閉
// //對(duì)話框的代碼
//}
// 由于對(duì)話框已關(guān)閉,所以將返回 FALSE 以便退出應(yīng)用程序,
// 而不是啟動(dòng)應(yīng)用程序的消息泵。
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////
// 還有一個(gè)菜單
// IDR_OPTIONMENU
// 兩個(gè)菜單項(xiàng)
// ID_LOCK IDQUIT