C# 桌面宠物 示例源码(透明窗体)

时间:2024-04-17 17:16:07

可以按时叫你吃饭可以控制在桌面上行走等等等等。。。。。。。。。。。。

【实例截图】

 

 

【核心代码】

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SpeechLib;
using System.Threading;
namespace 不做单身狗__桌面宠物
{
    public partial class cw : Form
    {
 
        private string a;
        bool run = false,bf=false ;
        SoundPlayer zl;
        Form1 f = new Form1();
        public cw(string str)
        {
            InitializeComponent();
            this.a = str;
            TransparencyKey = BackColor;
            pictureBox1.Focus();
        }//构造函数
        private void cw_Load(object sender, EventArgs e)//加载宠物
        {
            zheng();
 
                ts.Start();
             
             
        }
        #region 宠物动作 
        public void qian()
        {
           
                if (run == false)
                {
                    System.Reflection.Assembly assembly;
 
                    assembly = System.Reflection.Assembly.GetExecutingAssembly();
                    zl = new SoundPlayer(Properties.Resources.走路);
                    zl.PlayLooping();//循环播放
                    if (a == "男1") { pictureBox1.Image = Properties.Resources.男1前; }
                    if (a == "男2") { pictureBox1.Image = Properties.Resources.男2前; }
                    if (a == "男3") { pictureBox1.Image = Properties.Resources.男3前; }
                    if (a == "女1") { pictureBox1.Image = Properties.Resources.女1前; }
                    if (a == "女2") { pictureBox1.Image = Properties.Resources.女2前; }
                    if (a == "女3") { pictureBox1.Image = Properties.Resources.女3前; }
                    run = true;
                }
 
              //pictureBox1.Top = pictureBox1.Top -1;
            panel1.Top = panel1.Top -1;
 
        }
        public void hou()
        {
 
                if (run == false)
                {
                    System.Reflection.Assembly assembly;
                    assembly = System.Reflection.Assembly.GetExecutingAssembly();
                    zl = new SoundPlayer(Properties.Resources.走路);
                    zl.PlayLooping();//循环播放
                    bf = true;
                    if (a == "男1") { pictureBox1.Image = Properties.Resources.男1后; }
                    if (a == "男2") { pictureBox1.Image = Properties.Resources.男2后; }
                    if (a == "男3") { pictureBox1.Image = Properties.Resources.男3后; }
                    if (a == "女1") { pictureBox1.Image = Properties.Resources.女1后; }
                    if (a == "女2") { pictureBox1.Image = Properties.Resources.女2后; }
                    if (a == "女3") { pictureBox1.Image = Properties.Resources.女3后; }
 
                    run = true;
                }
 
              //pictureBox1.Top = pictureBox1.Top   1;
            panel1.Top =panel1 .Top 1;
         }
        public void zuo()
        {
 
                if (run == false)
                {
                    System.Reflection.Assembly assembly;
 
                    assembly = System.Reflection.Assembly.GetExecutingAssembly();
                    zl = new SoundPlayer(Properties.Resources.走路);
                    zl.PlayLooping();//循环播放
                    bf = true;
                    if (a == "男1") { pictureBox1.Image = Properties.Resources.男1左; }
                    if (a == "男2") { pictureBox1.Image = Properties.Resources.男2左; }
                    if (a == "男3") { pictureBox1.Image = Properties.Resources.男3左; }
                    if (a == "女1") { pictureBox1.Image = Properties.Resources.女1左; }
                    if (a == "女2") { pictureBox1.Image = Properties.Resources.女2左; }
                    if (a == "女3") { pictureBox1.Image = Properties.Resources.女3左; }
 
                    run = true;
                }
 
              //pictureBox1.Left = pictureBox1.Left - 1;
            panel1.Left = panel1.Left - 1;
        }
        public void you()
        {
 
                if (run == false)
                {
                    System.Reflection.Assembly assembly;
 
                    assembly = System.Reflection.Assembly.GetExecutingAssembly();
                    zl = new SoundPlayer(Properties.Resources.走路);
                    zl.PlayLooping();//循环播放
                    bf = true;
                    if (a == "男1") { pictureBox1.Image = Properties.Resources.男1右; }
                    if (a == "男2") { pictureBox1.Image = Properties.Resources.男2右; }
                    if (a == "男3") { pictureBox1.Image = Properties.Resources.男3右; }
                    if (a == "女1") { pictureBox1.Image = Properties.Resources.女1右; }
                    if (a == "女2") { pictureBox1.Image = Properties.Resources.女2右; }
                    if (a == "女3") { pictureBox1.Image = Properties.Resources.女3右; }
                    run = true;
                }
 
            //pictureBox1.Left = pictureBox1.Left  1;
            panel1.Left = panel1.Left   1;
        }
        public void zheng()
        {
            if (a == "男1") { pictureBox1.Image = Properties.Resources.男1正; }
            if (a == "男2") { pictureBox1.Image = Properties.Resources.男2正; }
            if (a == "男3") { pictureBox1.Image = Properties.Resources.男3正; }
            if (a == "女1") { pictureBox1.Image = Properties.Resources.女1正; }
            if (a == "女2") { pictureBox1.Image = Properties.Resources.女2正; }
            if (a == "女3") { pictureBox1.Image = Properties.Resources.女3正; }
             
        }
        public void fa()
        {
 
                if (run == false)
                {
                    System.Reflection.Assembly assembly;
                    assembly = System.Reflection.Assembly.GetExecutingAssembly();
                    zl = new SoundPlayer(Properties.Resources.Q);
                    zl.PlayLooping();//循环播放
                    bf = true;
                    if (a == "男1") { pictureBox1.Image = Properties.Resources.男1法; }
                    if (a == "男2") { pictureBox1.Image = Properties.Resources.男2法; }
                    if (a == "男3") { pictureBox1.Image = Properties.Resources.男3法; }
                    if (a == "女1") { pictureBox1.Image = Properties.Resources.女1法; }
                    if (a == "女2") { pictureBox1.Image = Properties.Resources.女2法; }
                    if (a == "女3") { pictureBox1.Image = Properties.Resources.女3法; }
                    run = true;
                 }               
        }
        #endregion
 
