none
有人做屏幕保护程序吗?我遇到了很多问题 RRS feed

  • 常规讨论

  • 首先我按照VS初学者示例的说明把做好的程序后缀改为SCR

    但之后问题来了

    我右键选择安装之后他出现在了系统属性屏幕保护程序选项里

    ·程序无法被显示在这个选项卡的预览窗口内(一个类似显示器的图象)

    ·当我在这个选项卡内选择我的屏幕保护程序时 他会自动运行(也就相当于双击了这个程序)

    ·选择程序右键菜单配置选项后出来的配置界面如何设置?

    2009年1月28日 7:17

全部回复

  •  

    http://support.microsoft.com/kb/818359

     

     

    本文分步介绍了如何通过使用 Microsoft Visual Basic.NET 或 Microsoft Visual Basic 2005 创建屏幕保护程序。 您可以开发字幕屏幕保护程序与配置窗体。 您可以构建可以测试,可以在 \ Windows 目录中注册屏幕保护程序。

    创建屏幕保护程序项目

    1. 启动 Microsoft Visual Studio.NET 或 Microsoft Visual Studio 2005。
    2. 文件 菜单上指向 新建 ,然后单击 项目
    3. 在, 新建项目 对话框中单击 项目类型 下的 Visual Basic 项目 ,然后单击 模板 下的 Windows 应用程序

      请注意 在 Visual 的 Studio 2005 中单击 Visual Basic 项目类型 下。
    4. MyScreenSaver ,该项目命名,然后单击 确定

      默认,会创建 Form 1。
    5. 如下所示设置 Form 1 的属性:
      • 名称 frmscr
      • ControlBox False
      • MaximizeBox False
      • MinimizeBox False
      • BackColor 黑色
      • 文本 ""(no text)
      • 最顶层 True
      • WindowState 最大化
    6. 在解决方案资源管理器窗口右键单击 Form 1 ,然后单击 属性
    7. 在文件属性窗口中设置该 文件名称 frmscr.vb 属性。
    8. 添加一个 标签 控件。

      默认情况下, Label 1 已添加到窗体。
    9. 如下所示设置 Label 1 的属性:
      • 名称 lblMessage
      • ForeColor 黄色
    10. 添加 计时器 控件。

      Timer1 被添加到窗体。
    11. 将计时器的 Enabled 属性设置为 True 中。
    12. 要禁用按 Ctrl+Alt+Del 组合键在 Microsoft Windows 95 操作系统或在 Microsoft Windows 98 操作系统并获得用户的字幕文本的上一个首选项,添加到 加载 事件 frmscr 窗体的以下代码:
       
        Dim tmpLng As Integer
              tmplng = SystemParametersInfo(SPI_SCREENSAVERRUNNING, 1&, 0&, 0&)
      
              Dim pRegKey As RegistryKey = Registry.CurrentUser
              pRegKey = pRegKey.OpenSubKey("Software\Test Screen Saver", True)
              Dim val As Object = pRegKey.GetValue("Message", "Enter Text here")
              pRegKey.Close()
              lblMessage.Text = val.ToString
      
              Me.Cursor.Current.Hide()
        
      Cursor.Current.Hide() 方法使光标屏幕保护程序显示时消失。
    13. 若要按任何键时,请关闭屏幕保护程序,将下面的代码添加到 frmscr 窗体的 KeyDown 事件:
      'Immediately end when any key is pressed.
              Me.Close()
      
      Me.Close()
    14. 若要移动鼠标时,请关闭屏幕保护程序,将下面的代码添加到 frmscr 窗体的 MouseMove 事件:
       
              Static OldX As Integer
              Static OldY As Integer
      
              'Determines whether the mouse was moved and whether the movement was large.
              'If so, the screen saver is ended.
              If (OldX > 0 And OldY > 0) And (Abs(e.X - OldX) > 3 Or Abs(e.Y - OldY) > 3) Then
                  Me.Close()
      
              End If
              'Assigns the current X and Y locations to OldX and OldY.
              OldX = e.X
              OldY = e.Y
      
    15. 若要在关闭屏幕保护程序和要重新启用按 Ctrl+Alt+Del 组合键将以下代码添加 frmscr 窗体的 关闭 事件中时,还原光标:
      'Restore the mouse cursor.
              Dim tmplng As Integer
              Me.Cursor.Current.Show()
      
             'Re-enable the CTRL+ALT+DEL key combination if it is disabled.
              tmplng = SystemParametersInfo(SPI_SCREENSAVERRUNNING, 0&, 0&, 0&)
      
    16. 为了一个字幕效果请将以下代码添加到 Timer 控件在 Tick 事件:
      'Determine whether the Message has moved completely off the left side of the screen.
              If lblMessage.Left < (0 - lblMessage.Width) Then
                  lblMessage.Left = GetScaleWidth(Me) 'ScaleWidth
              End If
      
              'Moves lblMessage to the left.
              lblMessage.Left = lblMessage.Left - 10
      
    17. 项目 菜单上单击 添加模块

      情况默认,创建 Module 1
    18. 显示用户的配置窗体或屏幕保护程序窗体 (基于 Windows 的命令行参数),请向 Module 1 中添加以下的 Sub Main 事件:
      Public Sub Main(ByVal args As String())
              Dim Inst As Boolean = False
      
              If args.Length > 0 Then
                  sStartType = args(0).ToLower.Trim().Substring(0, 2)
      
                  If sStartType = "" Then
                      'This will occur when a user right-clicks the .scr
                      'file and then selects "configure"
                      sStartType = "/c"
                  End If
      
                  ' Determine whether the screen saver should show user definable options.
                  If sStartType = "/c" Then
                      Dim usercnfg As New frmcnfg
                      usercnfg.ShowDialog()
      
                      ' Exit the application.
                      Exit Sub
                  End If
      
                  ' Determine whether the screen saver should just execute.
                  If sStartType = "/s" Then
                      'Check for previous instance.
                      Inst = PrevInstance()
                      If Not (Inst) Then
                          ' Create a Screen Saver form, and then display the form.
                          Dim scrsvr As New frmscr
                          scrsvr.ShowDialog()
                      Else
                          'If a previous instance exists, exit the application.
                          Exit Sub
                      End If
                  End If
              End If
       End Sub 
      
      PrevInstance() 函数可确定是否正在运行一个的屏幕保护程序的上一个实例。
    19. 将来确定的屏幕保护程序的一个以前实例是否正在运行, PrevInstance() 函数添加到 Module 1 ,如下所示:
      Function PrevInstance() As Boolean
              If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
                  Return True
              Else
                  Return False
              End If
      End Function

    添加屏幕保护程序设置窗体

    1. 项目 菜单上单击 添加 Windows 窗体
    2. 添加新项 对话框中,单击 模板 下的 Windows 窗体
    3. 名称 文本框中键入 frmcnfg ,,然后单击了此操作的 打开
    4. frmcnfg 窗体的属性窗口,请将 Text 属性设置为 屏幕保护程序设置
    5. 添加 Label 控件,添加一个 TextBox 控件,并向 frmcnfg 窗体中添加两个 Button 控件。
    6. 设置下列属性:
      • Label 1
        文本 输入消息
      • TextBox 1
        名称 txtmessage
        文本 输入此文本
        多行 True
      • Button 1
        名称 cmdOK
        文本 确定 (& S)
      • Button 2
        名称 cmdCancel
        文本 取消 (& S)
    7. 接受来自用户在字幕文本,然后以一个的注册表中存储该文本将下面的代码添加到 frmcnfg 窗体的 确定 按钮:
              'Save the current settings to
              'HKEY_CURRENT_USER\Software\
              'in the registry.
      
              Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software", True)
              Dim newkey As RegistryKey = key.CreateSubKey("Test Screen Saver")
              newkey.SetValue("Message", txtmessage.Text)
              newkey.Close()
              Me.Close()
      
      Dim 项另存为 RegistryKey 其中是 = Registry.CurrentUser.OpenSubKey("Software", True) Dim newkey 另存为 RegistryKey 其中是 = key.CreateSubKey"测试屏幕保护程序") newkey.SetValue ("消息") txtmessage.Text newkey.Close() Me.Close()

    完整代码列表

    完整代码列表 (frmscr)

    Imports Microsoft.Win32
    Imports Microsoft.VisualBasic
    Imports System.Math
    Public Class frmscr
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'The Windows Form Designer requires this call.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call.
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        'Required by the Windows Form Designer.
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The Windows Form Designer requires the following procedure.
        'It can be modified by using the Windows Form Designer.  
        'Do not modify the procedure by using the Code editor.
        Friend WithEvents lblMessage As System.Windows.Forms.Label
        Friend WithEvents Timer1 As System.Windows.Forms.Timer
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container
            Me.lblMessage = New System.Windows.Forms.Label
            Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
            Me.SuspendLayout()
            '
            'lblMessage
            '
            Me.lblMessage.AutoSize = True
            Me.lblMessage.BackColor = System.Drawing.Color.Black
            Me.lblMessage.ForeColor = System.Drawing.Color.Yellow
            Me.lblMessage.Location = New System.Drawing.Point(72, 118)
            Me.lblMessage.Name = "lblMessage"
            Me.lblMessage.Size = New System.Drawing.Size(193, 36)
            Me.lblMessage.TabIndex = 1
            Me.lblMessage.Text = "screen saver"
            Me.lblMessage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
            '
            'Timer1
            '
            Me.Timer1.Enabled = True
            '
            'frmscr
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(15, 33)
            Me.BackColor = System.Drawing.Color.Black
            Me.ClientSize = New System.Drawing.Size(292, 273)
            Me.ControlBox = False
            Me.Controls.Add(Me.lblMessage)
            Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 21.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            Me.MaximizeBox = False
            Me.MinimizeBox = False
            Me.Name = "frmscr"
            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
            Me.TopMost = True
            Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
        Private Sub frmscr_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim tmpLng As Integer
            tmpLng = SystemParametersInfo(SPI_SCREENSAVERRUNNING, 1&, 0&, 0&)
    
            'Get the user's previous preference for the marquee message.
    
            Dim pRegKey As RegistryKey = Registry.CurrentUser
            pRegKey = pRegKey.OpenSubKey("Software\\Test Screen Saver")
            Dim val As Object = pRegKey.GetValue("Message")
            pRegKey.Close()
            lblMessage.Text = val.ToString
    
            'Make the cursor disappear.
    
            Me.Cursor.Current.Hide()
        End Sub
    
        Private Sub frmscr_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
            'Immediately end when any key is pressed.
            Me.Close()
        End Sub
    
        Private Sub frmscr_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
            'Center the lblMessage label to the form.
            lblMessage.Left = GetScaleWidth(Me)
            lblMessage.Top = (GetScaleWidth(Me) - lblMessage.Height) / 3
        End Sub
    
        Private Sub frmscr_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            'System.Windows.Forms.Cursor.Current.Show
            ''Restore the mouse cursor.
            Dim tmplng As Integer
            Me.Cursor.Current.Show()
    
            tmplng = SystemParametersInfo(SPI_SCREENSAVERRUNNING, 0&, 0&, 0&)
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            'Determine whether the Message has moved completely off the left side of the screen.
            If lblMessage.Left < (0 - lblMessage.Width) Then
                lblMessage.Left = GetScaleWidth(Me) 'ScaleWidth
            End If
    
            'Moves lblMessage to the left.
            lblMessage.Left = lblMessage.Left - 10
    
    
        End Sub
    
        Private Sub frmscr_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    
            Static OldX As Integer
            Static OldY As Integer
    
            'Determines whether the mouse was moved and whether the movement was large.
            'if so, the screen saver is ended.
            If (OldX > 0 And OldY > 0) And (Abs(e.X - OldX) > 3 Or Abs(e.Y - OldY) > 3) Then
                Me.Close()
    
            End If
    
            'Assigns the current X and Y locations to OldX and OldY.
            OldX = e.X
            OldY = e.Y
    
        End Sub
    End Class

    请注意 您必须更改该代码在 Visual Basic 2005 中。 默认,Visual Basic 在您创建一个 Windows 窗体项目时创建项目的两个文件。 如果 Form 1 名窗体为在两个文件,表示该窗体被命名为 Form 1.vb 和 Form1.Designer.vb。 您在 Form 1.vb 文件中编写代码。 Windows 窗体设计器将代码写入 Form1.Designer.vb 文件。 Windows 窗体设计器使用部分的关键字将 Form 1 的实现划分为两个单独的文件。 此行为可防止被 interspersed 用您的代码设计器生成代码。

    有关新的 Visual Basic 2005 语言增强功能,请访问下面的 Microsoft Developer Network (MSDN) 网站:

    http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx (http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx)

    有关分部类和 Windows 窗体设计器的详细信息,请访问下面的 MSDN 网站:

    http://msdn2.microsoft.com/en-us/library/ms171843.aspx (http://msdn2.microsoft.com/en-us/library/ms171843.aspx)

    完整代码列表 (Module 1)

    Imports System.Runtime.InteropServices
    Imports System.Environment
    
    Module Module1
    
        Public Declare Auto Function SystemParametersInfo Lib "user32" (ByVal uAction As Integer, ByVal uParam As Integer, ByRef pvParam As Integer, ByVal fuWinIni As Integer) As Boolean
        Public Const SPI_SCREENSAVERRUNNING = 97&
        Public Const frmedge As Integer = 4
        Public nMouseMoves&
        Dim sStartType
    
    
        Public Sub Main(ByVal args As String())
            Dim Inst As Boolean = False
    
    
            If args.Length > 0 Then
                sStartType = args(0).ToLower.Trim().Substring(0, 2)
    
                If sStartType = "" Then
                    'This will occur when a user right-clicks the .SCR
                    'file and chooses "configure"
                    sStartType = "/c"
                End If
    
                ' Determine whether the screen saver should show user-definable options.
                If sStartType = "/c" Then
                    Dim usercnfg As New frmcnfg
                    usercnfg.ShowDialog()
    
                    ' Exit the application.
                    Exit Sub
                End If
    
                ' Determine whether the screen saver should just execute.
                If sStartType = "/s" Then
                    'Check for previous instance.
                    Inst = PrevInstance()
                    If Not (Inst) Then
                        ' Create a Screen Saver form and display the form.
                        Dim scrsvr As New frmscr
                        scrsvr.ShowDialog()
                    Else
                        'If a previous instance exists, exit the application.
                        Exit Sub
                    End If
                End If
            End If
        End Sub
     
    
        Function PrevInstance() As Boolean
            If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
                Return True
            Else
                Return False
            End If
        End Function
        Public Function GetScaleWidth(ByVal frm As Form) As Integer
    
            Dim ctl As Control
            Dim w As Integer = frm.ClientSize.Width
    
            For Each ctl In frm.Controls
                With ctl
                    If .GetContainerControl() Is frm Then
                        If .Dock = DockStyle.Left Or .Dock = DockStyle.Right Then
                            w = w - .Size.Width
                        End If
                    End If
                End With
            Next
            GetScaleWidth = w - frmedge
        End Function
    
        
    
    End Module

    完整代码列表 (frmcnfg)

    Imports Microsoft.Win32
    Public Class frmcnfg
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'The Windows Form Designer requires this call.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call.
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        'Required by the Windows Form Designer.
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The Windows Form Designer requires the following procedure.
        'It can be modified by using the Windows Form Designer.  
        'Do not modify the procedure by using the Code editor.
        Friend WithEvents txtmessage As System.Windows.Forms.TextBox
        Friend WithEvents cmdcancel As System.Windows.Forms.Button
        Friend WithEvents cmdOk As System.Windows.Forms.Button
        Friend WithEvents label1 As System.Windows.Forms.Label
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.cmdOk = New System.Windows.Forms.Button
            Me.cmdcancel = New System.Windows.Forms.Button
            Me.label1 = New System.Windows.Forms.Label
            Me.txtmessage = New System.Windows.Forms.TextBox
            Me.SuspendLayout()
            '
            'cmdOk
            '
            Me.cmdOk.Location = New System.Drawing.Point(200, 184)
            Me.cmdOk.Name = "cmdOk"
            Me.cmdOk.Size = New System.Drawing.Size(56, 24)
            Me.cmdOk.TabIndex = 0
            Me.cmdOk.Text = "&OK"
            '
            'cmdcancel
            '
            Me.cmdcancel.Location = New System.Drawing.Point(272, 184)
            Me.cmdcancel.Name = "cmdcancel"
            Me.cmdcancel.Size = New System.Drawing.Size(56, 24)
            Me.cmdcancel.TabIndex = 1
            Me.cmdcancel.Text = "&Cancel"
            '
            'label1
            '
            Me.label1.Location = New System.Drawing.Point(24, 40)
            Me.label1.Name = "label1"
            Me.label1.Size = New System.Drawing.Size(152, 24)
            Me.label1.TabIndex = 2
            Me.label1.Text = "Enter Message"
            '
            'txtmessage
            '
            Me.txtmessage.Location = New System.Drawing.Point(32, 64)
            Me.txtmessage.Multiline = True
            Me.txtmessage.Name = "txtmessage"
            Me.txtmessage.Size = New System.Drawing.Size(296, 96)
            Me.txtmessage.TabIndex = 3
            Me.txtmessage.Text = ""
            '
            'frmcnfg
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(344, 221)
            Me.Controls.Add(Me.txtmessage)
            Me.Controls.Add(Me.label1)
            Me.Controls.Add(Me.cmdcancel)
            Me.Controls.Add(Me.cmdOk)
            Me.Name = "frmcnfg"
            Me.Text = "CONFIGURATION"
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
    
        Private Sub cmdcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcancel.Click
            Me.Close()
        End Sub
    
        Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
            'Save the current settings to
            'HKEY_CURRENT_USER\Software\
            'in the registry.
    
    
            Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software", True)
            Dim newkey As RegistryKey = key.CreateSubKey("Test Screen Saver")
            newkey.SetValue("Message", txtmessage.Text)
            newkey.Close()
            Me.Close()
        End Sub
    End Class

    生成并测试屏幕保护程序

    1. 项目 菜单中上, 单击 MyScreenSaver 属性
    2. MyScreenSaver 属性页 对话框中,单击以选中 Sub Main 中, 启动对象 组合框。
    3. 生成 菜单上单击 生成解决方案
    4. 要测试屏幕保护程序安装程序,请按照下列步骤操作:
      1. 项目 菜单中上, 单击 MyScreenSaver 属性
      2. MyScreenSaver 属性页 中对话框中,单击以选择 Configuration Properties ,然后键入在 命令行参数 文本框,在右窗格中的 / c 。 单击 确定
      3. 按 F 5 键生成并运行该程序。

        在出现 屏幕保护程序安装程序 对话框。
      4. 在消息框中键入 测试屏幕保护程序 ,,然后单击 确定
    5. 要测试屏幕保护应用程序,请按照下列步骤操作:
      1. 项目 菜单中上, 单击 MyScreenSaver 属性
      2. MyScreenSaver 属性页 中对话框中,单击以选择 Configuration Properties ,然后键入在 命令行参数 文本框,在右窗格中的 / s 。 单击 确定
      3. 按 F 5 键生成并运行该程序。

        测试屏幕保护程序 可能会显示 屏幕保护程序 列表中。

    注册屏幕保护程序

    1. MyScreenSaver 应用程序 文件夹的 Bin 文件夹中找到 MyScreenSaver.exe 文件并重命名.exe 文件名称扩展 .scr 。 是例如将 MyScreenSaver.exe 重命名为 MyScreenSaver.scr
    2. 将 MyScreenSaver.scr 文件复制到 Windows 目录。

      MyScreenSaver 选项不 屏幕保护程序 列表中的可用。
    2009年1月28日 10:47
  • 我不知道你怎么会去那里搜索= =|

    另外我估计有点难了

    你会VB吗?

    2009年1月28日 13:06
  • 您好

    微软那个不是有初学者演示吗。。

    直接看源代码。。照着打一份源代码就理解了。。光自己瞎琢磨也不是个办法呀。。呵呵。。


    如果问题解答了请标记为【已解决】,欢迎到我的博客去访问http://www.shangducms.com/blog/uid115.html
    2009年1月30日 4:55
    版主
  • 大哥 我怀疑你是不是从来不把别人主题内容看完
    咱知道你是位热心的试用期员工
    但你别热了我的心
    热错心可是很麻烦的
    我在主题内容里说了下面一句话

    首先我按照VS初学者示例的说明把做好的程序后缀改为SCR
    2009年2月1日 9:30