2012年3月22日 星期四

[VC(Visual C++) ]- 抓取目前時間

[VC(Visual C++) ]- 抓取目前時間




本篇分享抓取目前時間範例有興趣的(C/P)同好歡迎來http://filemarkets.com/file/jashliao/5e78b377/索取因為我不會上傳檔案分享哈哈 ^ ^












主要程式碼



001    // GetNowTimeDlg.cpp : implementation file                

002    //                

003                    

004    #include "stdafx.h"                

005    #include "GetNowTime.h"                

006    #include "GetNowTimeDlg.h"                

007                    

008    #ifdef _DEBUG                

009    #definenew DEBUG_NEW                

010    #undef THIS_FILE                

011    staticchar THIS_FILE[] = __FILE__;                

012    #endif                

013                    

014    /////////////////////////////////////////////////////////////////////////////                

015    // CAboutDlg dialog used for App About                

016                    

017    class CAboutDlg : public CDialog                

018    {                

019    public:                

020        CAboutDlg();            

021                    

022    // Dialog Data                

023        //{{AFX_DATA(CAboutDlg)            

024        enum { IDD = IDD_ABOUTBOX };            

025        //}}AFX_DATA            

026                    

027        // ClassWizard generated virtual function overrides            

028        //{{AFX_VIRTUAL(CAboutDlg)            

029        protected:            

030        virtualvoid DoDataExchange(CDataExchange* pDX);    // DDX/DDV support            

031        //}}AFX_VIRTUAL            

032                    

033    // Implementation                

034    protected:                

035        //{{AFX_MSG(CAboutDlg)            

036        //}}AFX_MSG            

037        DECLARE_MESSAGE_MAP()            

038    };                

039                    

040    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)                

041    {                

042        //{{AFX_DATA_INIT(CAboutDlg)            

043        //}}AFX_DATA_INIT            

044    }                

045                    

046    void CAboutDlg::DoDataExchange(CDataExchange* pDX)                

047    {                

048        CDialog::DoDataExchange(pDX);            

049        //{{AFX_DATA_MAP(CAboutDlg)            

050        //}}AFX_DATA_MAP            

051    }                

052                    

053    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)                

054        //{{AFX_MSG_MAP(CAboutDlg)            

055            // No message handlers        

056        //}}AFX_MSG_MAP            

057    END_MESSAGE_MAP()                

058                    

059    /////////////////////////////////////////////////////////////////////////////                

060    // CGetNowTimeDlg dialog                

061                    

062    CGetNowTimeDlg::CGetNowTimeDlg(CWnd* pParent /*=NULL*/)                

063        : CDialog(CGetNowTimeDlg::IDD, pParent)            

064    {                

065        //{{AFX_DATA_INIT(CGetNowTimeDlg)            

066        m_v1 = 0.0f;            

067        m_v2 = 0.0f;            

068        m_v3 = 0.0f;            

069        m_v4 = 0.0f;            

070        m_v5 = 0.0f;            

071        m_v6 = 0.0f;            

072        //}}AFX_DATA_INIT            

073        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32            

074        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);            

075    }                

076                    

077    void CGetNowTimeDlg::DoDataExchange(CDataExchange* pDX)                

078    {                

079        CDialog::DoDataExchange(pDX);            

080        //{{AFX_DATA_MAP(CGetNowTimeDlg)            

081        DDX_Text(pDX, IDC_EDIT1, m_v1);            

082        DDX_Text(pDX, IDC_EDIT2, m_v2);            

083        DDX_Text(pDX, IDC_EDIT3, m_v3);            

084        DDX_Text(pDX, IDC_EDIT4, m_v4);            

085        DDX_Text(pDX, IDC_EDIT5, m_v5);            

086        DDX_Text(pDX, IDC_EDIT6, m_v6);            

087        //}}AFX_DATA_MAP            

088    }                

089                    

090    BEGIN_MESSAGE_MAP(CGetNowTimeDlg, CDialog)                

091        //{{AFX_MSG_MAP(CGetNowTimeDlg)            

092        ON_WM_SYSCOMMAND()            

093        ON_WM_PAINT()            

094        ON_WM_QUERYDRAGICON()            

095        ON_BN_CLICKED(IDC_BUTTON1, OnButton1)            

096        ON_BN_CLICKED(IDC_BUTTON2, OnButton2)            

097        //}}AFX_MSG_MAP            

098    END_MESSAGE_MAP()                

