VB课程设计 屏幕保护程序

作者&投稿:厨人段 (若有异议请与网页底部的电邮联系)
我有一个屏幕保护教程VB的,可惜字数太多,这里贴不下!

二、 生成界面
对象 属性 设置值
Form1 AutoRedraw True
BackColor 黑色
BorderStyle 0 - None
Caption 我的编程世界……
ControlBox False
KeyPreview True
MaxButton False
MinButton False
Name frmScreenSaver
ScaleMode 3 - Pixel
WindowState 2 - Maximized
Label1 AutoSize True
BackStyle 0 - Transparent
BorderStyle 0 - None
Caption 我的编程世界……
ForeColor 红色
Font 幼圆
Timer1 Enabled False
Interval 1

在窗体下加入下面的代码:
Option Explicit

Dim QuitFlag As Boolean
Const SPI_SETSCREENSAVEACTIVE = 17
Private Declare Function SetWindowPos Lib "user32" ( _
ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal cx As Long, _
ByVal cy As Long, _
ByVal wFlags As Long _
) As Long
Private Declare Function ShowCursor Lib "user32" ( _
ByVal bShow As Long _
) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" ( _
ByVal uAction As Long, _
ByVal uParam As Long, _
ByVal lpvParam As Long, _
ByVal fuWinIni As Long _
) As Long

Private Sub Form_Click()
QuitFlag = True
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
QuitFlag = True
End Sub
Private Sub Form_Load()
Dim Throw As Long

Throw = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, ByVal 0&, 0)
Select Case UCase$(Left$(Command$, 2))
Case "/A"
MsgBox "此屏幕保护程序不可以设置口令。"
Unload Me
Exit Sub
Case "/C"
MsgBox "此屏幕保护程序没有设置项。"
Unload Me
Exit Sub
Case "/P"
Unload Me
Exit Sub
Case "/S"
Show
Throw = ShowCursor(False)
Throw = SetWindowPos(hwnd, -1, 0, 0, 0, 0, (&H2 Or &H1))
Label1.Left = frmScreenSaver.ScaleWidth
Label1.Top = (frmScreenSaver.ScaleHeight - Label1.Height) / 2
Do
Label1.Left = Label1.Left - 3
Sleep (50)
If Label1.Left <= -Label1.Width Then Label1.Left = frmScreenSaver.ScaleWidth + Label1.Width
DoEvents
Loop Until QuitFlag = True
Timer1.Enabled = True
Case Else
Unload Me
Exit Sub
End Select
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Xnow As Single, Ynow As Single
Static Xlast As Single, Ylast As Single

Xnow = X
Ynow = Y
If Xlast = 0 And Ylast = 0 Then
Xlast = Xnow
Ylast = Ynow
Exit Sub
End If
If Xnow <> Xlast Or Ynow <> Ylast Then
QuitFlag = True
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim Throw As Long

Throw = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 1, ByVal 0&, 0)
Throw = ShowCursor(True)
End Sub
Private Sub Label1_Click()
QuitFlag = True
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
QuitFlag = True
End Sub
Private Sub Timer1_Timer()
Unload Me
End Sub

VB课程设计屏幕保护程序200分~

添加下列代码即可,一个如融化屏幕般的屏保:
Option Explicit

Private Declare Function GetDC Lib "user32" ( _
ByVal hWnd As Long) As Long

Private Declare Function ReleaseDC Lib "user32" ( _
ByVal hWnd As Long, _
ByVal hDC As Long) As Long

Private Declare Function BitBlt Lib "gdi32" ( _
ByVal hDestDC As Long, _
ByVal x As Long, _
ByVal Y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal hSrcDC As Long, _
ByVal xSrc As Long, _
ByVal ySrc As Long, _
ByVal dwRop As Long) As Long


Private lngDC As Long ' hDC of the screen, available to every sub/function, wich allows us to call ReleaseDC(0, lngDC) in cExit
Private blnLoop As Boolean

Private Sub Form_Load()
Dim intX As Integer, intY As Integer
Dim intI As Integer, intJ As Integer
Dim intWidth As Integer, intHeight As Integer

