Source Code of Input Tips Every Day
Some users of "Input Tips every day" wrote email to me and
ask how to make such a software. So I public my source code, for study only.
Create a Dialog Based application using MFC AppWizard, and there are CAboutDlg, CTipDlg,
CTipApp created.
I add method OnHelp() in CAboutDlg class, so when you
click About, you can read help.htm directly.
In CTipApp::InitInstance(), I add some sentence to control File Name of the Dialog:
CTipDlg dlg;
m_pMainWnd = &dlg;
if (m_lpCmdLine[0] == _T('\0'))
dlg.pFileName = "Tips.TXT";
else
dlg.pFileName = m_lpCmdLine;
int nResponse = dlg.DoModal();
So, every one using the same computer can use diferent input parameters to save
his data seperately.
Create such a dialog,
and IDs of the buttons are:ID_PREVIOUS, ID_NEXT, ID_DELETE, ID_RECORD,
IDC_Tips(Edit Box), IDC_TIME, IDC_INDEX. In CLASSWIZARD
we can add OnClick function for every button.
Here is TipDlg.h which defined all of the member functions.
ATTN: gray part is added by 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://Import tant functions. handle every file operation.
// called by OnRecord,OnDelete,OnPrevious,OnNext
//When I use Access database .MDB in version 3.0,
//I only change this function.
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 before the previous line.
#endif // !defined(AFX_TIPDLG_H__AB6D347B_A4AB_11D3_B8FB_82898A0F2927__INCLUDED_)
Here is TipDlg.cpp. Handle 4 buttons.
// 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. 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
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 you want to close the Tips?\n
It's a pity!", "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);
}
^ Top
|