Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

лабораторные С#

.pdf
Скачиваний:
38
Добавлен:
12.03.2015
Размер:
778.43 Кб
Скачать

3.# # % %. %

UserControlTimer2 ( " Timer Toolbox -

& * %.

4.Properties % Interval " Timer$ 1000 % Enabled $ True.

5.#! ( " Timer $! ! * #$ ! Timer.Tick %"$ # * #% (% % ""!:

this.Refresh();

 

6. # " # OnPaint % # #

 

" % *, " ' ", (

* , "

% :

 

protected override void OnPaint(PaintEventArgs pe)

{

base.OnPaint(pe);

Graphics g = pe.Graphics; g.FillRectangle(Brushes.Blue, 0, 0, this.Width,

this.Height);

}

7.

" # * #% ( # " #%

OnPaint #

" % *:

pe.Graphics.DrawString(DateTime.Now.ToLongTimeString(), this.Font, new SolidBrush(this.ForeColor), 0, 0);

8." File ! Save All, $! & * 0 0.

9." Build ! Build Solution.

" )

10. ! # % % Forml. Toolbox ( UserControlTimer2 ) "%. %# # , " 0' , " % , ! $ $! *

#% % #%.

 

 

 

 

11.

%

.

", $

* *

, " % # %

" !

" ", %.

 

 

3. & #

0 !

, " !

%

, !

* " , " ! % , 0 ( % %( % ( , " % .NET Framework. & )% ' * * %( % ( & , " % , , " #" #!, & # " " 0 # , " % .

! )

1.# Visual Studio Windows Forms.

WinButNum.

2." Project ! Add Class. ,

ClickButton ( Add.

31

3. " 1 , $! ClickButton #

Button:

public class ClickButton : System.Windows.Forms.Button

4. * #% ( # ' * #Clicks:

int mClicks; public int Clicks

{

get { return mClicks; }

}

5. # " # OnClick, $! " * ! % " % mClicks #! , # ( :

protected override void OnClick(EventArgs e)

{

mClicks++;

base.OnClick(e);

}

6. # " # OnPaint, $! * $ ($ " " % % , " % :

protected override void OnPaint(System.Windows.Forms.PaintEventArgs pevent)

{

base.OnPaint(pevent); System.Drawing.Graphics g = pevent.Graphics; System.Drawing.SizeF stringsize;

stringsize = g.MeasureString(Clicks.ToString(), this.Font, this.Width);

g.DrawString(Clicks.ToString(), this.Font, System.Drawing.SystemBrushes.ControlText, this.Width - stringsize.Width - 3, this.Height - stringsize.Height - 3);

}

7. & 0.

" ! )

8.! # % % Forml.

9.Toolbox ( , " ClickButton ) "% " " ! % %$ .

10.% .

11.) " ( ClickButton1. ", $$ ($ " " % %.

4. / -

%$ * " , # ( # ! %$ ! " #.

1. SaveFileDialog

7 ! * " & * ) !, " * * ! " SaveFileDialog.

32

, " % ! # , * %

" #

ShowDialog. 2 " " (*

DialogResult.OK ,

* * % $.

 

 

'

#

 

! :

 

 

1.# Windows Forms, % "

TestStandartDialog.

2.* , " MenuItem, # " % "

2 " #% -

3.* ) "% , " % richTextBox, " %"$ richTextBox1. % Dock % Fill.

4.* ) "% " SaveFileDialog. *, $

" " saveFileDialog1.

5.#! ( %, $! # * # #$ ! %"$ .

6.$ ! # * #% ( # # # - . / # & , # ! , " % richTextBox, ! ) % .

saveFileDialog1.Filter = "txt files (*.txt)|*.txt"; if(saveFileDialog1.ShowDialog() ==

System.Windows.Forms.DialogResult.OK

&& saveFileDialog1.FileName.Length > 0)

 

{

 

richTextBox1.SaveFile(saveFileDialog1.FileName,

 

RichTextBoxStreamType.PlainText);

 

}

7.

% .

8.

! 0 ) " # - .

9.

! " #% - & ) ("

" #

& ) !

"% % ").

10. # *, $ ! ) & # % " ".

2. ColorDialog

 

#

' ! "

* * ! " ColorDialog " , $! # * # .

, " % ! # TestStandartDialog, $! # * * " " * ! * ' " * ) " Windows % % ( " #! ".

' # ' ! ! :

1., " MenuItem # " % " –

2 " #% – .

2.* ) "% " ColorDialog.

33

3.*, $ " " colorDialog1.

4.#! ( % , $! # * $

! %"$ # #.

5. $ ! # * #% ( # # # ! ' " ) '! " * :

if (colorDialog1.ShowDialog() == DialogResult.OK)

{

richTextBox1.BackColor = colorDialog1.Color;

}

6. % .

3. FontDialog

# ! 0 ) "* * ! " FontDialog " , $! # * # .

, " % ! # TestStandartDialog, $! # * * " " * ! * 0 ) # "" " * %.

' # ! 0 ) ! :

1.2 # " 2 % " #% – %.