Form1.AutoRedraw = True
Form1.BorderStyle = 0
Form1.WindowState = 2
intWidth = Screen.Width / Screen.TwipsPerPixelX 'Screenwidth
intHeight = Screen.Height / Screen.TwipsPerPixelY 'Screenheight

Form1.Width = Screen.Width ' Set formwidth to screenwidth
Form1.Height = Screen.Height ' Set formheight to screenheight

lngDC = GetDC(0) ' GetDC(0) to get the hDC of the screen

Call BitBlt(hDC, 0, 0, intWidth, intHeight, lngDC, 0, 0, vbSrcCopy) ' BitBlt screen onto form
Form1.Visible = vbTrue ' Make form visible
Form1.AutoRedraw = vbFalse ' Set autoredraw to 0 (or your graphics-card might cause a reboot)

Randomize

blnLoop = vbTrue
Do While blnLoop = vbTrue
intX = (intWidth - 128) * Rnd
intY = (intHeight - 128) * Rnd

intI = 2 * Rnd - 1 ' Horizontal displacement
intJ = 2 * Rnd - 1 ' Vertical displacement

' Move a part of the screen 1 pixel in a semi-random direction, to get the "melting" effect
Call BitBlt(Form1.hDC, intX + intI, intY + intJ, 128, 128, Form1.hDC, intX, intY, vbSrcCopy)

DoEvents
Loop

Set Form1 = Nothing ' Remove form from memory
Call ReleaseDC(0, lngDC) ' Release the screen-hDC
End
End Sub

Private Sub Form_Click()
blnLoop = vbFalse
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
blnLoop = vbFalse
End Sub

步骤一:绘制界面。
新建一个标准EXE工程,将其Caption属性改为“超级记事本”,点击Icon属性给它找个合适的Icon图标。单击菜单“工程”-“部件”,在弹出的“部件”对话框里找到Microsoft RichText Box 6.0和公共对话框Microsoft Common Dialog 6.0并选中它们,单击“确定”按钮。这时左边的工具栏上出现了我们刚才新添的两个控件了。在窗体上绘制RichText Box和Commn Dialog,其中RichText Box的大小和位置可不用理睬,我们将在代码中处理它,当然,有必要把它的ScrollBar属性设为2-rtfVertical,这样在打开和编辑文件时垂直滚动条才可用。

步骤二:编辑菜单。
按Ctrl+E调出菜单编辑器,我们来做如下几个菜单:
一.文件菜单:
文件(第一层) mnuFile
新建(第二层) mnuNew
打开(第二层) mnuOpen
保存(第二层) mnuSave
- (第二层) mnuFileSep (分隔线)
退出(第二层) mnuExit
二.编辑菜单:
编辑(第一层) mnuEdit
复制(第二层) mnuCopy
剪切(第二层) mnuCut
粘贴(第二层) mnuPaste
- (第二层) mnuEditSep (分隔线)
全选(第二层) mnuSelecAll
三.搜索菜单:
搜索(第一层) mnuSearch
查找(第二层) mnuFind
查找下一个(第二层) mnuFindOn
四.帮助菜单:
帮助(第一层) mnuHelp
使用说明(第二层) mnuUsage
关于(第二层) mnuAbout
(注:各菜单项的快捷键请自行设置)

好了,其它的菜单项以后再根据需要添加。现在进入:

步骤三:编写代码。

'声明查找变量
Dim sFind As String
'声明文件类型
Dim FileType, FiType As String

'初始化程序
Private Sub Form_Load()
'设置程序启动时的大小
Me.Height = 6000
Me.Width = 9000
End Sub

'设置编辑框的位置和大小
Private Sub Form_Resize()
On Error Resume Next '出错处理
RichTextBox1.Top=20
RichTextBox1.Left=20
RichTextBox1.Height = ScaleHeight-40
RichTextBox1.Width = ScaleWidth-40
End Sub

'新建文件
Private Sub mnuNew_Click()
RichTextBox1.Text = "" '清空文本框
FileName = "未命名"
Me.Caption = FileName
End Sub


