charbuf[10];
sprintf(buf,"%d",Primes[i]);
str+=buf;
}
pWnd->SetWindowText(str);
}
只需改动“if(m%i==0)”,并在函数体结束时加“}”。
编译程序:
-------------------Configuration:
Fund1-Win32Debug--------------------
Compiling...
Skipping...(norelevantchangesdetected)
Fund1Dlg.cpp
Fund1Dlg.obj-0error(s),0warning(s)
执行连接:
-------------------Configuration:
Fund1-Win32Debug--------------------
Compiling...
Skipping...(norelevantchangesdetected)
Fund1Dlg.cpp
Fund1Dlg.obj-0error(s),0warning(s)
测试结果,如图:
2.文件操作
首先,在资源管理器提供的控件下,建立对话框图:
源参考代码及相关注释如下:
Fund2Dlg.cpp
voidCFund2Dlg:
:
OnPrevious()//前一个记录(这里未使用数据库,即不保存到外存储器中)
{
//TODO:
Addyourcontrolnotificationhandlercodehere
if(m_StuIndex==0)return;
UpdateData();//将控件的值赋给变量
StuInfostuinfo;//定义一个结构变量,方便引用数据成员
memset(&stuinfo,0,sizeof(StuInfo));
//将变量保存到结构体中
strcpy(stuinfo.name,m_name);
strcpy(stuinfo.birthday,m_birthday);
strcpy(stuinfo.address,m_address);
strcpy(stuinfo.phone,m_phone);
strcpy(stuinfo.email,m_email);
strcpy(stuinfo.hobby,m_hobby);
strcpy(stuinfo.photofile,m_photoroute);
*m_StuIndex=stuinfo;//指针指向结构体stuinfo
if(m_StuIndex==m_Records.begin())return;//如果到顶了,返回空值
m_StuIndex--;//否则继续指向前一个记录
Display();//在框体中显示信息体
}
voidCFund2Dlg:
:
OnNext()//下一个记录(这里未使用数据库,即不保存到外存储器中)(同上)
{
if(!
m_StuIndex)return;
UpdateData();
StuInfostuinfo;
memset(&stuinfo,0,sizeof(StuInfo));
strcpy(stuinfo.name,m_name);
strcpy(stuinfo.birthday,m_birthday);
strcpy(stuinfo.address,m_address);
strcpy(stuinfo.phone,m_phone);
strcpy(stuinfo.email,m_email);
strcpy(stuinfo.hobby,m_hobby);
strcpy(stuinfo.photofile,m_photoroute);
*m_StuIndex=stuinfo;
if(m_StuIndex==m_Records.end()-1)return;
m_StuIndex++;
Display();
}
voidCFund2Dlg:
:
OnAdd()//增加记录,事件响应
{
UpdateData();//将控件的值赋给变量
StuInfostuinfo;//定义一个结构体变量
memset(&stuinfo,0,sizeof(StuInfo));
//将变量保存到结构体中
strcpy(stuinfo.name,m_name);
strcpy(stuinfo.birthday,m_birthday);
strcpy(stuinfo.address,m_address);
strcpy(stuinfo.phone,m_phone);
strcpy(stuinfo.email,m_email);
strcpy(stuinfo.hobby,m_hobby);
strcpy(stuinfo.photofile,m_photoroute);
m_Records.push_back(stuinfo);
m_StuIndex=m_Records.end();//在窗口中,始终指向最后一个记录框
//清空编辑框
m_address=_T("");
m_birthday=_T("");
m_email=_T("");
m_name=_T("");
m_phone=_T("");
m_hobby=_T("");
m_photoroute=_T("");
UpdateData(FALSE);//最后,将变量值赋给控件
}
voidCFund2Dlg:
:
Display()//信息显示在主窗口中
{
UpdateData();//将控件的值赋给变量
StuInfostuinfo=*m_StuIndex;//把当前指针所指向的内容赋给结构体stuinfo
//对应地,结构体中的数据成员对号入座
m_name=stuinfo.name;
m_birthday=stuinfo.birthday;
m_address=stuinfo.address;
m_phone=stuinfo.phone;
m_email=stuinfo.email;
m_hobby=stuinfo.hobby;
m_photoroute=stuinfo.photofile;
UpdateData(FALSE);////最后,将变量值赋给控件
if(!
m_photoroute.IsEmpty())LoadBmp(m_photoroute);
}
voidCFund2Dlg:
:
OnDel()//删除,事件响应
{
if(m_StuIndex==0)AfxMessageBox(_T("无记录!
"));//消息提示
else
{
if(m_StuIndex!
=m_Records.begin())//如果不是第一条记录,则删除当前记录,并指向前一条记录
{m_Records.erase(m_StuIndex);
m_StuIndex--;
Display();
}
else
{AfxMessageBox(_T("再删就没记录了!
"));//提示用户,此时为倒数第一条记录,注意!
//编辑框内容清空
m_address=_T("");
m_birthday=_T("");
m_email=_T("");
m_name=_T("");
m_phone=_T("");
m_hobby=_T("");
m_photoroute=_T("");
UpdateData(FALSE);////最后,将变量值赋给控件
}
}
}
编译程序:
--------------------Configuration:
Fund2-Win32Debug--------------------
Compiling...
Fund2.cpp
Fund2.obj-0error(s),0warning(s)
执行连接:
--------------------Configuration:
Fund2-Win32Debug--------------------
Compiling...
Fund2Dlg.cpp
Linking...
Creatingbrowseinfofile...
Fund2.exe-0error(s),0warning(s)
测试结果如图:
4.图形编程
首先,在菜单栏中添加用户自定义功能。
然后编写代码:
源代码参考:
//用户单击相应地菜单项,响应事件
voidCGraphicView:
:
OnDot()//点
{
//TODO:
Addyourcommandhandlercodehere
m_nDrawType=1;
}
voidCGraphicView:
:
OnLine()//直线
{
//TODO:
Addyourcommandhandlercodehere
m_nDrawType=2;
}
voidCGraphicView:
:
OnRectangle()//矩形
{
//TODO:
Addyourcommandhandlercodehere
m_nDrawType=3;
}
voidCGraphicView:
:
OnEllipse()//椭圆
{
//TODO:
Addyourcommandhandlercodehere
m_nDrawType=4;
}
voidCGraphicView:
:
OnLButtonDown(UINTnFlags,CPointpoint)//当按下左键时,保存变量
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
m_ptOrigin=point;//将点保存到成员变量当中
CView:
:
OnLButtonDown(nFlags,point);
}
voidCGraphicView:
:
OnLButtonUp(UINTnFlags,CPointpoint)//当鼠标左键松开,就可以作图(所有线条都不相互覆盖!
)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
CClientDCdc(this);
CPenpen(PS_SOLID,3,RGB(0,0,255));//创建用户画笔,方法(实线,宽度,颜色)
dc.SelectObject(&pen);//将画笔选到设备描述表当中
CBrush*pBrush=CBrush:
:
FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));//返回透明画刷句柄,并返回给指针
dc.SelectObject(pBrush);//同样,将画刷选到设备描述表当中
//用户选择不同的类型,将触发响应的事件
switch(m_nDrawType)
{
case1:
dc.SetPixel(point,RGB(53,183,15));//画点
break;
case2:
dc.MoveTo(m_ptOrigin);//画直线,先移动到原点
dc.LineTo(point);//再划线到终点
break;
case3:
dc.Rectangle(CRect(m_ptOrigin,point));//画矩形(两个重载的方法)
break;
case4:
dc.Ellipse(CRect(m_ptOrigin,point));//画椭圆(同上)
break;
}
CGraph*pGraph=newCGraph(m_nDrawType,m_ptOrigin,point);//new一个对象(类型,起点,终点),保证窗口不会相互干扰
CGraphicDoc*pDoc=GetDocument();
pDoc->m_obArray.Add(pGraph);
CView:
:
OnLButtonUp(nFlags,point);
}
--------------------Configuration:
Graphic-Win32Debug--------------------
Compiling...
GraphicView.cpp
Linking...
Creatingbrowseinfofile...
Graphic.exe-0error(s),0warning(s)
执行连接:
测试结果,如图:
4.字符串操作
源代码:
voidCViewerView:
:
OnButtonStat()//单击统计工具按钮,响应事件
{
//TODO:
Addyourcommandhandlercodehere
CViewerDoc*pDoc=GetDocument();//让pDoc指向文本内容
intnLines=pDoc->m_strContent.GetSize();//获得字符内容的大小
if(nLines<1)return;
CStringstrline;
charch;
boolchBOOL=true;//说明字符是单词首字符
longnum_char=0,num_C=0,num_E=0;
for(inti=0;i{
strline=pDoc->m_strContent.GetAt(i);
for(intj=0;j{
num_char=num_char+1;
ch=strline.GetAt(j);
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))//如果该字符为字母
{
if(chBOOL)num_E=num_E+1;
chBOOL=false;
}
elsechBOOL=true;
if(ch<0)//如果为中文字
{
num_C=num_C+1;
j++;
}
}
}
//将判断的结果对号入座
CDLGStatedlg;
dlg.m_char=num_char;
dlg.m_numch=num_C;
dlg.m_numen=num_E;
dlg.DoModal();
}
--------------------Configuration:
Viewer-Win32Debug--------------------
Linking...
Creatingbrowseinfofile...
Viewer.exe-0error(s),0warning(s)
执行编译:
测试结果,如图:
5.动态链接库
首先,设计对话框:
代码如下:
//Win32dll.cpp:
ImplementationofDLLExports.
BOOLWINAPIDllMain(HINSTANCEhInstance,DWORDdwReason,LPVOID/*lpReserved*/)
{
if(dwReason==DLL_PROCESS_ATTACH)
{
_Module.Init(ObjectMap,hInstance,&LIBID_WIN32DLLLib);
DisableThreadLibraryCalls(hInstance);
}
elseif(dwReason==DLL_PROCESS_DETACH)
_Module.Term();
returnTRUE;//ok
}
extern"C"_declspec(dllexport)intSum(inti)//在每个要导出函数的前面都加上标识符,以后都是从.lib中被导入的
{
intm=0;
for(intj=0;j<=i;j++)
m+=j;
returnm;
}
//RegularDll.cpp:
DefinestheinitializationroutinesfortheDLL.
CRegularDllApptheApp;
extern"C"_declspec(dllexport)intDisplay(inti)
{
MessageBox(NULL,"计算","从dll中调用",MB_OK);
intm=1;
for(intj=2;j<=i;j++)
m*=j;
returnm;
}
//testdllDlg.cpp:
implementationfile
extern"C"_declspec(dllexport)intDisplay(inti);
extern"C"_declspec(dllexport)intSum(inti);
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
enum{IDD=IDD_ABOUTBOX};
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
protected:
DECLARE_MESSAGE_MAP()
};
voidCAboutDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataE