文本編輯器自己_第1頁
文本編輯器自己_第2頁
文本編輯器自己_第3頁
文本編輯器自己_第4頁
文本編輯器自己_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

《文本編輯器》實驗報告實驗題目:文本編輯器班級:軟件工程專業(yè)(1)班11092711姓名: 指導老師:設計日期:2012年12月30日1、 實驗目的:綜合運行各種控件,進一步熟悉可視化編程方式,特別是熟悉使用Windows的編程環(huán)境和風格綜合運用C#.NET提供的標準控件,如:菜單控件、公共對話框控件、RichTextBox控件、ToolStrip控件制作小型應用系統(tǒng)。2、 實驗內容:參考系統(tǒng)提供的寫字板功能,編寫一個小型的文字編輯工具。對該文檔編輯器,至少完成以下功能:文件:(新建、打開、保存、退出、保存、另存為、打印、打印預覽)編輯:(復制、剪切、粘貼、撤銷、重做、)界面設計時應有工具欄,彈出菜單,狀態(tài)欄等功能來提高操作的便利性;過程與實施文件-〉新建->項目-〉WPF應用程序(1)將Grid布局面板改為DockPanel布局面板。代碼如下:<DockPanelLastChildFill=〃True〃Height=〃314〃></DockPanel>(2) 插入菜單工具條,代碼如下:<MenuName=〃menu1〃Height=〃26〃Width=〃503〃DockPanel.Dock=〃Top〃Focusable=〃True〃VerticalAlignment=〃Top〃></Menu>(3) 在菜單工具欄中插入文件(_F)編輯(_E)選項。代碼如下:<MenuItemHeader="文件(_F)〃Name=“文件"〉<MenuItemHeader="編輯(_E)〃Name=〃編輯">(4) 在文件選項下插入新建、分隔線跟退出,并且給新建跟退出增加單機函數(shù),為快捷鍵增加事件函數(shù),代碼如下:<MenuItemClick=〃MenuItem_Click"Header=新建(_N)"InputGestureText="Ctrl+N"/>〈Separator/><MenuItemHeader="M出(_X)〃Click=〃Exit_MenuItem_Click〃InputGestureText="CtrlX"/>privatevoidMenuItem_Click(objectsender,RoutedEventArgse){杭州電子科技大學一一軟件工程專業(yè)一一C#實驗報告richTextBoxl.Document.Blocks.Clear()}privatevoidExit_MenuItem_Click(objectsender,RoutedEventArgse){Application.Current.Shutdown();}privatevoidWindow_KeyDown(objectsender,RoutedEventArgse){if((Keyboard.GetKeyStates(Key.LeftCtrl)&KeyStates.Down)>0){if((Keyboard.GetKeyStates(Key.N)&KeyStates.Down)>0)MenuItem_Click(sender,e);得到如圖所示界面:3.(5)在編輯選項下增加如下功能選頊<MenuItemHeader="(撤銷_Z)〃Command=〃Undo〃InputGestureText="Ctrl+Z"/><MenuItemHeader="全選_A)"Command="SelectAll"InputGestureText="Ctrl+A"/><Separator/><MenuItemHeader="|^切_X)"Command="Cut"InputGestureText="Ctrl+X"/><MenuItemHeader="復制—C)"Command="Copy"InputGestureText="Ctrl+C"/><MenuItemHeader="粘貼_V)"Command="Paste"InputGestureText="Ctrl+V"/>得到如下界面:

