# 祝你早日脱单 **Repository Path**: Dreamelf/CSharpGetLovePrograms ## Basic Information - **Project Name**: 祝你早日脱单 - **Description**: 得到爱情程序黑客情人节特供版 - **Primary Language**: C# - **License**: WTFPL - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-05-21 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 祝你早日脱单 设计思路(笔记) ## 执行流程 1. 启动界面 2. 五秒后载入窗口2,黑色背景全屏 3. 从数组中按照顺序抽取文本并按顺序准备显示 4. 每条文本至少显示5秒,点击继续 5. 结束后自我销毁 ## 全屏代码: ``` c# this.FormBorderStyle = FormBorderStyle.None; //设置窗体为无边框样式 this.WindowState = FormWindowState.Maximized; //最大化窗体 ``` ## 打字效果实现思路: ``` c# public void writeStr(string str){ char[] chs = str.toCharArray(); foreach(char item in chs){ textBox.text += item; thread.sleep(0.3) //延迟时间 } } ``` 但因为C#程序窗口渲染的尿性,大概有以下解决方案 ``` c# public char[] txtChars; public char[] writeStr(char[] chs){ textBox.Text += chs[0]; char[] NewChs = new char[chs.length - 1]; for(int i = 0;i < NewChs.length;i++){ //丢弃chs[0],复制数组 NewChs[i] = chs[i + 1]; /* 这里是不会溢出的,因为: 新数组.长度=旧数组.长度-1 故i必小于chs.length 即i最大值(最后一次循环)必为chs的最后一个元素 */ } return NewChs; } private void timer_tick(...){//调用,启用时钟前在txtChars放入要显示的话 if(txtChars.length == 0){//文本队列被清空后,停止输出。 timer.Enabled = false; return ; } txtChars = writeStr(txtChars); } ``` 看上去有点复杂,应该还有一个更简单的办法。 我们可以使用`BackgroundWork`控件来实现这个功能。首先为了能够更改窗口控件,需要禁用编译器的线程安全检查 **在实际编程中请遵循线程安全原则,因为这是个小程序我懒得折腾那么多,折腾麻烦了大部分小白就跑了** 禁用线程安全: ``` c# Control.CheckForIllegalCrossThreadCalls = false; ```