VS简明教程.pdf
《VS简明教程.pdf》由会员分享,可在线阅读,更多相关《VS简明教程.pdf(48页珍藏版)》请在冰点文库上搜索。
伯兮科技第1页共48页VS2008简明教程简明教程version1.0写在前面:
.1第一章:
启动软件.2第二章:
HelloWorld.22.1:
使用BASIC构建HelloWorld的控制台程序.22.2:
使用BASIC创建窗体应用程序helloworld.92.3:
使用C#创建控制台程序helloworld.142.4:
使用C#创建窗口应用程序helloworld.172.5:
使用C+的CLR创建控制台程序helloworld.202.6:
使用C+创建Win32控制台程序helloworld.222.7:
使用C+创建CLR窗体程序helloworld.262.8:
使用C+创建win32控制台程序helloworld.30第三章:
简单的串口程序.343.1:
串口控制台程序.34串口控制台程序详细步骤解析:
.35构建VC+控制台串口程序.35VISUALBASIC控制台串口程序.41写在前面:
写在前面:
程序设计,经过了机器语言-汇编语言-面向过程的程序设计-面向对象的程序设计-面向托管的程序设计-未来。
比尔盖茨创建微软公司,以BASIC起家,开发推广了MSDOS,Windows系列操作系统,和形式各样的软件。
包括今天的专题VS2008开发软件。
Windows平台(无论是windowsxp,还是windows7)已经成为每个人进入信息社会的必经的平台,包括微软公司在内的诸多软件公司,开发了各种各样的软件(娱乐,休闲,工作,行业等等软件)。
学习和使用VS2008,其一是为了开发属于自己的应用或软件;其二是为了谋取公司的某个职位,做企业的应用和开发,或者进入软件巨头的公司,开发和完善我们耳熟能详的专业软件(比如:
Photoshop等)本简明教程,只作为一个通俗入门读物。
更深层研究开发需要研读MSDN,算法,应用数学等相关知识。
选择大学的计算机专业,可以系统的学习相关专业知识。
自学可以选择麻省理工(MIT)的开放课程进行学习。
当然,后者不会有证书作为敲门砖。
作为一名学习者VS2008的来源是个问题,庆幸的是微软公司提供了试用版和体验版,可以到微软的官方网站(http:
/)查询下载。
盗版软件,最好不要使用,至少不要用于商业开发。
因为使用盗版软件本身就是损害软件从业人员的福利和前途,也损害者您自己的前途和福利。
尽管,目前很多小型软件公司依然使用盗版软件做软件开发。
伯兮科技第2页共48页第一章:
启动软件第一章:
启动软件上如图,是安装VS2008后,开始菜单中的程序项。
启动软件,VS2008界面第二章:
第二章:
HelloWorldHelloWorld是最简单的入门程序。
本章,将演示多个HelloWorld程序。
1,使用Basic构建2,使用VC+构建3,使用C#构建2.1:
使用:
使用BASIC构建构建HelloWorld的控制台程序的控制台程序伯兮科技第3页共48页以下是自动生成的代码,不做任何更改:
ModuleModule1SubMain()EndSub伯兮科技第4页共48页EndModule启动调试后,程序显示一下什么都不做!
或者“Ctrl+F5”按住Ctrl键,不松手,在按下F5伯兮科技第5页共48页修改代码如下:
ModuleModule1SubMain()Console.Write(helloworld)EndSubEndModule编译如下图单击或者(Ctrl+Alt+F7)执行,如下图单击或者(Ctrl+F5)显示执行结果,在控制台中显示helloworld回顾一下修改代码的过程:
VS2008会提供提示功能。
伯兮科技第6页共48页伯兮科技第7页共48页上图可见,Console.Write有17种参数形式,根据自己的需要可以探索更多的实现方式和方法。
对生成的文件和文件夹的探索伯兮科技第8页共48页用记事本打开VS2008生成的文件MicrosoftVisualStudioSolutionFile,FormatVersion10.00#VisualStudio2008Project(F184B08F-C81C-45F6-A57F-5ABD9991F28F)=helloworld1,helloworld1helloworld1.vbproj,5BE89406-4C02-4332-959B-8003B9A57C6FEndProjectGlobalGlobalSection(SolutionConfigurationPlatforms)=preSolutionDebug|AnyCPU=Debug|AnyCPU伯兮科技第9页共48页Release|AnyCPU=Release|AnyCPUEndGlobalSectionGlobalSection(ProjectConfigurationPlatforms)=postSolution5BE89406-4C02-4332-959B-8003B9A57C6F.Debug|AnyCPU.ActiveCfg=Debug|AnyCPU5BE89406-4C02-4332-959B-8003B9A57C6F.Debug|AnyCPU.Build.0=Debug|AnyCPU5BE89406-4C02-4332-959B-8003B9A57C6F.Release|AnyCPU.ActiveCfg=Release|AnyCPU5BE89406-4C02-4332-959B-8003B9A57C6F.Release|AnyCPU.Build.0=Release|AnyCPUEndGlobalSectionGlobalSection(SolutionProperties)=preSolutionHideSolutionNode=FALSEEndGlobalSectionEndGlobal限于篇幅限制更多的文件探索,根据个人兴趣进行不同深度的探索。
探索的目的:
了解VS2008生成文件的组织架构,但并不意味着要打破砂锅问到底,而是,进入一定深度的了解。
保持操作黑箱的方法和态度是能够快速学习的基本条件,如果,时间充裕,打破一定数量的黑箱,能让我们更加了解机理。
2.2:
使用:
使用BASIC创建窗体应用程序创建窗体应用程序helloworld伯兮科技第10页共48页增加button按钮,并双击该按钮添加代码添加代码前:
PublicClassForm1伯兮科技第11页共48页PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.ClickEndSubEndClass添加代码后:
PublicClassForm1PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.ClickButton1.Text=helloworldEndSubEndClass编译Ctrl+Alt+F7执行Ctrl+F5伯兮科技第12页共48页单击“Button1”扩展应用:
改变颜色,隐藏Button1.Hide()隐藏按钮编译Ctrl+Alt+F7伯兮科技第13页共48页执行Ctrl+F5单击“Button1”伯兮科技第14页共48页更多地功能,读者可以自行探索和实验,以备以后应用。
2.3:
使用:
使用C#创建控制台程序创建控制台程序helloworld伯兮科技第15页共48页usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacehelloworld3classProgramstaticvoidMain(stringargs)添加代码如下:
伯兮科技第16页共48页usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacehelloworld3classProgramstaticvoidMain(stringargs)Console.Write(helloworld);编译(Ctrl+Alt+F7)并执行(Ctrl+F5)伯兮科技第17页共48页2.4:
使用:
使用C#创建窗口应用程序创建窗口应用程序helloworld添加“Button”按钮,并且双击该按钮,编辑代码伯兮科技第18页共48页usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacehelloworld4publicpartialclassForm1:
FormpublicForm1()InitializeComponent();privatevoidbutton1_Click(objectsender,EventArgse)伯兮科技第19页共48页usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacehelloworld4publicpartialclassForm1:
FormpublicForm1()InitializeComponent();privatevoidbutton1_Click(objectsender,EventArgse)button1.Text=helloworld;伯兮科技第20页共48页编译(Ctrl+Alt+F7)并执行(Ctrl+F5),并单击Button12.5:
使用:
使用C+的的CLR创建控制台程序创建控制台程序helloworld什么是CLR:
CLR是微软的通用语言运行时,可以与java的虚拟机VM类比。
无论是C+,BASIC,C#都可以运行在CLR上,CLR与硬件无关,可以理解为操作系统的一种封装形式。
伯兮科技第21页共48页已经有helloworld代码了无需更改。
编译(Ctrl+Alt+F7)并执行(Ctrl+F5)伯兮科技第22页共48页2.6:
使用:
使用C+创建创建Win32控制台程序控制台程序helloworld伯兮科技第23页共48页伯兮科技第24页共48页#includestdafx.h伯兮科技第25页共48页int_tmain(intargc,_TCHAR*argv)return0;/helloworld6.cpp:
定义控制台应用程序的入口点。
/#includestdafx.hint_tmain(intargc,_TCHAR*argv)printf(helloworld);return0;编译(Ctrl+Alt+F7)并执行(Ctrl+F5)伯兮科技第26页共48页2.7:
使用:
使用C+创建创建CLR窗体程序窗体程序helloworld伯兮科技第27页共48页添加“Button”按钮,并双击,查看代码:
伯兮科技第28页共48页#pragmaoncenamespacehelloworld7usingnamespaceSystem;usingnamespaceSystem:
ComponentModel;usingnamespaceSystem:
Collections;usingnamespaceSystem:
Windows:
Forms;usingnamespaceSystem:
Data;usingnamespaceSystem:
Drawing;/Form1摘要/警告:
如果更改此类的名称,则需要更改/与此类所依赖的所有.resx文件关联的托管资源编译器工具的/“资源文件名”属性。
否则,/设计器将不能与此窗体的关联/本地化资源正确交互。
/publicrefclassForm1:
publicSystem:
Windows:
Forms:
Formpublic:
Form1(void)InitializeComponent();/TODO:
在此处添加构造函数代码/protected:
/清理所有正在使用的资源。
/Form1()if(components)deletecomponents;伯兮科技第29页共48页private:
System:
Windows:
Forms:
Buttonbutton1;protected:
private:
/必需的设计器变量。
/System:
ComponentModel:
Containercomponents;#pragmaregionWindowsFormDesignergeneratedcode/设计器支持所需的方法-不要/使用代码编辑器修改此方法的内容。
/voidInitializeComponent(void)this-button1=(gcnewSystem:
Windows:
Forms:
Button();this-SuspendLayout();/button1/this-button1-Location=System:
Drawing:
Point(57,62);this-button1-Name=Lbutton1;this-button1-Size=System:
Drawing:
Size(136,58);this-button1-TabIndex=0;this-button1-Text=Lbutton1;this-button1-UseVisualStyleBackColor=true;this-button1-Click+=gcnewSystem:
EventHandler(this,&Form1:
button1_Click);/Form1/this-AutoScaleDimensions=System:
Drawing:
SizeF(6,12);this-AutoScaleMode=System:
Windows:
Forms:
AutoScaleMode:
Font;this-ClientSize=System:
Drawing:
Size(292,266);this-Controls-Add(this-button1);this-Name=LForm1;this-Text=LForm1;this-ResumeLayout(false);#pragmaendregionprivate:
System:
Voidbutton1_Click(System:
Objectsender,System:
EventArgse)伯兮科技第30页共48页;添加代码:
编译(Ctrl+Alt+F7)并执行(Ctrl+F5)并单击“Button1”2.8:
使用:
使用C+创建创建win32控制台程序控制台程序helloworld伯兮科技第31页共48页伯兮科技第32页共48页伯兮科技第33页共48页编译并执行使用Win32创建窗体程序,比较复杂,需要创建类,变量,并且作相关的链接。
这里不做详细表述。
相关技术可参考。
伯兮科技第34页共48页第三章:
简单的串口程序第三章:
简单的串口程序3.1:
串口控制台程序:
串口控制台程序程序列表:
/VS2008VC+控制台控制台串口串口#includestdafx.husingnamespaceSystem;intmain(arrayargs)System:
IO:
Ports:
SerialPortaa;aa.BaudRate=2400;aa.Open();aa.Write(hello);Console:
WriteLine(LHelloWorld);return0;/VS2008BASIC控制台控制台串口串口ModuleModule1DimaAsNewSystem.IO.Ports.SerialPortSubMain()a.BaudRate=2400a.Open()a.Write(hello,boxitech)EndSubEndModule/VS2008CS控制台控制台串口串口usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacecsconuartclassProgramstaticvoidMain(stringargs)伯兮科技第35页共48页System.IO.Ports.SerialPortaa=newSystem.IO.Ports.SerialPort();aa.BaudRate=4800;aa.Open();aa.Write(hellocsuart);串口控制台程序详细步骤解析:
串口控制台程序详细步骤解析:
构建构建VC+控制台串口程序控制台串口程序/uart01.cpp:
主项目文件。
#includestdafx.husingnamespaceSystem;伯兮科技第36页共48页intmain(arrayargs)Console:
WriteLine(LHelloWorld);return0;增加串口控制代码:
System:
IO:
Ports:
SerialPortaa;/定义一个串口对象的一个实例aaaa.Open();/调用串口对象aa的方法open,用来打开串口aa.Write(“hello”);/调用串口对象aa的方法Write,用户通过串口发送字符串hello使用AccessPort对串口进行监控,观察设计的程序的运行结果。
伯兮科技第37页共48页编译(Ctrl+Alt+F7)并执行(Ctrl+F5)程序观察串口监控的结果:
伯兮科技第38页共48页或者Ctrl+H上图,串口波特率是9600修改串口对象的参数:
波特率4800伯兮科技第39页共48页编译(Ctrl+Alt+F7)并执行(Ctrl+F5)程序观察串口监控的结果:
修改串口其他参数:
伯兮科技第40页共48页伯兮科技第41页共48页小知识:
类与对象小知识:
类与对象类,上面操作的串口,可以归纳综合为一类。
而我们所操作的一个串口,就是一个对象或实例。
所有的操作,称为方法;对象中被赋值或者引用的相关变量,成为属性。
串口的打开是一个方法;aa.Open();串口发送数据也是一个方法;aa.Write(“hello”);串口名称是一个属性;aa.PortName=“COM1”;串口的波特率也是一个属性:
aa.BaudRate=4800;VISUALBASIC控制台串口程序控制台串口程序伯兮科技第42页共48页编译并执行监控串口结果伯兮科技第43页共48页更多地参数:
属性:
伯兮科技第44页共48页方法:
伯兮科技第45页共48页伯兮科技第46页共48页多线程编程usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;namespaceConsoleApplication2publicclassa1publicvoida11()while(true)Console.WriteLine(a11aa);publicclassa2publicvoida22()while(true)Console.WriteLine(a22);classProgramstaticvoidMain(stringargs)a1mya1=newa1();a2mya2=newa2();Threadomya1=newThread(newThreadStart(mya1.a11);伯兮科技第47页共48页Threadomya2=newThread(newThreadStart(mya2.a22);omya1.Start();omya2.Start();SOCKET程序服务器端privatevoidbutton2_Click(objectsender,EventArgse)IPAddressmyipa1=IPAddress.Parse(127.0.0.1);IPEndPointmyipe1=newIPEndPoint(myipa1,2000);Socketd=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);d.Bind(myipe1);d.Listen(0);Sockete11=d.Accept();stringrecstr=;byterecbytes=newbyte1024;intbytes;bytes=e11.Receive(recbytes,recbytes.Length,0);recstr+=Encoding.ASCII.GetString(recbytes,0,bytes);label1.Text=recstr;客户端privatevoidbutton1_Click(objectsender,EventArgse)IPAddressmyipa=IPAddress.Parse(127.0.0.1);IPEndPointmyipe=newIPEndPoint(myipa,2000);Socketc=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);c.Connect(myipe);伯兮科技第48页共48页stringsendstring=helloworld;bytebs=Encoding.ASCII.GetBytes(sendstring);c.Send(bs,bs.Length,0);