Input Tips Every Day 源程序



新近把Input Tips改动了一下,使用ACCESS的文件来存取每天输入的Tip, 所以就把原来的2.0的源程序放上来,请您多指点,以改进,学习。
第一件事当然是利用VC的MFC AppWizar成立一个DIALOG based 的应用程序了, 会自动生成一个CAboutDlg, 一个App, 一个Dlg.我在第一个类里加了一个OnHelp(), 这样你在AboutDlg里点一下那个图标就能打开HELP.HTM,参见程序. 在CTipApp::InitInstance()里加了一句话,


    CTipDlg dlg;

    m_pMainWnd = &dlg;



    if (m_lpCmdLine[0] == _T('\0'))

        dlg.pFileName = "Tips.TXT";

    else

        dlg.pFileName = m_lpCmdLine;



    int nResponse = dlg.DoModal();

把输入参数转化成pFileName,实现多用户使用不同的存储文件的功能。 以下的事情都是在CTipDlg中进行了.
在资源编辑器里把那个缺省的DIALOG改成
上面的按纽我分别起ID为:ID_PREVIOUS, ID_NEXT, ID_DELETE, ID_RECORD, IDC_Tips(那个大的编辑框), IDC_TIME, IDC_INDEX. 然后对应每个按纽 在CLASSWIZARD加上CLICK的函数.
下面是TipDlg.h,定义了所有的成员函数,灰色的那段都是 CLASSWIZARD加的,


// TipDlg.h : header file

//



#if !defined(AFX_TIPDLG_H__AB6D347B_A4AB_11D3_B8FB_82898A0F2927__INCLUDED_)

#define AFX_TIPDLG_H__AB6D347B_A4AB_11D3_B8FB_82898A0F2927__INCLUDED_



#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000



#define IDM_TIMER15        WM_USER+1

#define IDM_TIMER20        WM_USER+2

#define IDM_TIMER30        WM_USER+3

#define IDM_ONTOP        WM_USER+4

#define IDM_NOTOP        WM_USER+5



/////////////////////////////////////////////////////////////////////////////

// CTipDlg dialog



class CTipDlg : public CDialog

{

private:

    int index;

    BOOL    bOnTop;

    CString pRecord;



protected://这个函数用来处理文件方面的操作,
            //在OnRecord,OnDelete,OnPrevious,OnNext中都调用.

            //在3.0中要改成采用.MDB的数据库文件操作,
            //基本上只对这里作改动.

    int FileOp (CString& tempString, BOOL bRead, BOOL bSetLength);

public:

    CString pFileName;

// Construction

public:

    CTipDlg(CWnd* pParent = NULL);    // standard constructor

//    ~CTipDlg();

// Dialog Data

    //{{AFX_DATA(CTipDlg)

    enum { IDD = IDD_TIP_DIALOG };

    CString    m_Tips;

    //}}AFX_DATA



    // ClassWizard generated virtual function overrides

    //{{AFX_VIRTUAL(CTipDlg)

    protected:

    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    //}}AFX_VIRTUAL



// Implementation

protected:

    HICON m_hIcon;



    // Generated message map functions

    //{{AFX_MSG(CTipDlg)

    virtual BOOL OnInitDialog();

    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);

    afx_msg void OnPaint();

    afx_msg HCURSOR OnQueryDragIcon();

    afx_msg void OnTimer(UINT nIDEvent);

    afx_msg void OnRecord();

    afx_msg void OnPrevious();

    afx_msg void OnNext();

    afx_msg void OnDelete();

    afx_msg BOOL OnHelpInfo(HELPINFO* pHelpInfo);

    virtual void OnCancel();

    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

};



 //{{AFX_INSERT_LOCATION}}

//Microsoft Visual C++ will insert additional declarations immediately 



#endif 
// !defined(AFX_TIPDLG_H__AB6D347B_A4AB_11D3_B8FB_82898A0F2927__INCLUDED_)







下面是TipDlg.cpp,主要的工作有两个:1,对4个按键进行相应的处理,重点当然是

文件的操作.2,对系统菜单的操作.其实这本不是重点的.

// TipDlg.cpp : implementation file





#include "stdafx.h"

#include "Tip.h"

#include "TipDlg.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 void OnHelp();

    //}}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)

    ON_BN_CLICKED(IDC_HELP1, OnHelp)

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()



///////////////////////////////////////////////

// CTipDlg dialog



CTipDlg::CTipDlg(CWnd* pParent /*=NULL*/)

    : CDialog(CTipDlg::IDD, pParent)