4.接下來在菜單欄下面插入工具欄toolbar<ToolBarHeight=〃30"Name=〃toolBar1"Width="500"DockPanel.Dock="Top”VerticalAlignment=〃Top〃>5.在拖入的工具欄內增加Button18個,然后在XMAL內增加代碼,并且直接改command屬性使其擁有所需的效果(在引用中增加一個文件夾并且加入所需要的圖片):<ButtonHeight="30"Name="button1"Width="30"Command="ApplicationCommands.Cut”ToolTip」剪貼"><Button.Background><ImageBrushImageSource="/文本器;component/Images/editcut.png"/></Button.Background></Button><Button Height="30" Name="button2”Command="ApplicationCommands.Copy"ToolTip="復制"><Button.Background><ImageBrushImageSource="/文本器;component/Images/editcopy.png"/></Button.Background></Button><Button Height="30" Name="button3"Command="ApplicationCommands.Paste"ToolTip='粘貼"><Button.Background><ImageBrushImageSource="/文本器;component/Images/editpaste.png"/></Button.Background></Button><Button Height="30" Name="button4"Command="ApplicationCommands.Redo"ToolTip」恢復"><Button.Background><ImageBrushImageSource="/文本器;component/Images/editredo.png"/>編 輯Width="30"編 輯Width="30"編 輯Width="30”編 輯Width="30”編 輯</Button><ButtonHeight="30"Name="button5"Width="30"ToolTip="撤銷Command="ApplicationCommands.Undo"><Button.Background><ImageBrush ImageSource="/ 文本編輯器;component/Images/editundo.png"/></Button.Background></Button><Button Height="30" Name="button6" Width="30”Command="EditingCommands.ToggleBold"ToolTip='加粗"><TextBlockFontWeight="Bold">B</TextBlock></Button><Button Height="30" Name="button7" Width="30"Command="EditingCommands.ToggleItalic"ToolTip='斜傾"><TextBlockFontStyle="Italic"FontWeight="Bold">I</TextBlock></Button><Button Height="30" Name="button8" Width="30"Command="EditingCommands.ToggleUnderline"ToolTip」下劃線"><TextBlock TextDecorations="Underline"FontWeight="Bold">U</TextBlock></Button><Button Height="30" Name="button9" Width="30"Command="EditingCommands.IncreaseFontSize"ToolTip="字符加寬"><Button.Background><ImageBrushImageSource="/文本編輯器;component/Images/charactergrowfont.png"/></Button.Background></Button><Button Height="30" Name="button10" Width="30"Command="EditingCommands.DecreaseFontSize"ToolTip="字符變窄"〉<Button.Background><ImageBrushImageSource="/文本編輯器;component/Images/charactershrinkfont.png"/></Button.Background></Button><Button Height="30" Name="button11" Width="30"Command="EditingCommands.ToggleBullets"ToolTip='項目符號"><Button.Background><ImageBrushImageSource="/文本編輯器;component/Images/listbullets.png"/></Button.Background></Button><Button Height="30" Name="button12" Width="30"Command="EditingCommands.ToggleNumbering"ToolTip='編號"><Button.Background><ImageBrush ImageSource="/ 文本編輯器;component/Images/listnumbering.png"/>