099                    

100    /////////////////////////////////////////////////////////////////////////////                

101    // CGetNowTimeDlg message handlers                

102                    

103    BOOL CGetNowTimeDlg::OnInitDialog()                

104    {                

105        CDialog::OnInitDialog();            

106                    

107        // Add "About..." menu item to system menu.            

108                    

109        // IDM_ABOUTBOX must be in the system command range.            

110        ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);            

111        ASSERT(IDM_ABOUTBOX < 0xF000);            

112                    

113        CMenu* pSysMenu = GetSystemMenu(FALSE);            

114        if (pSysMenu != NULL)            

115        {            

116            CString strAboutMenu;        

117            strAboutMenu.LoadString(IDS_ABOUTBOX);        

118            if (!strAboutMenu.IsEmpty())        

119            {        

120                pSysMenu->AppendMenu(MF_SEPARATOR);    

121                pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);    

122            }        

123        }            

124                    

125        // Set the icon for this dialog.  The framework does this automatically            

126        //  when the application's main window is not a dialog            

127        SetIcon(m_hIcon, TRUE);            // Set big icon

128        SetIcon(m_hIcon, FALSE);        // Set small icon    

129                    

130        // TODO: Add extra initialization here            

131                    

132        return TRUE;  // return TRUE  unless you set the focus to a control            

133    }                

134                    

135    void CGetNowTimeDlg::OnSysCommand(UINT nID, LPARAM lParam)                

136    {                

137        if ((nID & 0xFFF0) == IDM_ABOUTBOX)            

138        {            

139            CAboutDlg dlgAbout;        

140            dlgAbout.DoModal();        

141        }            

142        else            

143        {            

144            CDialog::OnSysCommand(nID, lParam);        

145        }            

146    }                

147                    

148    // If you add a minimize button to your dialog, you will need the code below                

149    //  to draw the icon.  For MFC applications using the document/view model,                

150    //  this is automatically done for you by the framework.                

151                    

152    void CGetNowTimeDlg::OnPaint()                 

153    {                

154        if (IsIconic())            

155        {            

156            CPaintDC dc(this); // device context for painting        

157                    

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

159                    

160            // Center icon in client rectangle        

161            int cxIcon = GetSystemMetrics(SM_CXICON);        

162            int cyIcon = GetSystemMetrics(SM_CYICON);        

163            CRect rect;        

164            GetClientRect(&rect);        

165            int x = (rect.Width() - cxIcon + 1) / 2;        

166            int y = (rect.Height() - cyIcon + 1) / 2;        

167                    

168            // Draw the icon        

169            dc.DrawIcon(x, y, m_hIcon);        

170        }            

171        else            

172        {            

173            CDialog::OnPaint();        

174        }            

175    }                

176                    

177    // The system calls this to obtain the cursor to display while the user drags                

178    //  the minimized window.                

179    HCURSOR CGetNowTimeDlg::OnQueryDragIcon()                

180    {                

181        return (HCURSOR) m_hIcon;            

182    }                

183                    

184    void CGetNowTimeDlg::OnButton1()                 

185    {                

186        // TODO: Add your control notification handler code here            

187        CTime time = CTime::GetCurrentTime();            

188        int m_nYear = time.GetYear();      ///            

189        int m_nMonth = time.GetMonth();      ///            

190        int m_nDay = time.GetDay();      ///            

191        int m_nHour = time.GetHour();      ///            

192        int m_nMinute = time.GetMinute();   ///            

193        int m_nSecond = time.GetSecond();   ///            

194        UpdateData(true);            

195        m_v1=m_nYear;            

196        m_v2=m_nMonth;            

197        m_v3=m_nDay;            

198        m_v4=m_nHour;            

199        m_v5=m_nMinute;            

200        m_v6=m_nSecond;            

201        UpdateData(false);            

202    }                

203                    

204    void CGetNowTimeDlg::OnButton2()                 

205    {                

206        // TODO: Add your control notification handler code here            

207        SYSTEMTIME ti;            

208        GetSystemTime(&ti);            

209        UpdateData(true);            

210        m_v1=ti.wYear;            

211        m_v2=ti.wMonth;            

212        m_v3=ti.wDay;            

213        m_v4=ti.wHour;            

214        m_v5=ti.wMinute;            

215        m_v6=ti.wSecond;            

216        UpdateData(false);            

217    }                

 



 


沒有留言:

張貼留言