{

    //{{AFX_DATA_INIT(CTipDlg)

    m_Tips = _T("");

    //}}AFX_DATA_INIT

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

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

}



void CTipDlg::DoDataExchange(CDataExchange* pDX)

{

    CDialog::DoDataExchange(pDX);

    //{{AFX_DATA_MAP(CTipDlg)

    DDX_Text(pDX, IDC_Tips, m_Tips);

    //}}AFX_DATA_MAP

}



BEGIN_MESSAGE_MAP(CTipDlg, CDialog)

    //{{AFX_MSG_MAP(CTipDlg)

    ON_WM_SYSCOMMAND()

    ON_WM_PAINT()

    ON_WM_QUERYDRAGICON()

    ON_WM_TIMER()

    ON_BN_CLICKED(ID_RECORD, OnRecord)

    ON_BN_CLICKED(ID_PREVIOUS, OnPrevious)

    ON_BN_CLICKED(ID_NEXT, OnNext)

    ON_BN_CLICKED(ID_DELETE, OnDelete)

    ON_WM_HELPINFO()

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()



////////////////////////////////////////////

// CTipDlg message handlers



BOOL CTipDlg::OnInitDialog()

{

    CDialog::OnInitDialog();



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



    // IDM_ABOUTBOX must be in the system command range.

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

    ASSERT(IDM_ABOUTBOX <0xf000); 
    #ifndef _DEBUG 
        SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
        
        #endif 
        bOnTop=TRUE; 
        CMenu* pSysMenu=GetSystemMenu(FALSE); 
 if (pSysMenu != NULL)
               {pSysMenu->DeleteMenu(4, MF_BYPOSITION);    //Maximum;

        pSysMenu->DeleteMenu(2, MF_BYPOSITION);    //Size;



        pSysMenu->AppendMenu(MF_SEPARATOR);

        pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, "&About Tip...");



        pSysMenu->AppendMenu(MF_SEPARATOR);

        pSysMenu->AppendMenu(MF_STRING, IDM_TIMER15, "&15 Minutes(Default)");

        pSysMenu->AppendMenu(MF_STRING, IDM_TIMER20, "&20 Minutes");

        pSysMenu->AppendMenu(MF_STRING, IDM_TIMER30, "&30 Minutes");



        pSysMenu->AppendMenu(MF_SEPARATOR);

        pSysMenu->AppendMenu(MF_STRING, IDM_ONTOP, "Always &OnTop");

        pSysMenu->AppendMenu(MF_STRING, IDM_NOTOP, "&Cancel OnTop");



    }



    // Set the icon for this dialog. 

    //  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

    pRecord = "Tips in " + pFileName;

    index = GetProfileInt(pRecord, "number", 1);

    index = FileOp( m_Tips, TRUE, FALSE);

    int time = GetProfileInt(pRecord, "timer", 15);

    SetTimer(1, time*1000 * 60, NULL);    //15 Minutes.

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

}



void CTipDlg::OnSysCommand(UINT nID, LPARAM lParam)

{

    CAboutDlg dlgAbout;

    switch(nID)

    {

    case    IDM_ABOUTBOX:

        dlgAbout.DoModal();

        break;

    case IDM_TIMER15:

        WriteProfileString(pRecord, "timer", "15");

        SetTimer(1, 15*1000*60, NULL);    //15 Minutes.

        MessageBox("Timer has been changed as 15 Minutes.", "Right!",
                MB_ICONEXCLAMATION|MB_OK);

        break;

    case IDM_TIMER20:

        WriteProfileString(pRecord, "timer", "20");

        SetTimer(1, 20*1000*60, NULL);    //20 Minutes.

        MessageBox("Timer has been changed as 20 Minutes.", "Right!",
                MB_ICONEXCLAMATION|MB_OK);

        break;

    case IDM_TIMER30:

        WriteProfileString(pRecord, "timer", "30");

        SetTimer(1, 30*1000*60, NULL);    //30 Minutes.

        MessageBox("Timer has been changed as 30 Minutes.", "Right!",
                MB_ICONEXCLAMATION|MB_OK);

        break;

    case IDM_ONTOP:

        SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);

        bOnTop = TRUE;

        break;

    case IDM_NOTOP:

        SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);

        bOnTop = FALSE;

        break;

    default:

        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 CTipDlg::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

    {

        CString tempString;

        tempString.Format("%d", index);

        SetDlgItemText(IDC_INDEX, tempString);

        SYSTEMTIME m_Time;

        GetLocalTime(&m_Time);

        tempString.Format("%d:%d", m_Time.wHour, m_Time.wMinute);

        SetDlgItemText(IDC_TIME,  tempString);

        SetDlgItemText(IDC_Tips, m_Tips);

        CDialog::OnPaint();

    }

}



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

