这是什么
这是我正式上传分享的第一个程序代码,主要用于查看管理高三繁重的作业。
它有什么功能
- 自动下载对应不同选科的不同作业
- 自定义界面样式
- 直观的作业显示
- 附带音乐播放器小程序
程序运行性能要求
项目 | 最低配置 |
---|---|
操作系统 | Windows7及以上 |
CPU | 能开机就可以 |
内存 | 128MB以上 |
硬盘空间 | 剩余100MB以上 |
互联网 | 可用 |
下载软件
你可以在Github的release版块中下载或访问下载地址
一些核心代码
由于这个程序的任务文件使用json格式进行存档,因此读取和保存都需要对json文件进行处理,这里我是用的是newtonsoft.json的引用。
首先建立一个tasklist类,便于主程序调用。
Public Class Tsklist
Private Shared ReadOnly Log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
Public Structure Task '设置任务结构
Dim tskName As String
Dim tskDetail As String
Dim isInfo As Boolean '是否为通知类信息(则无需计算进度)
Dim tskProgress As Long '当前进度
Dim tskTotal As Long '总进度
Dim Files As String '附件
End Structure
Public Structure Subject '设置学科结构
Dim subName As String
Dim subTasks() As Task
Public tskCount As Long
Public Function Count() As Long
Try
Return subTasks.Count
Catch ex As Exception
Log.Error(ex.Message)
Return 0
End Try
End Function
Public Sub AddTask(TaskName As String, isInfo As Boolean, Total As Long, Progress As Long, Optional TaskDetail As String = "", Optional FileURL As String = "")
ReDim Preserve subTasks(Count()) 'Redim确定新数组长度,preserve保存原始数据
With subTasks(Count() - 1)
.tskName = TaskName
.isInfo = isInfo
.tskTotal = Total
.tskProgress = Progress
.tskDetail = TaskDetail
.Files = FileURL
End With
tskCount = Count()
End Sub
End Structure
Public Author As String
Public Subjects() As Subject
Public SubCount As Long
Public Sub New()
Author = ""
SubCount = Count()
End Sub
Public Function Count() As Long
Try
Return Subjects.Count
Catch ex As Exception
Log.Error(ex.Message)
Return 0
End Try
End Function
Public Sub AddSubject(SubName As String)
If SubIndex(SubName) = -1 Then
ReDim Preserve Subjects(Count()) 'Redim确定新数组长度,preserve保存原始数据
Subjects(Count() - 1).subName = SubName
SubCount = Count()
End If
End Sub
Public Function SubIndex(SubName As String) As Long
Dim i As Long
For i = 0 To Count() - 1
If Subjects(i).subName = SubName Then Return i
Next
Return -1
End Function
Public Function Convert2Json() As String
Dim serializer As New JavaScriptSerializer()
Dim result As String = serializer.Serialize(Me)
Return result
End Function
End Class
它的主要功能很简单,无非就是新增任务/学科以及转换为json格式的文件。
接下来是将任务显示在listview控件中,但这里我并没有选择直接显示,而是先增加一个数组存放任务。原因是VB.net的listview控件并不能很好的筛选任务。
一些实现功能的核心代码如下:
1、在数组中增加一项
Public Function AddIntoList(SubjectName As String, TaskName As String, TaskDetail As String, isInfo As Boolean, TaskProgress As Long, TaskTotal As Long, FileURL As String) As Long
Dim item0 As String = (SubjectName)
Dim item1 As String = (TaskName)
Dim item2 As String = (TaskDetail)
Dim item3 As String
If isInfo Then
item3 = ("通知")
Else
item3 = (TaskProgress & "/" & TaskTotal)
Global_Progress += TaskProgress
Global_Total += TaskTotal
End If
Dim a = list_Remains.Items.Add(item0)
a.SubItems.AddRange({item1, item2, item3, FileURL, RemainList_count})
If isInfo Then
a.ForeColor = My.Settings.NotificationColor
a.Font = My.Settings.NotificationFont
a.StateImageIndex = 2
Else
If TaskProgress = TaskTotal Then
a.ForeColor = My.Settings.FinishedColor
a.Font = My.Settings.FinishedFont
a.StateImageIndex = 1
Else
a.ForeColor = My.Settings.UnfinishedColor
a.Font = My.Settings.UnfinishedFont
a.StateImageIndex = 0
End If
End If
ReDim Preserve RemainList(RemainList_count)
With RemainList(RemainList_count)
.GlobalID = RemainList_count
.subject = item0
.name = item1
.detail = item2
.progress = item3
.url = FileURL
.show = True
End With
RemainList_count += 1
Return 0
End Function
2、刷新列表
Public Sub RefreshList()
list_Remains.Items.Clear()
For i = 0 To RemainList.Count - 1
With RemainList(i)
If .show = True Then
Dim a = list_Remains.Items.Add(.subject)
a.SubItems.AddRange({ .name, .detail, .progress, .url, .GlobalID})
If .progress = "通知" Then
a.ForeColor = My.Settings.NotificationColor
a.Font = My.Settings.NotificationFont
a.StateImageIndex = 2
Else
Dim progress() As String
progress = Split(.progress, "/")
If progress(0) = progress(1) Then
a.ForeColor = My.Settings.FinishedColor
a.Font = My.Settings.FinishedFont
a.StateImageIndex = 1
Else
a.ForeColor = My.Settings.UnfinishedColor
a.Font = My.Settings.UnfinishedFont
a.StateImageIndex = 0
End If
End If
End If
End With
Next
End Sub
3、列表排序(来源于网络)
Private Sub List_Remains_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles list_Remains.ColumnClick
' Get the new sorting column.
Dim new_sorting_column As ColumnHeader = sender.Columns(e.Column)
' Figure out the new sorting order.
Dim sort_order As System.Windows.Forms.SortOrder
If m_SortingColumn Is Nothing Then
' New column. Sort ascending.
sort_order = SortOrder.Ascending
Else ' See if this is the same column.
If new_sorting_column.Equals(m_SortingColumn) Then
' Same column. Switch the sort order.
If m_SortingColumn.Text.EndsWith("▲") Then
sort_order = SortOrder.Descending
Else
sort_order = SortOrder.Ascending
End If
Else
' New column. Sort ascending.
sort_order = SortOrder.Ascending
End If
' Remove the old sort indicator.
m_SortingColumn.Text = m_SortingColumn.Text.Substring(0, m_SortingColumn.Text.Length - 1)
End If
' Display the new sort order.
m_SortingColumn = new_sorting_column
If sort_order = SortOrder.Ascending Then
m_SortingColumn.Text &= "▲"
Else
m_SortingColumn.Text &= "▼"
End If
' Create a comparer.
sender.ListViewItemSorter = New ClsListviewSorter(e.Column, sort_order)
' Sort.
sender.Sort()
End Sub
4、列表搜索
4.1、将输入框中的字符转换为小写的拼音
Public Function ToPinyin(ByVal chars As String) As String
Dim pinyin As String = ""
Dim charArray() As Char = chars.ToCharArray
Dim frmt As hyjiacan.py4n.PinyinFormat = hyjiacan.py4n.PinyinFormat.WITHOUT_TONE Or hyjiacan.py4n.PinyinFormat.LOWERCASE Or hyjiacan.py4n.PinyinFormat.WITH_V
For Each c As Char In charArray
If hyjiacan.py4n.PinyinUtil.IsHanzi(c) Then
pinyin += hyjiacan.py4n.Pinyin4Net.GetPinyin(c, frmt)(0)
Else
pinyin += c
End If
Next
Return pinyin
End Function
4.2、搜索列表中的项目
Private Sub ListSearch(Text As String)
Try
For i As Integer = 0 To RemainList.Count - 1
Dim oristr As String = RemainList(i).subject + RemainList(i).name + RemainList(i).detail + RemainList(i).progress + RemainList(i).url
Dim nowstr As String = ToPinyin(oristr)
If InStr(nowstr.ToLower, Text.ToLower) = 0 Then
RemainList(i).show = False
Else
RemainList(i).show = True
End If
If CheckBox5.Checked = False And RemainList(i).progress = "通知" Then
RemainList(i).show = False
Else
If CheckBox6.Checked = False And RemainList(i).progress <> "通知" Then
If Split(RemainList(i).progress, "/")(0) = Split(RemainList(i).progress, "/")(1) Then
RemainList(i).show = False
End If
End If
End If
RefreshList()
Next
Catch ex As Exception
Log.Error(ex.Message)
End Try
End Sub
4.3、刷新列表(这在初始化中也会用到)
Public Sub RefreshList()
list_Remains.Items.Clear()
For i = 0 To RemainList.Count - 1
With RemainList(i)
If .show = True Then
Dim a = list_Remains.Items.Add(.subject)
a.SubItems.AddRange({ .name, .detail, .progress, .url, .GlobalID})
If .progress = "通知" Then
a.ForeColor = My.Settings.NotificationColor
a.Font = My.Settings.NotificationFont
a.StateImageIndex = 2
Else
Dim progress() As String
progress = Split(.progress, "/")
If progress(0) = progress(1) Then
a.ForeColor = My.Settings.FinishedColor
a.Font = My.Settings.FinishedFont
a.StateImageIndex = 1
Else
a.ForeColor = My.Settings.UnfinishedColor
a.Font = My.Settings.UnfinishedFont
a.StateImageIndex = 0
End If
End If
End If
End With
Next
End Sub
5、一些细节上的优化
例如使用GC.Collect()
回收进程,这会防止你的程序内存溢出。
其次,一些显示效果上的优化,下面的代码可以使你的控件自动调整大小以自适应屏幕分辨率。
Private Sub FrmLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
x = Me.Width
y = Me.Height
SetTag(Me)
End Sub
'递归取控件的原始大小和位置,用tag来纪录
Private Sub SetTag(ByVal obj As Object)
For Each con As Control In obj.Controls
'Application.DoEvents()
con.Tag = con.Width & ":" & con.Height & ":" & con.Left & ":" & con.Top & ":" & con.Font.Size
'如果是容器控件,则递归继续纪录
If con.Controls.Count > 0 Then
SetTag(con)
End If
Next
End Sub
'递归重新设定控件的大小和位置
Private Sub SetControls(ByVal newx As Single, ByVal newy As Single, ByVal obj As Object)
For Each con As Control In obj.Controls
'Application.DoEvents()
'Try
con.AutoSize = False
Dim mytag() As String = con.Tag.ToString.Split(":")
con.Width = mytag(0) * newx
con.Height = mytag(1) * newy
con.Left = mytag(2) * newx
con.Top = mytag(3) * newy
con.AutoSize = con.AutoSize
'计算字体缩放比例,缩放字体
Dim currentSize As Single = (mytag(1) * newy * mytag(4)) / mytag(1)
con.Font = New Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit)
'如果是容器控件,则递归继续缩放
If con.Controls.Count > 0 Then
SetControls(newx, newy, con)
End If
'Catch ex As Exception
' Log.Warn(ex.Message)
'End Try
Next
End Sub
Private Sub FrmResize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
'得到现在窗体的大小,然后根据原始大小计算缩放比例
'Application.DoEvents()
Dim newx As Single = Me.Width / x
Dim newy As Single = Me.Height / y
SetControls(newx, newy, Me)
End Sub
尾声
总的来说,这个程序在某种意义上是我第一个完全使用Visual Studio编写的程序了,问题还是有不少的。
至于任务存储使用json而不用数据库存储的原因,是因为我懒得改了╮(╯▽╰)╭。
最后祝大家身体健康,再见。