2.( ) "% " FontDialog.

3.*, $ " " fontDialog1.

4.#! ( " #% %, $! # * # # $ ! %"$ .

5.$ ! # * #% ( # # # ! 0 ) " 0 )

! " * :

if (fontDialog1.ShowDialog() == DialogResult.OK)

{

richTextBox1.Font = fontDialog1.Font;

}

6. % .

4. OpenFileDialog

7 !

* " ! * ! ) % *

, " % RichTextBox

) " Windows Forms, "

* * " OpenFileDialog.

, " % ! #

TestStandartDialog,

$! # *

* " " * ! * ! ).

1.2 # " 2 % " #%

2.( ) "% " OpenFileDialog.

" " openFileDialog1.

34

3.#! ( " #% , $! # * # # $ ! %"$ .

4.$ ! # * #% ( # # # ! ):

Stream myStream = null;

OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = @"c:\"; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All

files (*.*)|*.*"; openFileDialog1.FilterIndex = 2;

if (openFileDialog1.ShowDialog() == DialogResult.OK)

{

try

{

if ((myStream = openFileDialog1.OpenFile()) != null)

{

using (myStream)

{

richTextBox1.LoadFile(openFileDialog1.FileName,

RichTextBoxStreamType.PlainText);

}

}

}

catch (Exception ex)

{

MessageBox.Show("Error: Could not read file from disk: " + ex.Message);

}

}

5. % .

5.

%$ * " 1 ## # ", % "!" # CLR %$ !% " " # % " # % "!".

1. COM-

PDF-

# , * % ( & ) "% Microsoft .NET,#$ " !& & % !&

#, !& # % &

! &. !

% " ( "" !

*

!&

0,

, "% & & # * *,

% % %

!& .

 

 

#,

! ( # % " #! !

( %$

) "! .NET — #! Common Language Runtime),

!

.

 

 

 

35

 

#, % "!

 

# % " #!, !

. " " % " # " % % * COM-

" !, Microsoft ActiveX ) ! )% ' API Win32.

 

Microsoft

.NET

Framework

 

" # *

 

COM-

" ", COM+-% ", 0 " " !"

% " ' "!.

 

 

 

) " .NET Framework # # % ! " #

% "

#

 

% "!" —

Platform Invoke

 

COM

interoperability, ! * % * ! " .

1.# Windows- PDF Reader.

2.* ) "% , " ! OpenFileDialog MenuStrip.

3.#% ( ) "! Form1:

 

 

Text

* # %" ) " PDF

WindowState

Maximized

4. FileName , " OpenFileDialog # % !".

5.* % " & % File c " # " Open

Exit.

6.! Tools | Choose Toolbox Items…

7.# COM Components ! Adobe Acrobat (PDF)

Reader (, " % ""! Adobe Acrobat Reader) " OK. # *, $ Toolbox , / # * .

8.Adobe Acrobat Reader ) "% % % Dock $ Fill, % (Name) axAcroPDF1 ( " 1 %# ! ! * % ! " " #!, ' " " ! * !").

9.* $ % " Open:

private void openToolStripMenuItem_Click (object sender, System.EventArgs e)

{

openFileDialog1.Filter = "Файлы pdf|*.pdf"; openFileDialog1.ShowDialog(); axAcroPDF1.LoadFile(openFileDialog1.FileName);

}

10.

% $ ! Click #

% " Exit.

11.

% . ! # %"

) "

pdf & #, %,

) "% )

""! Adobe Acrobat Reader.

2. ' % API

% ! Platform Invoke % " "% #% % * )% ' % " #, ! & # ) & # "$ " (DLL).

36

/ % ! # " & "! % % % "!& )% ' # !& & # ( & & # ( & %" )% '.

# % "! # % )% ' % " #, % DLL-), % ! Platform Invoke & # , DLL ), % % " * & # # )% ' " . , % ! # & # ( %" ! )% ', ! # !, ! & # " ,

,"% %

%

"%

 

# %

# %

% " )% '

" .

 

 

 

!"

0 "

 

%

% "

)% '

 

1 )% '. -% '

# ! * $ (static) 0

(extern). #% " , # ( , % )% '.

" *

% %, * %

%

DllImport,

! & # " System.Runtime.InteropServices.

1.

#

WinAnim.

2.

 

) " % #% (

) "! :

 

 

Form1

 

Text

" ' ) "!

Button1

 

Name

btnAW_BLEND

Location

30; 62

Size

232; 23

Text

 

Button2

 

Name

btnHOR_AW_SLIDE

Location

30; 118

Size

232; 23

Text

8 *

Button3

 

Name

btnCenter_AW_SLIDE

Location

30; 182

Size

232; 23

Text

'

3. * WinAPIClass:

using System;

using System.Runtime.InteropServices; using System.Windows.Forms; namespace AnimatedWindow

