⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timedlg.cpp

📁 一个vc的电子时钟的程序!!实现基本的时钟功能。
💻 CPP
字号:
// timeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "time.h"
#include "timeDlg.h"
#include "Splash.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTimeDlg dialog

CTimeDlg::CTimeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTimeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTimeDlg)
	m_time = 30;
	m_curtime = _T("");
	m_suiji = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_bool=FALSE;

	//m_tra=200;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTimeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTimeDlg)
	DDX_Control(pDX, IDC_COMBO2, m_str);
	DDX_Text(pDX, IDC_EDIT1, m_time);
	DDX_Text(pDX, IDC_STATIC1, m_curtime);
	DDX_Text(pDX, IDC_EDIT2, m_suiji);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTimeDlg, CDialog)
	//{{AFX_MSG_MAP(CTimeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_APPLY, OnApply)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_ALL, OnAll)
	ON_WM_CREATE()
	ON_CBN_SELCHANGE(IDC_COMBO2, OnSelchangeCombo2)
	ON_BN_CLICKED(IDC_SUIJI, OnSuiji)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTimeDlg message handlers

BOOL CTimeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.
	if (!m_bar.CreateEx(this,TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS,CRect(4, 4, 0, 0), m_bar.LoadToolBar(IDR_TOOLBAR1)))
	{
		TRACE0("gaoqian");
		return FALSE;
	}
	m_bar.ShowWindow(SW_SHOW);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
    CSpinButtonCtrl *spin;
    spin=(CSpinButtonCtrl *)GetDlgItem(IDC_SPIN1);
	spin->SetRange(0,150);
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	SetTimer(1,1000,NULL);
    m_str.SetCurSel(0);
	m_change=FALSE;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTimeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTimeDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTimeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTimeDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
   	CString str;
    SYSTEMTIME tm;
	GetLocalTime(&tm);
	str.Format("当前时间是:%02d:%02d:%02d",tm.wHour,tm.wMinute,tm.wSecond);
	SetDlgItemText(IDC_STATIC1,str);
	if (m_bool)
	{ 
		//CString str2;
		tcurr++;
		str.Format("你还有%d秒关机",tnow-tcurr);
		GetDlgItem(IDC_STATIC4)->SetWindowText(str);
		//str.format("你还有%d秒关机!!",tnow-tcurr);
		//GetDlgItem(IDC_STATIC3)->SetWindowText(str);
		if (tcurr==tnow)
		{
			OnButton2();
		}
		CDialog::OnTimer(nIDEvent);
	}

//void CTimeDlg::OnButton1() 
//{
	// TODO: Add your control notification handler code here
	//UpdateData(TRUE);
	
	//CString str;
    
  //  GetDlgItem(IDC_STATIC3)->SetWindowText(str);
//}
//}

//DEL void CTimeDlg::OnButton() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 	m_bool=TRUE;
//DEL     tnow=m_time*60;
//DEL }


//DEL void CTimeDlg::OnApply() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 	// m_bool=TRUE;
//DEL    // tnow=m_time*60;	
//DEL }
}

void CTimeDlg::OnApply() 
{
	// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_bool=TRUE;
tnow=m_time*60;
tcurr=0;
}

void CTimeDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here

	// TODO: Add your control notification handler code here
	KillTimer(1);
	DWORD dwVersion = GetVersion();
	if(dwVersion < 0x80000000)    //是NT系列操作系统
	{ 
		HANDLE hToken;
		TOKEN_PRIVILEGES tkp;

		OpenProcessToken(GetCurrentProcess(),
				TOKEN_ADJUST_PRIVILEGES 
				| TOKEN_QUERY,
				&hToken);
		LookupPrivilegeValue(NULL,
				SE_SHUTDOWN_NAME,
				&tkp.Privileges[0].Luid);
		tkp.PrivilegeCount = 1;
		tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
		//提升权限,以便关闭系统
		AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
		
		//重启
		ExitWindowsEx(EWX_SHUTDOWN, 0); 
	} 
	else    //是9X系列操作系统 
	ExitWindowsEx(EWX_SHUTDOWN,0); 
	
}

void CTimeDlg::ontopwindows()
{::SetWindowPos(AfxGetMainWnd()->m_hWnd,HWND_TOPMOST,-1,-1,-1,-1,SWP_NOMOVE|SWP_NOSIZE);

}

//DEL void CTimeDlg::OnButton1() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 	
//DEL }

void CTimeDlg::OnAll() 
{
	// TODO: Add your control notification handler code here
	ontopwindows();
}

int CTimeDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{    CSplashWnd::ShowSplashScreen(this);
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	
	return 0;
}

void CTimeDlg::OnSelchangeCombo2() 
{
	// TODO: Add your control notification handler code here
	//char ch[10];
   
	int int1;
//int com[5];
	//UpdateData(FALSE);

int1=m_str.GetCurSel();
m_str.GetLBText(int1,str.GetBuffer(4));

UpdateData(TRUE);

if (str.Compare("20%")==0)
{m_tra=220;
m_change=TRUE;
}

if (str.Compare("40%")==0)
{m_tra=180;
m_change=TRUE;
}

if (str.Compare("60%")==0)
{m_tra=100;
m_change=TRUE;
}

if (str.Compare("80%")==0)
{	
	m_tra=50;
	m_change=TRUE;
}
	
	HINSTANCE hinst=LoadLibrary("User32.DLL");
	if (hinst&&m_change==TRUE)
	{
		SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),
		GWL_EXSTYLE)|0x80000);
		typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
		MYFUNC fun=NULL;
		fun=(MYFUNC)GetProcAddress(hinst,"SetLayeredWindowAttributes");
		if(fun)
		{
			fun(this->GetSafeHwnd(),0,m_tra,2);	
			ShowWindow(SW_SHOW);
		}
		m_change=FALSE;
		FreeLibrary(hinst);
	}
	
	

}

void CTimeDlg::OnSuiji() 
{
	// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_suiji=(int)AverageRandom(0,30);
UpdateData(FALSE);
}

double CTimeDlg::AverageRandom(double min, double max)
{
    int minInteger = (int)(min*10000);
	int maxInteger = (int)(max*10000);
	int randInteger = rand()*rand();
	int diffInteger = maxInteger - minInteger;
	int resultInteger = randInteger % diffInteger + minInteger;
	return resultInteger/10000.0;
}

//DEL void CTimeDlg::OnButton1() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 	
//DEL }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -