C++WinWord格式.docx
- 文档编号:19703794
- 上传时间:2023-01-09
- 格式:DOCX
- 页数:24
- 大小:431.87KB
C++WinWord格式.docx
《C++WinWord格式.docx》由会员分享,可在线阅读,更多相关《C++WinWord格式.docx(24页珍藏版)》请在冰豆网上搜索。
双击view
在.h头文件添加:
//Attributes
public:
CMyMFCDoc*GetDocument();
CStrings1,s2;
CRectr;
//字体和颜色
COLORREFcol;
LOGFONTlf;
//Operations
双击:
在构造函数添加代码:
CMytextView:
CMytextView()
s1="
s2="
我的内容。
r.left=50;
r.top=50;
r.right=700;
r.bottom=700;
voidCMytextView:
OnDraw(CDC*pDC)
CMytextDoc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
inti=1;
for(i=1;
i<
=12;
i++)
{
lf.lfEscapement=300*i;
lf.lfWeight=800;
lf.lfUnderline=0;
lf.lfStrikeOut=0;
lf.lfHeight=14;
lf.lfWidth=14;
strcpy(lf.lfFaceName,"
宋体"
);
CFontf;
f.CreateFontIndirect(&
lf);
pDC->
SelectObject(&
f);
TextOut(200,330,s2);
SetTextCharacterExtra(16);
}
在菜单添加两个选项:
将ID改为ID_FONT和ID_COLOR,然后添加方法。
在MytextView.cpp中添加
OnColor()
CColorDialogdlg(col);
if(dlg.DoModal()!
=IDOK)return;
col=dlg.GetColor();
Invalidate();
OnFont()
CFontDialogdlg(&
dlg.m_cf.rgbColors=col;
col=dlg.m_cf.rgbColors;
dlg.GetCurrentFont(&
voidCMyMFCView:
OnDraw(CDC*pDC)添加(On_Draw的最上方)
pDC->
SetTextColor(col);
f.CreateFontIndirect(&
SelectObject(f);
CStringstr=s1;
str.Format(s1);
DrawText(str,r,DT_CENTER|DT_NOPREFIX|DT_WORDBREAK);
添加键盘移动方法:
OnChar(UINTnChar,UINTnRepCnt,UINTnFlags)
switch(nChar)
case'
\b'
s1.Delete(s1.GetLength()-1);
break;
//backspace
\t'
s1+="
"
//tab
\n'
case'
\r'
//carriagereturn
\x1B'
break;
//escape
default:
s1+=nChar;
CView:
OnChar(nChar,nRepCnt,nFlags);
OnKeyDown(UINTnChar,UINTnRepCnt,UINTnFlags)
switch(nChar)
caseVK_UP:
r.top-=15;
r.bottom-=15;
caseVK_DOWN:
r.top+=15;
r.bottom+=15;
caseVK_LEFT:
r.left-=15;
r.right-=15;
caseVK_RIGHT:
r.left+=15;
r.right+=15;
OnKeyDown(nChar,nRepCnt,nFlags);
3.用线性表画,直线,圆,矩形,五角星并记录。
导入线性表:
先把文件拷到项目的文件夹下。
sqList2.h
修改一下线性表:
最上方添加:
classMyPoint
intx,y,drawstate;
//MyPoint(intx0=0,inty0=0,drawstate0=1):
x(x0),y(y0),drawstate(drawstate0){}
intoperator!
=(MyPointob)
{/*boolresult=false;
if(x==ob.x&
&
y==ob.y&
drawstate==ob.drawstate)result=true;
returnresult;
//重载!
=*/
returndrawstate!
=ob.drawstate;
};
找到
ElemType&
GetElem(inti)//取第i个元素值,i=1~n
{if(i<
1||i>
Length)returnX;
//返回随机数
returndata[i-1];
//ElemType&
operator[](inti){returnGetElem(i);
}//下标重载将此行注解掉
/**/
找到将以下注解掉
voidDispList(charc='
'
charc2='
)//输出线性表中每个元素值
{/*inti;
if(Length==0){cout<
<
空表"
if(c2)if(c2=='
)cout<
endl;
elsecout<
c2;
return;
for(i=0;
Length;
i++){cout<
data[i];
if(c)cout<
c;
*/
在菜单添加
分别叫ID_LINE,ID_CIRCLE,ID_RECTANGLE,ID_FIVESTAR
在MyDrawView.h中添加引用
#include"
sqList2.h"
//顺序表的头文件
然后
定义变量:
CMyDrawDoc*GetDocument();
intdrawstate;
CPointp1,p2;
sqList<
MyPoint>
d;
//中空画刷
LOGBRUSHlpb;
CBrushb;
MyDrawView.cpp中
构造函数初始化
drawstate=0;
p1.x=0;
p1.y=0;
p2.x=0;
p2.y=0;
lpb.lbStyle=BS_NULL;
b.CreateBrushIndirect(&
lpb);
建立类向导:
添加
(刚才的四种,直线,圆,五角星,矩形)
voidCMyDrawView:
OnCircle()
{drawstate=2;
OnFivestar()
{drawstate=4;
OnLine()
{drawstate=1;
OnRectangle()
{drawstate=3;
添加右键菜单:
OnRButtonDown(UINTnFlags,CPointpoint)
CMenum;
m.CreatePopupMenu();
m.AppendMenu(0,ID_LINE,"
直线"
m.AppendMenu(MF_SEPARATOR,0,"
//分隔线
m.AppendMenu(0,ID_CIRCLE,"
椭圆"
m.AppendMenu(0,ID_RECTANGLE,"
矩形"
m.AppendMenu(0,ID_FIVESTAR,"
五角星"
CPointp;
GetCursorPos(&
p);
m.TrackPopupMenu(TPM_RIGHTBUTTON,p.x,p.y,this);
OnRButtonDown(nFlags,point);
}
MyDrawView.cpp头部引入数学头文件,定义PI
math.h"
#definePI3.1415
添加画五角星的方法:
必须在调用之前也就是On_Draw之前
voidwjx(CDC*&
pDC,intx0=300,inty0=300,intr=200)
doublex1,y1,x2,y2,rr;
inti;
CPointp[11];
rr=r*cos(PI/180*72)/cos(PI/180*36);
for(i=0;
=10;
if(i%2==1)
{
p[i].x=x0+rr*cos(PI/2+PI/180*36*i);
p[i].y=y0-rr*sin(PI/2+PI/180*36*i);
}
else
p[i].x=x0+r*cos(PI/2+PI/180*36*i);
p[i].y=y0-r*sin(PI/2+PI/180*36*i);
Polygon(p,11);
On_Draw方法:
CMyDrawDoc*pDoc=GetDocument();
inti,n=d.GetLen();
SelectObject(b);
=n;
i+=2)
//画以前的
if(d[i].drawstate==1)
pDC->
MoveTo(d[i].x,d[i].y);
LineTo(d[i+1].x,d[i+1].y);
elseif(d[i].drawstate==2)
Ellipse(d[i].x,d[i].y,d[i+1].x,d[i+1].y);
elseif(d[i].drawstate==3)
Rectangle(d[i].x,d[i].y,d[i+1].x,d[i+1].y);
elseif(d[i].drawstate==4)
intr=sqrt((d[i].x-d[i+1].x)*(d[i].x-d[i+1].x)+(d[i].y-d[i+1].y)*(d[i].y-d[i+1].y));
wjx(pDC,d[i].x,d[i].y,r);
//画当前的
if(drawstate==1)
{
MoveTo(p1.x,p1.y);
LineTo(p2.x,p2.y);
elseif(drawstate==2)
Ellipse(p1.x,p1.y,p2.x,p2.y);
elseif(drawstate==3)
Rectangle(p1.x,p1.y,p2.x,p2.y);
elseif(drawstate==4)
intr=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
wjx(pDC,p1.x,p1.y,r);
添加左键按下效果,左键抬起效果,鼠标经过效果。
OnLButtonDown(UINTnFlags,CPointpoint)
p1=point;
OnLButtonDown(nFlags,point);
OnLButtonUp(UINTnFlags,CPointpoint)
intn=d.GetLen();
MyPointt1,t2;
t1.x=p1.x;
t1.y=p1.y;
t1.drawstate=this->
drawstate;
t2.x=p2.x;
t2.y=p2.y;
t2.drawstate=this->
d.InsElem(++n,t1);
d.InsElem(++n,t2);
OnLButtonUp(nFlags,point);
OnMouseMove(UINTnFlags,CPointpoint)
if(nFlags==MK_LBUTTON)
p2=point;
this->
Invalidate();
OnMouseMove(nFlags,point);
4.工作者线程,进度条随机关闭。
这里先运行一下。
创建新的对话框,添加进度条,样式设为平滑。
添加进度条变量;
名字为m_Progress1
双击“确定”,“取消”添加ok,cancel方法。
Mydlg.h添加变量
intn;
Mydlg.cpp添加
CMydlg:
CMydlg(CWnd*pParent/*=NULL*/)
:
CDialog(CMydlg:
IDD,pParent)
n=0;
//初始化计数器
//{{AFX_DATA_INIT(CMydlg)
//NOTE:
theClassWizardwilladdmemberinitializationhere
//}}AFX_DATA_INIT}
voidCMydlg:
chars[100];
//
GetWindowText(s,100);
//"
第001个对话框"
//
intn;
sscanf(s,"
第%03d个对话框"
&
n);
HWNDhwnd=:
FindWindow(NULL,"
Muti"
//寻找窗体,工程名!
!
SendMessage(hwnd,9999,(WPARAM)&
n,0);
OnCancel()
sscanf(s,"
OnCancel();
建立类向导,添加timer,以及InitDialog方法
//}}AFX_DATA_INIT
OnTimer(UINTnIDEvent)
CDC*cDC=GetDC();
CStrings;
s.Format("
%02d"
n++);
m_Progress1.SetPos(n%100);
cDC->
TextOut(200,20,s);
OnTimer(nIDEvent);
BOOLCMydlg:
OnInitDialog()
OnInitDialog();
SetTimer(1,100,NULL);
m_Progress1.SetRange(0,100);
returnTRUE;
//returnTRUEunlessyousetthefocustoacontrol
//EXCEPTION:
OCXPropertyPagesshouldreturnFALSE
添加线程类
MyThread.cpp添加头文件
Mydlg.h"
//调用mydlg
BOOLCMyThread:
InitInstance()
CMydlg*p=newCMydlg;
p->
Create(IDD_DIALOG1);
ShowWindow(SW_SHOW);
//动态创建对话框
MutiDlg.h添加变量
//Construction
CMutiDlg(CWnd*pParent=NULL);
//standardconstructor
CWinThread*pThread[100];
intcount;
MutiDlg.cpp添加头文件#include"
MyThread.h"
双击找到构造函数count=0;
MutiDlg.h添加
protected:
HICONm_hIcon;
//Generatedmessagemapfunctions
//{{AFX_MSG(CMutiDlg)
virtualBOOLOnInitDialog();
afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);
afx_msgvoidOnPaint();
afx_msgHCURSOROnQueryDragIcon();
afx_msgvoidOnButton1();
afx_msgvoidOnButton2();
//}}AFX_MSG
afx_msgintOnMyMsg(WPARAMw,LPARAMl);
//自定义消息
DECLARE_MESSAGE_MAP()
MutiDlg.cpp添加
BEGIN_MESSAGE_MAP(CMutiDlg,CDialog)
//{{AFX_MSG_MAP(CMutiDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1,OnButton1)
ON_BN_CLICKED(IDC_BUTTON2,OnButton2)
ON_MESSAGE(9999,OnMyMsg)//消息映射
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
intCMutiDlg:
OnMyMsg(WPARAMw,LPARAMl)
int*p=(int*)w;
%d"
*p);
//AfxMessageBox(s);
pThread[*p-1]=NULL;
return0;
voidCMutiDlg:
OnButton1()
inti;
count;
if(pThread[i]==NULL)
break;
pThread[i]=AfxBeginThread(RUNTIME_CLASS(CMyThre
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Win
![提示](https://static.bdocx.com/images/bang_tan.gif)