        #region 拖动宠物
        //是否正在拖拽
        bool isDrag = false;
        //鼠标按下坐标(control控件的相对坐标)
        Point mouseDownPoint = Point.Empty;
        //将被拖动的控件
        private Control control;
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrag = true;
            control = panel1; //pictureBox1;
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint = panel1.Location;
            }
        }
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrag)
            {
                Point mousePos = new Point(control.Location.X, control.Location.Y);
                mousePos.Offset(e.X, e.Y);
                mousePos.X -= this.control.Width / 7;
                mousePos.Y -= this.control.Height / 7;
                control.Location = mousePos;
            }
        }
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (isDrag)
            {
                isDrag = false;
            }
        }
        #endregion
        private void cw_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)//执行动作
        {
            if (e.KeyValue == 87) { qian(); }
            if (e.KeyValue == 83) { hou (); }
            if (e.KeyValue == 65) { zuo (); }
            if (e.KeyValue == 68) { you (); }
            if (e.KeyValue == 81) { fa  (); }
            if (e.KeyValue == 27) {Application.Exit();ts.Abort(); }
        }
        private void cw_KeyUp(object sender, KeyEventArgs e)//结束动作
        {
            zheng();
            if (bf == true)
            { zl.Stop();}
             
            run = false ;
        }
        Thread ts = new Thread(Function.Ts);//按时提示功能
         
        private void pictureBox1_DoubleClick(object sender, EventArgs e)//对话
        {
            SpVoice voice = new SpVoice();
            voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);
            voice.Speak("亲爱的,好期待和您讲话啊!再耐心等等吧!等我爸爸给我这个功能!", SpeechVoiceSpeakFlags.SVSFDefault);
             
 
             
 
        }
 
 
    }
}