{

public class WinAPIClass

{

#region Анимация окна

37

///<summary>

///Тип анимации окна. Перечисление возвращает тип int

///после приведения. Каждому элементу присвоено

///свое значение типа int.

///</summary>

[Flags]

public enum AnimateWindowFlags:int

{

AW_HOR_POSITIVE = 1,

AW_HOR_NEGATIVE = 2,

AW_VER_POSITIVE = 4,

AW_VER_NEGATIVE = 8, AW_CENTER = 16, AW_HIDE = 65536, AW_ACTIVATE = 131072, AW_SLIDE = 262144, AW_BLEND = 524288

};

///<summary>

///Анимация окна.

///</summary>

///<param name="hwnd">Окно.</param>

///<param name="dwTime">Время.</param>

///<param name="dwFlags">Тип анимации. Если в

///неуправляемом коде используется перечисление, то его

///нужно конвертировать в тип данных int.

</param>

///<returns></returns>

[DllImportAttribute("user32.dll",EntryPoint="AnimateWindow",Set

LastError=true)]

public static extern bool AnimateWindow(IntPtr hwnd,int dwTime,int dwFlags);

///<summary>

///Анимация окна.

///</summary>

///<param name="ctrl">Окно.</param>

///<param name="dwTime">Время.</param>

///<param name="Flags">Флаги.</param>

///<returns></returns>

public static bool AnimateWindow(Control ctrl,int dwTime, AnimateWindowFlags Flags)

{

return AnimateWindow(ctrl.Handle,dwTime,(int)Flags);

}

#endregion

}

}

4. # $ :

private void btnAW_BLEND_Click(object sender, System.EventArgs e)

{

//Скрываем окно this.Hide();

//Запускаем анимацию.

//Второй параметр в скобках время анимации в

//миллисекундах.

38

WinAPIClass.AnimateWindow(this, 3000, WinAPIClass.AnimateWindowFlags.AW_ACTIVATE|

WinAPIClass.AnimateWindowFlags.AW_BLEND); // Отображаем кнопки после анимации this.btnAW_BLEND.Invalidate(); this.btnHOR_AW_SLIDE.Invalidate(); this.btnCenter_AW_SLIDE.Invalidate();

}

private void btnHOR_AW_SLIDE_Click(object sender, System.EventArgs e)

{

this.Hide(); WinAPIClass.AnimateWindow(this, 3000,

WinAPIClass.AnimateWindowFlags.AW_HOR_POSITIVE| WinAPIClass.AnimateWindowFlags.AW_SLIDE); this.btnAW_BLEND.Invalidate(); this.btnHOR_AW_SLIDE.Invalidate(); this.btnCenter_AW_SLIDE.Invalidate();

}

private void btnCenter_AW_SLIDE_Click(object sender, System.EventArgs e)

{

this.Hide(); WinAPIClass.AnimateWindow(this, 3000,

WinAPIClass.AnimateWindowFlags.AW_CENTER| WinAPIClass.AnimateWindowFlags.AW_SLIDE); this.btnAW_BLEND.Invalidate(); this.btnHOR_AW_SLIDE.Invalidate(); this.btnCenter_AW_SLIDE.Invalidate();

}

5. % . % # " '.

6. - windows

%$ , % ( & #$% ""

$

%$ !

 

"" # !" ".

 

1. # $

 

 

 

$ $!& # %"

* " $ & #

" *

" ! $ . !$ $

# " &

", '

'!, 0

" %".

.NET Framework # !, !

 

#

* " " * %( * ! .

& #

!

#

%

%#

#

 

,

# (

*

" *

* *

PageSetupDialog,

PrintDialog

 

PrintPreviewDialog

#

%

$*

# %".

 

 

 

 

 

 

 

#

1. # ! Windows Forms. WinPrint.

39

2.Toolbox ) "% , " PrintDocument.

3." (* Properties # " printDocument1 # * $ ! PrintPage % # * #% ( #:

Font myFont = new Font("Tahoma", 12, FontStyle.Regular, GraphicsUnit.Pixel);

string Hello = "Hello World!"; e.Graphics.DrawString(Hello, myFont, Brushes.Black, 20, 20);

4.Form1 " %.

5.Toolbox ( , " Button ) "%.

6. # * # $ Text:

Page Setup,

Print,

Print Preview.

7.& # " %$* " ! .

8.Toolbox ( ) "% " ! PageSetupDialog,

PrintDialog PrintPreviewDialog.

9.Properties % % Document # " # $ PrintDocumentl.

10., " PrintDialog % AllowSomePages$ True.

-

11. % # #! ( % Page Setup # * #% ( #:

pageSetupDialog1.ShowDialog();

12. % # #! ( Print # * #% (

#:

if (printDialog1.ShowDialog() == DialogResult.OK) printDocument1.Print();

13. % # #! ( Print Preview # * #% ( #:

printPreviewDialog1.ShowDialog();

14. % . ! #% , $! * ! $!& # !& $ .

2. $

! , % , ! # 0,

% 1, # #

, 0 ( *

! * ! )

$* # ".

1.0, ! % 1.

2.Toolbox ( , " - # OpenFileDialog) "%.

3.# " Properties % Filter , " openFileDialog1 $ Text Files | *.txt, $ FileName.

4.) "% # * % % " Open File.

40