上一节主要讲了数据库的数据访问层,逻辑业务层以及贯穿始终的模型层从而访问数据源。这届通过表示层来讲述C#中WPF界面的设计以及表示层如何与逻辑业务层互相传递数据。 界面主要有如下七个:登录界面;个人主页;修改个人信息界面;头像选择界面;聊天窗口界面;陌生人查询界面;注册个人信息界面
实现功能:账号密码的验证(提示错误或跳转到个人主页),登录,退出登录,注册账号(跳转到注册界面),以及顶部动画的显示 编辑界面代码:
后台代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Data; using System.Data.SqlClient; using BLL; using System.Windows.Media.Animation;
namespace MyQQTest5 { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); }
private void Label_MouseEnter(object sender, MouseEventArgs e) { RegisterButton.Foreground = Brushes.Blue; } private void Label_MouseLeave(object sender, MouseEventArgs e) { RegisterButton.Foreground = Brushes.Red; } private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Register rs = new Register(); rs.ShowDialog(); } private void Button_Click(object sender, RoutedEventArgs e) { try { if (um.CheckLogin(int.Parse(IdText.Text), PwdText.Password) == 1) { UserView uv = new UserView(); uv.id = int.Parse(IdText.Text); this.Close(); uv.ShowDialog(); } else { MessageBox.Show("账号或密码不正确!请重新输入!"); PwdText.Clear(); } } catch (Exception) { MessageBox.Show("账号或密码格式不正确!"); IdText.Clear(); } } UserManager um = new UserManager(); /// <summary> /// 用户登录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Window_Loaded(object sender, RoutedEventArgs e) { RotateTransform rt = new RotateTransform(); Leaf.RenderTransformOrigin = new Point(0,0); Leaf.RenderTransform = rt; DoubleAnimation da = new DoubleAnimation(); da.Duration = new Duration(TimeSpan.FromSeconds(2)); da.From = 45; da.To = 0; da.RepeatBehavior = RepeatBehavior.Forever; da.AutoReverse = true; Storyboard sb = new Storyboard(); sb.Children.Add(da); Storyboard.SetTarget(da, Leaf); Storyboard.SetTargetProperty(da, new PropertyPath("RenderTransform.Angle")); sb.Begin(); DoubleAnimation da1 = new DoubleAnimation(); da1.Duration = new Duration(TimeSpan.FromSeconds(8)); da1.RepeatBehavior = RepeatBehavior.Forever; da1.From = -550; da1.To = 550; Storyboard sb1 = new Storyboard(); sb1.Children.Add(da1); Storyboard.SetTarget(da1, Cloud); Storyboard.SetTargetProperty(da1, new PropertyPath("(Canvas.Left)")); DoubleAnimation da2 = new DoubleAnimation(); da2.Duration = new Duration(TimeSpan.FromSeconds(8)); da2.RepeatBehavior = RepeatBehavior.Forever; da2.From = 1; da2.To = 0; sb1.Children.Add(da2); Storyboard.SetTarget(da2,Cloud); Storyboard.SetTargetProperty(da2,new PropertyPath("Opacity")); sb1.Begin(); } private void RegisterButton_Copy_MouseEnter(object sender, MouseEventArgs e) { RegisterButton_Copy.Foreground = Brushes.Black; } private void RegisterButton_Copy_MouseLeave(object sender, MouseEventArgs e) { RegisterButton_Copy.Foreground = Brushes.Green; } private void RegisterButton_Copy_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.Close(); }}}
实现功能:好友的相应信息显示(账号,性别,昵称等),个人的相应信息显示,搜索陌生人(跳转到搜索界面),修改个人资料(跳转到修改个人信息界面),好友消息提醒并向好友发送消息(跳转到聊天窗口界面) 编辑界面代码:
后台代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Data; using System.Data.SqlClient; using BLL; using System.Windows.Threading;
namespace MyQQTest5 { /// /// UserView.xaml 的交互逻辑 /// public partial class UserView : Window { public UserView() { InitializeComponent(); } public int id; UserManager um = new UserManager(); StackPanel[] spAll = new StackPanel[3]; Random r = new Random(); DispatcherTimer t1 = new DispatcherTimer(); private void Window_Loaded(object sender, RoutedEventArgs e) { t1.Interval = TimeSpan.FromMilliseconds(600); t1.Tick += t1_Tick; DataTable dt = um.FindData(id); picTitle.Source = new BitmapImage(new Uri(“image/hpic/” + (int)dt.Rows[0][“faceid”] + “.bmp”, UriKind.Relative)); nickTitle.Content = dt.Rows[0][“nickname”].ToString(); nameTitle.Content = dt.Rows[0][“age”].ToString(); sexTitle.Content = dt.Rows[0][“sex”].ToString(); for (int i = 0; i < 3; i++) { spAll[i] = new StackPanel(); spAll[i].Orientation = Orientation.Vertical; } try { DataTable dt1 = um.FindFamily(id); FindList(dt1, 0); } catch (Exception) { } MyFamily.Content = spAll[0]; try { DataTable dt1 = um.FindFriends(id); FindList(dt1, 1); } catch (Exception) {
} MyFriend.Content = spAll[1]; try { DataTable dt1 = um.FindPeople(id); FindList(dt1, 2); } catch (Exception) { } MyPeople.Content = spAll[2]; } int x = 0; void t1_Tick(object sender, EventArgs e) { for (int i = 0; i < 3; i++) { foreach (UIElement sp in spAll[i].Children) { if (sp.GetType().Name == "StackPanel") { foreach (UIElement lb in ((StackPanel)sp).Children) { if (lb.GetType().Name == "Label") { if (((Label)lb).Tag.ToString() == "hasNews") { if (x++ == 0) { ((Label)lb).Background = Brushes.Red; } else { x = 0; ((Label)lb).Background = Brushes.Blue; } } } } } } } } private void FindList(DataTable dt1, int j) { for (int i = 0; ; i++) { StackPanel sp = new StackPanel(); sp.Orientation = Orientation.Horizontal; Image img = new Image(); img.Width = img.Height = 60; img.Source = new BitmapImage(new Uri("image/hpic/" + (int)dt1.Rows[i]["faceid"] + ".bmp", UriKind.Relative)); Label label = new Label(); label.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center; label.VerticalContentAlignment = System.Windows.VerticalAlignment.Center; label.FontSize = 20; label.Background = new SolidColorBrush(Color.FromRgb((byte)r.Next(0, 256), (byte)r.Next(0, 256), (byte)r.Next(0, 256))); label.Height = 60; label.Width = 200; label.Content = dt1.Rows[i]["nickname"].ToString(); sp.Orientation = Orientation.Horizontal; sp.Children.Add(img); sp.Children.Add(label); spAll[j].Children.Add(sp); sp.Tag = (int)dt1.Rows[i]["id"]; if (um.IsNews((int)sp.Tag, id) >= 1) { label.Tag = "hasNews"; t1.Start(); } sp.MouseLeftButtonDown += sp_MouseLeftButtonDown; } } void sp_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { StackPanel sp = sender as StackPanel; foreach (UIElement lb in sp.Children) { if (lb.GetType().Name == "Label") { if (((Label)lb).Tag.ToString() == "hasNews") { ((Label)lb).Tag = "noNews"; } } } MessageSender ms = new MessageSender(); ms.userid = id; ms.friendid = (int)sp.Tag; ms.ShowDialog(); } private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Update up = new Update(); up.id = id; up.uv = this; up.ShowDialog(); } private void Label_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e) { FindQQ fq = new FindQQ(); fq.ShowDialog(); }}}
实现功能:显示并修改个人头像(跳转到头像选择界面),必填信息和可选信息的完善 编辑界面代码:
后台代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Data; using System.Data.SqlClient; using BLL; using Model;
namespace MyQQTest5 { /// /// Update.xaml 的交互逻辑 /// public partial class Update : Window { public Update() { InitializeComponent(); } UserManager um = new UserManager(); /// /// 选择图片 /// /// /// private void FaceImg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { FaceImg fi = new FaceImg(); fi.up = this; fi.ShowDialog(); } int faceId = 1; public UserView uv; /// /// 选择图片 /// /// public void ChangeImg(int num) { faceId = num; facePic.Source = new BitmapImage(new Uri(“image/hpic/” + num + “.bmp”, UriKind.Relative)); } //加载事件 private void Window_Loaded(object sender, RoutedEventArgs e) { DataTable dt = um.FindData(id); foreach (DataRow r in um.StarTable().Rows) { starBox.Items.Add(r[“star”]); } starBox.SelectedIndex = (int)dt.Rows[0][“starid”] - 1; foreach (DataRow r in um.BloodTypeTable().Rows) { BloodBox.Items.Add(r[“BloodType”]); } BloodBox.SelectedIndex = (int)dt.Rows[0][“bloodtypeid”] - 1; foreach (DataRow r in um.FriednshpPolicyTable().Rows) { FirendshipBox.Items.Add(r[“friendshippolicy”]); } FirendshipBox.SelectedIndex = (int)dt.Rows[0][“friendshippolicyid”] - 1; nickText.Text = dt.Rows[0][“nickname”].ToString(); if (dt.Rows[0][“sex”].ToString() == “女”) { GirlCh.IsChecked = true; } else BoyCh.IsChecked = true; ageText.Text = dt.Rows[0][“age”].ToString(); pwdText1.Password = dt.Rows[0][“loginpwd”].ToString(); pwdText2.Password = dt.Rows[0][“loginpwd”].ToString(); nameText.Text = dt.Rows[0][“name”].ToString(); ChangeImg((int)dt.Rows[0][“faceid”]); } User user = new User(); public int id; private void Button_Click(object sender, RoutedEventArgs e) { try { if (pwdText1.Password == pwdText2.Password) { user.Sex1 = GirlCh.IsChecked == true ? “女” : “男”; user.Starid1 = starBox.SelectedIndex + 1; user.BloodTypeid1 = BloodBox.SelectedIndex + 1; user.FriendshipPolicyid1 = FirendshipBox.SelectedIndex+1; user.LoginPwd1 = pwdText1.Password; user.Age1 = int.Parse(ageText.Text); user.Faceid1 = faceId; user.Name1 = nameText.Text; user.NickName1 = nickText.Text; if (um.IsChanged(user, id) == 1) { if (MessageBox.Show(“确认修改吗?”, “温馨提示”, MessageBoxButton.OKCancel, MessageBoxImage.Asterisk) == MessageBoxResult.OK) { uv.picTitle.Source = new BitmapImage(new Uri(“image/hpic/” + faceId + “.bmp”, UriKind.Relative)); ; uv.nickTitle.Content = nickText.Text; uv.sexTitle.Content = GirlCh.IsChecked == true ? “女” : “男”; uv.nameTitle.Content = ageText.Text; this.Close(); } } } else MessageBox.Show(“两次密码不一致,请重新输入!”); } catch (Exception) { throw; //MessageBox.Show(“请将信息填写完整并且格式正确!”); } } //取消注册 private void Button_Click_1(object sender, RoutedEventArgs e) { this.Close(); } } }
头像选择界面 实现功能:头像的浏览与选择 编辑界面代码:
后台代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Data; using System.Data.SqlClient;
namespace MyQQTest5 { /// /// FaceImg.xaml 的交互逻辑 /// public partial class FaceImg : Window { public FaceImg() { InitializeComponent(); }
private void Window_Loaded(object sender, RoutedEventArgs e) { int TotalImg = 100; for (int i = 1; i < TotalImg + 1; i++) { Border br = new Border(); br.Width = br.Height = 90; br.BorderThickness = new Thickness(5); br.BorderBrush = Brushes.Black; Image img = new Image(); img.Source = new BitmapImage(new Uri("image/hpic/" + i + ".bmp", UriKind.Relative)); img.Tag = i; br.Child = img; back.Children.Add(br); img.MouseLeftButtonDown += img_MouseLeftButtonDown; } } public Register rs; public Update up; void img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Image img = sender as Image; try { rs.faceId = (int)img.Tag; rs.ChoosePic(rs.faceId); } catch (Exception) { up.ChangeImg((int)img.Tag); } this.Close(); }}}
实现功能:显示好友个人信息,显示双方聊天信息,发送消息,实现广告窗口的抖动效果 编辑界面代码:
后台代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Data; using System.Data.SqlClient; using System.Windows.Threading; using System.Threading; using BLL;
namespace MyQQTest5 { /// /// MessageSender.xaml 的交互逻辑 /// public partial class MessageSender : Window { public MessageSender() { InitializeComponent(); } public int userid; public int friendid; UserManager um = new UserManager(); DispatcherTimer t1 = new DispatcherTimer(); DispatcherTimer t2 = new DispatcherTimer(); DispatcherTimer t3 = new DispatcherTimer();
private void Window_Loaded(object sender, RoutedEventArgs e) { t1.Tick += t1_Tick; t1.Interval = TimeSpan.FromMilliseconds(40); t1.Start(); FriendPic.Source = new BitmapImage(new Uri("image/hpic/" + (int)um.FindData(friendid).Rows[0]["faceid"] + ".bmp", UriKind.Relative)); t2.Interval = TimeSpan.FromMilliseconds(100); t2.Tick += t2_Tick; t2.Start(); t3.Tick += t3_Tick; t3.Interval = TimeSpan.FromMilliseconds(40); } int k = 0; int count = 0; void t3_Tick(object sender, EventArgs e) { switch (k) { case 0: Canvas.SetLeft(textBox2, Canvas.GetLeft(textBox2) + 2); k++; break; case 1: Canvas.SetTop(textBox2, Canvas.GetTop(textBox2) + 2); k++; break; case 2: Canvas.SetLeft(textBox2, Canvas.GetLeft(textBox2) - 2); k++; break; case 3: Canvas.SetTop(textBox2, Canvas.GetTop(textBox2) - 2); k = 0; break; default: break; } if (count++ > 30) { count = 0; k = 0; t3.Stop(); } } void t2_Tick(object sender, EventArgs e) { DataTable dt = um.FindMessage(friendid, userid); try { for (int i = 0; ; i++) { if ((int)dt.Rows[i]["messagetypeid"] == 1) { textBox2.Text = textBox2.Text + dt.Rows[i]["message"] + "/n"; } else if ((int)dt.Rows[i]["messagetypeid"] == 2) { textBox2.Text = textBox2.Text + dt.Rows[i]["message"] + "/n"; if (MessageBox.Show("是否添加对方为好友?", "温馨提示", MessageBoxButton.OKCancel, MessageBoxImage.Information) == MessageBoxResult.OK) { if (um.IsFriend(userid, friendid) >= 1) { MessageBox.Show("你们已经是好友了!无需添加"); } else { um.AddFriends(userid, friendid); t3.Start(); } } } else if ((int)dt.Rows[i]["messagetypeid"] == 3) { textBox2.Text = textBox2.Text + dt.Rows[i]["message"] + "/n"; } } } catch (Exception) { try { um.DeleteMessages(friendid, userid); } catch (Exception) { } } } int i = 0; void t1_Tick(object sender, EventArgs e) { switch (i) { case 0: Canvas.SetLeft(ad, Canvas.GetLeft(ad) + 3); i++; break; case 1: Canvas.SetTop(ad, Canvas.GetTop(ad) + 3); i++; break; case 2: Canvas.SetLeft(ad, Canvas.GetLeft(ad) - 3); i++; break; case 3: Canvas.SetTop(ad, Canvas.GetTop(ad) - 3); i = 0; break; default: break; } } string MsContent; /// <summary> /// 震动消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { MsContent = um.FindData(userid).Rows[0]["nickname"].ToString() + " " + DateTime.Now + "/n" + ">>窗口抖动<<"; um.AddMessage(MsContent, userid, friendid, 3); textBox2.Text = textBox2.Text + MsContent + "/n"; } /// <summary> /// 加好友消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Image_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e) { if (um.IsFriend(userid, friendid) >= 1) { MessageBox.Show("你们已经是好友了!无需添加"); } else { um.AddFriends(userid, friendid); MsContent = um.FindData(userid).Rows[0]["nickname"].ToString() + " " + DateTime.Now + "/n" + ">>好友验证消息<<"; um.AddMessage(MsContent, userid, friendid, 2); textBox2.Text = textBox2.Text + MsContent + "/n"; } } /// <summary> /// 发文字消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Image_MouseLeftButtonDown_2(object sender, MouseButtonEventArgs e) { if (textBox1.Text != "") { MsContent = um.FindData(userid).Rows[0]["nickname"].ToString() + " " + DateTime.Now + "/n" + textBox1.Text; um.AddMessage(MsContent, userid, friendid, 1); textBox2.Text = textBox2.Text + MsContent + "/n"; textBox1.Clear(); } else MessageBox.Show("你倒是输入聊天信息啊!"); }}}
实现功能:按照QQ号码部分位数进行模糊查询 编辑界面代码:
后台代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Data; using System.Data.SqlClient; using BLL;
namespace MyQQTest5 { /// /// FindQQ.xaml 的交互逻辑 /// public partial class FindQQ : Window { public FindQQ() { InitializeComponent(); } UserManager um = new UserManager(); private void Button_Click(object sender, RoutedEventArgs e) { for (int i = sp.Children.Count-1; i >=0 ; i–) { sp.Children.Remove(sp.Children[i]); } try { sv.Visibility = System.Windows.Visibility.Visible; for (int i = 0; ; i++) { Label lb = new Label(); lb.Content = um.FindIt(int.Parse(simpleData.Text)).Rows[i][“nickname”] +”(“+ um.FindIt(int.Parse(simpleData.Text)).Rows[i][“id”]+”)”; lb.Width = 280; lb.Background = Brushes.Cyan; sp.Children.Add(lb); } } catch (Exception) {
} } private void Button_Click_1(object sender, RoutedEventArgs e) { sv.Visibility = System.Windows.Visibility.Hidden; simpleData.Clear(); }}}
实现功能:注册个人信息,提交个人信息,头像选择(跳转到头像选择界面) 编辑界面代码:
后台代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Data; using System.Data.SqlClient; using BLL; using Model;
namespace MyQQTest5 { /// /// Register.xaml 的交互逻辑 /// public partial class Register : Window { public Register() { InitializeComponent(); } UserManager um = new UserManager(); User user = new User(); private void Window_Loaded(object sender, RoutedEventArgs e) { DataTable dt = um.StarTable(); DataTable dt1 = um.BloodTypeTable(); DataTable dt2 = um.FriednshpPolicyTable(); foreach (DataRow r in dt.Rows) { starBox.Items.Add(r[“star”]); } starBox.SelectedIndex = 0; foreach (DataRow r in dt1.Rows) { BloodBox.Items.Add(r[“bloodtype”]); } BloodBox.SelectedIndex = 0; foreach (DataRow r in dt2.Rows) { FirendshipBox.Items.Add(r[“friendshippolicy”]); } FirendshipBox.SelectedIndex = 0; ChoosePic(faceId); }
public void ChoosePic(int faceid) { faceCh.Source = new BitmapImage(new Uri("image/hpic/" + faceId + ".bmp", UriKind.Relative)); } private void Button_Click(object sender, RoutedEventArgs e) { try { if (pwdText1.Password==pwdText2.Password) { user.LoginPwd1 = pwdText1.Password; user.Name1 = nameText.Text; user.NickName1 = nickText.Text; user.Age1 = int.Parse(ageText.Text); user.Faceid1 = faceId; user.Starid1 = starBox.SelectedIndex + 1; user.BloodTypeid1 = BloodBox.SelectedIndex + 1; user.FriendshipPolicyid1 = FirendshipBox.SelectedIndex + 1; user.Sex1=BoyCh.IsChecked==true?"男":"女"; if (um.RegisterQQ(user)==1) { if (MessageBox.Show("是否确认注册?","温馨提示",MessageBoxButton.OKCancel,MessageBoxImage.Question)==MessageBoxResult.OK) { MessageBox.Show("恭喜你获得靓号,Id为"+um.FindId()); this.Close(); } } } else { MessageBox.Show("两次密码不一致,请重新输入!"); pwdText1.Clear(); pwdText2.Clear(); } } catch (Exception) { MessageBox.Show("请将信息填写完整并且格式正确!"); } } private void Button_Click_1(object sender, RoutedEventArgs e) { this.Close(); } public int faceId=1; private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { FaceImg Fi = new FaceImg(); Fi.rs=this; Fi.ShowDialog(); }}} 资源下载地址:http://pan.baidu.com/s/1i5Fmsi5
新闻热点
疑难解答