'打开文件
Private Sub mnuOpen_Click()
CommonDialog1.Filter = "文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
RichTextBox1.Text = "" '清空文本框
FileName = CommonDialog1.FileName
RichTextBox1.LoadFile FileName
Me.Caption = "超级记事本:" & FileName
End Sub

'保存文件
Private Sub mnuSave_Click()
CommonDialog1.Filter = "文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
CommonDialog1.ShowSave
FileType = CommonDialog1.FileTitle
FiType = LCase(Right(FileType, 3))
FileName = CommonDialog1.FileName
Select Case FiType
Case "txt"
RichTextBox1.SaveFile FileName, rtfText
Case "rtf"
RichTextBox1.SaveFile FileName, rtfRTF
Case "*.*"
RichTextBox1.SaveFile FileName
End Select
Me.Caption = "超级记事本:" & FileName
End Sub

'退出
Private Sub mnuExit_Click()
End
End Sub

'复制
Private Sub mnuCopy_Click()
Clipboard.Clear
Clipboard.SetText RichTextBox1.SelText
End Sub

'剪切
Private Sub mnuCut_Click()
Clipboard.Clear
Clipboard.SetText RichTextBox1.SelText
RichTextBox1.SelText = ""
End Sub

'全选
Private Sub mnuSelectAll_Click()
RichTextBox1.SelStart = 0
RichTextBox1.SelLength = Len(RichTextBox1.Text)
End Sub

'粘贴
Private Sub mnuPaste_Click()
RichTextBox1.SelText = Clipboard.GetText
End Sub

'查找
Private Sub mnuFind_Click()
sFind = InputBox("请输入要查找的字、词:", "查找内容", sFind)
RichTextBox1.Find sFind
End Sub

'继续查找
Private Sub mnuFindOn_Click()
RichTextBox1.SelStart = RichTextBox1.SelStart + RichTextBox1.SelLength + 1
RichTextBox1.Find sFind, , Len(RichTextBox1)
End Sub

'使用说明
Private Sub mnuReadme_Click()
On Error GoTo handler
RichTextBox1.LoadFile "Readme.txt", rtfText '请写好Readme.txt文件并存入程序所在文件夹中
Me.Caption = "超级记事本:" & "使用说明"
Exit Sub
handler:
MsgBox "使用说明文档可能已经被移除,请与作者联系。", vbOKOnly, " 错误信息"
End Sub

'关于
Private Sub mnuAbout_Click()
MsgBox "超级记事本 Ver1.0 版权所有(C) 2001 土人",vbOKOnly,"关于"
End Sub

'设置弹出式菜单(即在编辑框中单击鼠标右键时弹出的动态菜单)
Private Sub RichTextBox1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
PopupMenu mnuEdit, vbPopupMenuLeftAlign
Else
Exit Sub
End If
End Sub

'防止在切换输入法时字体自变(感谢王必成先生提供此方案)
Private Sub RichTextBox1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeySpace Then
RichTextBox1.SelFontName = CommonDialog1.FontName
End If
End Sub

至此,我们的记事本可以编译使用了。点击菜单“文件”-“生成XXX.EXE”,回到桌面运行我们的记事本看看,是不是颇有成就感?
当然,这样的记事本还比较粗糙,我们还需要做些工作,请看下一章。


第二章 美化程序界面

多数字处理软件都有工具栏和状态栏。工具栏和状态栏除了能美化我们的程序使其更具有专业性质外,还给用户带来操作上的便利。现在我们就来做一做这两样东西。

