时钟小程序 v 1.0

时钟小程序 v 1.0


// 时间:22:05 2005-10-5

主要程序文件
stdafx.h
stdafx.cpp
Resource.h
Clock.h
Clock.cpp
clockWnd.h
colokWnd.cpp

功能:
 显示当前时间
 界面有半透明效果
 能锁定到桌面,使之不能移动
 当然也能解锁,使之能够移动到你想要的地方
 结束程序,退出
 
想到增加或改进的地方:
 去掉任务栏中的小窗口,改放到“通知区域”
 增加透明度调节
 增加新的界面风格
 将界面风格部分与主程序分离
 加入闹钟、倒计时、秒表等功能

程序效果

 

以下为源程序:

 



// 程序员:黄江斌
// 功能:时钟小程序 v1.0
// 时间:22:13 2005-10-5
// 最后修改时间:22:13 2005-10-5



// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 项目特定的包含文件

#pragma once

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN  // 从 Windows 标头中排除不常使用的资料
#endif

// 如果您必须使用下列所指定的平台之前的平台,则修改下面的定义。
// 有关不同平台的相应值的最新信息,请参考 MSDN。
#ifndef WINVER    // 允许使用 Windows 95 和 Windows NT 4 或更高版本的特定功能。
#define WINVER 0x0400  //为 Windows98 和 Windows 2000 及更新版本改变为适当的值。
#endif

#ifndef _WIN32_WINNT  // 允许使用 Windows NT 4 或更高版本的特定功能。
#define _WIN32_WINNT 0x0400  //为 Windows98 和 Windows 2000 及更新版本改变为适当的值。
#endif      

#ifndef _WIN32_WINDOWS  // 允许使用 Windows 98 或更高版本的特定功能。
#define _WIN32_WINDOWS 0x0410 //为 Windows Me 及更新版本改变为适当的值。
#endif

#ifndef _WIN32_IE   // 允许使用 IE 4.0 或更高版本的特定功能。
#define _WIN32_IE 0x0400 //为 IE 5.0 及更新版本改变为适当的值。
#endif

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // 某些 CString 构造函数将是显式的

// 关闭 MFC 对某些常见但经常被安全忽略的警告消息的隐藏
#define _AFX_ALL_WARNINGS

#include <afxwin.h>         // MFC 核心和标准组件
#include <afxext.h>         // MFC 扩展
#include <afxdisp.h>        // MFC 自动化类

#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 : 只包括标准包含文件的源文件
// clock.pch 将是预编译头
// stdafx.obj 将包含预编译类型信息

#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 : 实现文件
//

#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()
{
 //创建主窗口
 char windowTitle[] = "ClockWindow";
 CreateEx(
  0 ,
  ::AfxRegisterWndClass(
   0 ,
   ::LoadCursor( NULL , IDC_ARROW ) ,
   NULL ,
   NULL
  ) ,
  windowTitle ,
  WS_POPUP ,
  0 , 0 , 0 , 0 ,
  NULL ,
  NULL
  );
 //设置变量

 //钟表面没有锁定
 locked = false;
 //程序计时开始
 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:  在此添加您专用的创建代码
 // 设置窗口大小
 this->MoveWindow( 100 , 100 , 150 , 150 );
 //设置为透明窗口类型
 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函数指针
          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: 在此处添加消息处理程序代码
 // 不为绘图消息调用 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 点用粗笔画
   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: 在此添加消息处理程序代码和/或调用默认值
 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: 在此添加消息处理程序代码和/或调用默认值
 Invalidate();
 CWnd::OnTimer(nIDEvent);
}

void CClockWnd::OnQuit()
{
 // TODO: 在此添加命令处理程序代码
 PostQuitMessage( 0 );
}

void CClockWnd::OnRButtonDown(UINT nFlags, CPoint point)
{
 // TODO: 在此添加消息处理程序代码和/或调用默认值
 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 应用程序的主头文件
//

#pragma once

#ifndef __AFXWIN_H__
 #error 在包含用于 PCH 的此文件之前包含“stdafx.h”
#endif

#include "resource.h"  // 主符号


// CclockApp:
// 有关此类的实现,请参阅 clock.cpp
//

class CclockApp : public CWinApp
{
public:
 CclockApp();
 virtual ~CclockApp();
// 重写
 public:
 virtual BOOL InitInstance();

// 实现

 DECLARE_MESSAGE_MAP()
};

extern CclockApp theApp;


// clock.cpp : 定义应用程序的类行为。
//

#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 构造

CclockApp::CclockApp()
{
 // TODO: 在此处添加构造代码,
 // 将所有重要的初始化放置在 InitInstance 中
}
CclockApp::~CclockApp()
{
 // TODO: 在此处添加析构代码,
 GdiplusShutdown( gdiplusToken );
}

// 唯一的一个 CclockApp 对象

CclockApp theApp;


// CclockApp 初始化

BOOL CclockApp::InitInstance()
{
 // 如果一个运行在 Windows XP 上的应用程序清单指定要
 // 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
 //则需要 InitCommonControls()。否则,将无法创建窗口。
 InitCommonControls();

 GdiplusStartupInput gdiplusStartupInput;
 GdiplusStartup( &gdiplusToken , &gdiplusStartupInput , NULL );

 CWinApp::InitInstance();

 AfxEnableControlContainer();

 // 标准初始化
 // 如果未使用这些功能并希望减小
 // 最终可执行文件的大小,则应移除下列
 // 不需要的特定初始化例程
 // 更改用于存储设置的注册表项
 // TODO: 应适当修改该字符串,
 // 例如修改为公司或组织名
 SetRegistryKey(_T("黄江斌制作的时钟小程序"));

 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: 在此放置处理何时用“确定”来关闭
 // //对话框的代码
 //}
 //else if (nResponse == IDCANCEL)
 //{
 // // TODO: 在此放置处理何时用“取消”来关闭
 // //对话框的代码
 //}

 // 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
 // 而不是启动应用程序的消息泵。
 return TRUE;
}


// 还有一个菜单
// IDR_OPTIONMENU
// 两个菜单项
// ID_LOCK IDQUIT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值