//  the minimized window.

HCURSOR CTipDlg::OnQueryDragIcon()

{

    return (HCURSOR) m_hIcon;

}





void CTipDlg::OnTimer(UINT nIDEvent) 

{

    // TODO: Add your message handler code here and/or call default

    //OpenIcon();

    ShowWindow(SW_SHOWNORMAL);

    CDialog::OnTimer(nIDEvent);

}



void CTipDlg::OnRecord() 

{

    // TODO: Add your control notification handler code here

    GetDlgItemText(IDC_Tips, m_Tips);

    index = GetProfileInt(pRecord, "number", 1) ;

    FileOp(m_Tips, FALSE, TRUE);

    OnPaint();

}



void CTipDlg::OnPrevious() 

{

    // TODO: Add your control notification handler code here

    if (index == 1)

        return;

    index --;

    index = FileOp(m_Tips, TRUE, FALSE);

    OnPaint();

}



void CTipDlg::OnNext() 

{

    // TODO: Add your control notification handler code here

    index ++;

    index = FileOp(m_Tips, TRUE, FALSE);

    OnPaint();

}



void CTipDlg::OnDelete() 

{

    // TODO: Add your control notification handler code here

    int total = GetProfileInt(pRecord, "number", 1);

    if (index >= total)

    {

        index --;

        index = FileOp(m_Tips, TRUE, TRUE);

        OnPaint();

    }

}



int CTipDlg::FileOp(CString& String, BOOL bRead, BOOL bSet)

{

/*//////////////////////////////

    PHKEY hKey;

    DWORD dwDisposition;

    index = RegCreateKeyEx(HKEY_LOCAL_MACHINE, 

        "Software\Tips", 0, "Index", KEY_QUERY_VALUE , 

        KEY_ALL_ACCESS, NULL, hKey, &dwDisposition);

    long lSize;

    index = RegQueryValue(*hKey, "Index", &index, &lSize);

/*//////////////////////////////////

    CStdioFile m_hfile;

    if( !m_hfile.Open( pFileName, CFile::modeCreate | CFile::modeNoTruncate

           | CFile::modeReadWrite | CFile::typeText ) ) {

        #ifdef     _DEBUG afxDump

    << "Unable to open file" <<

           "\n"; 
           #endif 
         exit( 1 ); 
    } 
    int    temp;    
    CString tempString1,tempString2;  
    BOOL bEOF; 
    for (temp=0;(temp<index)&&(bEOF=m_hfile.ReadString(tempString1)); temp++)
            
        {tempString2= tempString1;} 
    if(bSet== TRUE) 
        {             
        DWORD dwPosition=m_hfile.GetPosition();
        m_hfile.SetLength(dwPosition);
        if(bRead== FALSE) {
            m_hfile.WriteString(String);
            m_hfile.WriteString("\n");
            tempString2="String;" 
            index="temp" + 1; 
        }             
        CString number;     
        number.Format("%d", index); 
        WriteProfileString(pRecord, "number", number);
    }
    m_hfile.Close();
    String=  "tempString2;"  
    return temp;}

    
    BOOL CTipDlg::OnHelpInfo(HELPINFO* pHelpInfo) 
    { // TODO: Add your message handler code here and/or call default

    ShellExecute(*this, "open", "help.htm", NULL, NULL, SW_SHOW);

    return TRUE;

}



void CTipDlg::OnCancel() 

{

    // TODO: Add extra cleanup here

    ShowWindow(SW_SHOWMINIMIZED);

    if (bOnTop == FALSE)

        CDialog::OnCancel();

    else

    {

    int iTemp = MessageBox("Are you sure to close the Tips?", "Exit?",
        MB_YESNO|MB_ICONSTOP|MB_DEFBUTTON2);

    if (iTemp == IDYES)

            CDialog::OnCancel();

    }

}



void CAboutDlg::OnHelp() 

{

    // TODO: Add your control notification handler code here

    ShellExecute(*this, "open", "help.htm", NULL, NULL, SW_SHOW);    

}



Copyright 1998-2002 Fadshop.net, Inc. All rights reserved. jhj123@163.net