一.工具栏
(一)制作工具栏
单击“工程”-“部件”,选中Microsoft Windows Common Control 6.0并确定。这时,我们要用到的控件就出现在左边的工具栏上了。
要做工具栏,首先需要一个叫ImageList的控件来装载图像。在程序界面上添加它,然后右键单击此控件,左键单击“属性”,弹出“属性页”对话框的“图像”,再单击“插入图片”就可以一次性装载图片了(如不满意,以后还可以添加)。图片可在C:\Microsoft Visual Studio\Common\Graphics\Bitmaps\TlBr_W95下选择(这里假设你的VB安装在C盘下)。注意了:在插入图片时给每一张图片注明关键字,以便在引用图片时不至于混乱。如插入“新建”的图片,我们在“关键字”栏注明“New”。
图片有了,接下来在程序界面添加工具栏(ToolBar)。添加后工具栏就出现在菜单下面,右键单击它,选择“属性”,在弹出的“属性页”对话框中的“通用”项作些设置,主要如下两项:
1.“图像列表”:选择ImageList1
2.“样式”:根据喜爱选择1-trbStandard或者2-trbFlat
继续点击“属性页”的“按钮”选项,插入若干按钮。按钮有多种样式,请根据需要设置。这里请一定注意:每一个与用户操作有关的按钮都必须注明关键字、装载图片,如“新建”按钮,在“关键字”项注明“新建”,在“图像”项键入“New”(即ImageList1中的图片关键字),需要的话还可以在“工具提示文本”项填入适当的提示语。
(二)编写工具栏的按钮代码
工具栏按钮的代码编辑很简单,可以按照下面的格式去编写:
Private Sub ToolBar1_ButtonClick(ByVal Button As MSComctlLib.Button)
On Error Resume Next '出错处理
Select Case Button.Key '按关键字选择
Case "新建"
mnuNew_Click '等于菜单项“新建”被单击
Case "打开" '等于菜单项“打开”被单击
mnuOpen_Click
'......(继续编写其它按钮的代码)
End Select
End Sub

完成后试运行一下我们的程序,我们发现,有了工具栏之后,程序变得漂亮多了,只是有一个问题:打开一个较长的文档后,编辑框的下拉滚动条向上的箭头不见了。原因是:工具条占用一定的空间。解决方法:将“设置编辑框的位置和大小”中的RichTextBox1.Top = 20 和 RichTextBox1.Height = Me.ScaleHeight - 40分别改为RichTextBox1.Top = 380,RichTextBox1.Height = Me.ScaleHeight - 400即可。

二.状态栏
(一)制作状态栏
状态栏的英文名字叫StatusBar,在窗体上添加它后会默认出现在窗体的最下方。用鼠标右键点击它,调出“属性页”对话框,单击“窗格”项,插入一些窗格,可以将各个窗格的“样式”设置为:
0-sbrText 显示文本,需编写代码
1-sbrCaps 显示大小写状态,无需编程
2-sbrNum 显示NumLock键开关状态,无需编程
3-sbrIns 显示Insert键状态,无需编程
4-sbrScr1
5-sbrTime 显示时间,不编程时时间不会随系统时钟变化
6-sbrDate 显示日期,无需编程
注意:加进状态栏后需将Form_Resze中的RichTextBox1.Height = Me.ScaleHeight - 400改为RichTextBox1.Height = Me.ScaleHeight - 600。
(二)状态栏根据其“样式”属性决定用不用编写代码(如上文所述)。下面举些例子,读者可以认真揣摩,从而达到举一反三的效果。

例一:用户选取了“新建”后,让第一个窗格显示:“目前状态:正在打开文件《……》”。请将下面代码写进“打开”菜单里面:
StatusBar1.Panels(1).Text = "目前状态:正在打开文件" & "《" &CommonDialog1.FileTitle & "》"

例二:让第三个窗格显示时间并让时间跟随系统时钟变化。
首先,给程序加一个Timer控件,将其Interval属性设为1000。然后:
在Form_Load过程加入:StatusBar1.Panels(1).Text = Time;然后给Timer控件编写代码:

Private Sub Timer1_Timer()
If StatusBar1.Panels(3).Text CStr(Time) Then
StatusBar1.Panels(3).Text = Time
End If
End Sub

例三:当编辑框的文本发生变化时让第一个窗格显示:“正在编辑文档:文件名”。
Private Sub RichTextBox1_Chang()
StatusBar1.Panels(1).Text = "正在编辑文档:" & CommonDialog1.FileName
End Sub