</Button.Background></Button><Button Height="30" Name="button13”Command="EditingCommands.AlignLeft"ToolTip="左對齊"><Button.Background><ImageBrush ImageSource="/ 文本器;component/Images/paragraphleftjustify.png"/></Button.Background></Button><Button Height="30" Name="button14"Command="EditingCommands.AlignCenter"ToolTip="中間對齊"><Button.Background><ImageBrush ImageSource="/ 文本器;component/Images/paragraphcenterjustify.png"/></Button.Background></Button><Button Height="30" Name="button15"Command="EditingCommands.AlignRight"ToolTip="右對齊"〉<Button.Background><ImageBrush ImageSource="/ 文本器;component/Images/paragraphrightjustify.png"/></Button.Background></Button><Button Height="30" Name="button16"Command="EditingCommands.AlignJustify"ToolTip="兩邊對齊"><Button.Background><ImageBrush ImageSource="/ 文本器;component/Images/paragraphfulljustify.png"/></Button.Background></Button><Button Height="30" Name="button17"Command="EditingCommands.IncreaseIndentation"ToolTip」增加縮進量"><Button.Background><ImageBrush ImageSource="/ 文本器;component/Images/paragraphincreaseindentation.png"/></Button.Background></Button><Button Height="30" Name="button18"Command="EditingCommands.DecreaseIndentation"ToolTip="減少縮進量"〉<Button.Background><ImageBrush ImageSource="/ 文本Width="30"編 輯Width="30"編 輯Width="30”編 輯Width="30”Width="30"編 輯Width="30"編 輯Width="30”編 輯Width="30”編 輯Width="30”編 輯Width="30”編 輯得到如圖所示效果:得到如圖所示效果:5.再在工具條上增加兩個combox下拉框,用來改變字體以及改變字體的大小函數(shù)為:(1).改變IsReadOnly=〃True〃的屬性,并且增加函數(shù)為:SelectionChanged="comboBox1_SelectionChangedprivatevoidcomboBox1_SelectionChanged(objectsender,SelectionChangedEventArgse){FontFamilyfontName=newFontFamily(comboBox1.SelectedItem.ToString());richTextBox1.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty,fontName);}privatevoidcomboBox2_SelectionChanged(objectsender,SelectionChangedEventArgse){ComboBoxItemCbItem=comboBox2.SelectedItemasComboBoxItem;doublefontSize=Convert.ToDouble(CbItem.Content.ToString());richTextBox1.Selection.ApplyPropertyValue(TextElement.FontSizeProperty,fontSize);}得到如圖所示效果:(在這個之前還需要在項目管理器中引用菜單里添加system?drawing項)6.About對話框(1)單擊“項目1添加窗體”菜單項,在模板中選擇“窗體”,以此建立一個新的窗體,在窗體中添加一個Lable和一個button,增加按鈕的事件處理函數(shù):privatevoidbutton1_Click(objectsender,RoutedEventArgse){Close();}⑵在頂級菜單項中增加“關于...”并且增加單擊處理函數(shù):privatevoidHelpAbout_MenuItem_Click(objectsender,RoutedEventArgse){Aboutd1=newAbout();d1.ShowDialog();}得到如圖所示效果7.在文件選項下插入查找和替換:(1)就像插入上述About對話框一樣,加入查找和替換對話框。并且設置為不能放大或者縮小然后加入兩個textbox,兩個button;然后增加事件處理函數(shù):publicpartialclassFindReplace:Window{Window1mainWindow;publicFindReplace(Window1window1){InitializeComponent();mainWindow=window1;}publicFindReplace(){InitializeComponent();}

privatevoidWindow_Loaded(objectsender,RoutedEventArgse){}RoutedEventArgse)elseprivatevoidbutton1_Click(objectsender,{RoutedEventArgse)elseif(textBox1.Text.Length!=0)mainWindow.FindRichTextBoxString(textBox1.Text);MessageBox.Show(privatevoidbutton2_Click(objectsender,RoutedEventArgse){if(textBox2.Text.Length!=0)mainWindow.ReplaceRichTextBoxString(textBox2.Text); elseMessageBox.Show }}}得到如圖所示效果:1Window!文件⑥幫勘:VA「鞘CtrkZ|■ w+-AA::Ctd+¥Ctrf+A磐切Ctri+X復制CtrkCCtri+V8.在文件菜單項中增加打印和打印預覽兩項,然后直接增加事件單擊處理函數(shù):privatevoidFilePrint_MenuItem_Click(objectsender,RoutedEventArgse){PrintDialogpd=newPrintDialog();if((pd.ShowDialog()==true)){//pd.PrintVisual(richTBasVisual,);//pd.PrintDocument((((IDocumentPaginatorSource)richTextBox1.Document).DocumentPaginator),”將richTB顯示內容作為DocumentPaginator對象打印");}}privatevoidFilePrintPreview_MenuItem_Click(objectsender,RoutedEventArgse){〃得到打印紙打印區(qū)域,打開PrintDialog對話框,選擇打印機、紙大小等參數(shù),賦給iaPrintDocumentImageableAreaia=null;XpsDocumentWriterdocWriter=PrintQueue.CreateXpsDocumentWriter(refia);PrintPreviewprintPreviewDlg=newPrintPreview();printPreviewDlg.Owner=this;//不能直接對richTextBoxl.Document處理,做一個拷貝,將拷貝顯示到DocumentviewerTextRange sourceDocument = newTextRange(richTextBox1.Document.ContentStart,richTextBoxl.Document.ContentEnd);MemoryStreamstream=newMemoryStream();sourceDocument.Save(stream,DataFormats.Xaml);FlowDocumentflowDocumentCopy=newFlowDocument();TextRange copyDocumentRange = newTextRange(flowDocumentCopy.ContentStart,flowDocumentCopy.ContentEnd);copyDocumentRange.Load(stream,DataFormats.Xaml);if(ia!=null){IDocumentPaginatorSourcetext=flowDocumentCopyasIDocumentPaginatorSource;//對拷貝處理MemoryStreamms=newMemoryStream();Packagepkg=Package.Open(ms,FileMode.Create,FileAccess.ReadWrite);stringpack="p

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論