1、配置文件L:App.config
2、启动界面:MainWindow.xaml
3、MainWindow.xaml.cs
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;
namespace GuessClient
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void StartWindow(string username, int left, int top)
{
GuessWindow w = new GuessWindow();
w.Left = left;
w.Top = top;
w.UserName = username;
w.Owner = this;
w.Show();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
StartWindow("西西", 100, 100);
StartWindow("瓜瓜", 500, 200);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
GuessWindow w = new GuessWindow();
w.WindowStartupLocation = WindowStartupLocation.CenterScreen;
w.Owner = this;
w.Show();
}
}
}
4、程序主页面窗体:GuessWindow.xaml
5、GuessWindow.xaml.cs
using GuessClient.ServiceReference1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.ServiceModel;
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.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace GuessClient
{
///
/// GuessWindow.xaml 的交互逻辑
///
public partial class GuessWindow : Window, IService1Callback
{
public GuessWindow()
{
InitializeComponent();
this.Closing += GuessWindow_Closing;
}
private void GuessWindow_Closing(object sender, CancelEventArgs e) //退出系统
{
if (client != null)
{
client.Logout(UserName);
client = null;
//client.Close();
}
}
private Service1Client client; //连接服务器
public string UserName //该用户的用户名
{
get { return this.username.Text; } //获取用户名框的内容
set { this.username.Text = value; }
}
private void login_Click(object sender, RoutedEventArgs e) //登录按钮事件
{
InstanceContext context = new InstanceContext(this);
client = new Service1Client(context);
try
{
if (this.username.Text != "")
{
client.Login(this.username.Text);
}
else
{
MessageBox.Show("用户名不能为空!");
}
}
catch (Exception ex)
{
MessageBox.Show("与服务器连接失败:" + ex.Message);
return;
}
}
private void AddMessage(string str) //在大厅发言,消息加入消息列表
{
TextBlock t = new TextBlock();
t.Text = str;
t.FontSize = 15;
this.messagelist.Items.Add(t);
}
public void InitRoomUsersInfo(string RoomUsersInfo, string ink, bool hua_or_cai, string timu) //初始化新进房间用户的界面
{
this.game_message.Items.Clear();
if (RoomUsersInfo.Length == 0) //等于0说明是第一个进入房间的,担当画画角色
{
this.guess_message.IsEnabled = false;
this.guess_messagesend.IsEnabled = false;
this.timu.Text = timu;
return;
}
this.guess_message.IsEnabled = true;
this.guess_messagesend.IsEnabled = true;
this.tool_Color.IsEnabled = false;
this.tool_Clear.IsEnabled = false;
//不等于0,不是第一个进入房间的,猜
//初始化玩家列表
string[] users = RoomUsersInfo.Split('、');
this.userlist.Items.Clear();
for (int i = 0; i < users.Length; i++)
{
this.userlist.Items.Add(users[i]);
}
this.myink.IsEnabled = false;
//初始化画板笔迹
if (ink != null)
Showink(ink);
//初始化按钮和题目信息
if (hua_or_cai == false)
{
this.timu_label.Content = "提示:";
int len = timu.Length;
string str = "";
for (int i = 0; i < len; i++)
{
str += "?";
}
str += " ";
this.timu.Text = str;
this.refersh_timu.IsEnabled = false;
}
}
private void send_button_Click(object sender, RoutedEventArgs e) //大厅发送消息事件
{
client.Talk(UserName, this.message.Text);
message.Text = "";
}
private void message_KeyDown(object sender, KeyEventArgs e) //大厅按回车发送消息事件
{
if (e.Key == Key.Enter)
{
client.Talk(UserName, this.message.Text);
message.Text = "";
}
}
public void ShowLogin(string loginUserName, int[] roomcount) //显示新用户登录游戏,进入大厅
{
if (loginUserName == UserName) //如果是这个正在登录的用户,初始化界面
{
MessageBox.Show("登录成功!正在进入大厅……");
for (int i = 0; i < 4; i++)
{
string str = "room" + (i + 1) + "_user_count";
Showlabel(str, roomcount[i]);
}
this.loginwindow.Visibility = Visibility.Hidden;
this.lobby.Visibility = System.Windows.Visibility.Visible;
}
AddMessage(loginUserName + "进入大厅。");
}
public void ShowLogout(string userName) //显示用户退出游戏
{
AddMessage(userName + "退出游戏。");
}
public void ShowTalk(string userName, string message) //在大厅发消息显示消息内容
{
AddMessage(userName + ": " + message);
}
public void ShowInRoom(string userName) //显示用户进入房间
{
//如果是正在进入房间的用户,进行初始化
if (this.UserName == userName)
{
MessageBox.Show("正在进入房间……");
this.lobby.Visibility = Visibility.Hidden;
this.gameroom.Visibility = System.Windows.Visibility.Visible;
}
//在房间玩家列表显示新进来的玩家
this.userlist.Items.Add(userName);
//在房间消息列表中显示新玩家进入
this.game_message.Items.Add(userName + "进入房间。");
}
public void ShowOutRoom(string userName) //显示用户退出房间
{
//清空正在退出的玩家的画板墨迹
if (this.UserName == userName)
this.myink.Strokes.Clear();
this.game_message.Items.Add(userName + "退出房间。");
this.userlist.Items.Remove(userName);
}
public void Showlabel(string labelname, int roomusercount) //大厅界面显示四个房间的人数
{
if (labelname == "room1_user_count")
this.room1_user_count.Content = "房间人数:" + roomusercount;
else if (labelname == "room2_user_count")
this.room2_user_count.Content = "房间人数:" + roomusercount;
else if (labelname == "room3_user_count")
this.room3_user_count.Content = "房间人数:" + roomusercount;
else if (labelname == "room4_user_count")
this.room4_user_count.Content = "房间人数:" + roomusercount;
}
public void Show_Room_Talk(string userName, string message) //显示在房间里聊天的消息
{
this.game_message.Items.Add(userName + ":" + message);
}
public void Show_Room_Talk_error(string message)
{
this.game_message.Items.Add(message);
}
public void Show_Guess_Talk(string userName, string message, string daan) //显示猜测聊天框中的聊天消息
{
this.guess_message_list.Items.Add(userName + ":" + message);
//判断猜测的答案是否正确
if (userName == this.UserName)
{
if (message != daan)
MessageBox.Show("猜测答案错误!");
else
{
client.Gusee_win(userName);
}
}
}
public void Showink(string ink) //显示墨迹信息
{
//清除画板原来的墨迹
this.myink.Strokes.Clear();
//字符串类型墨迹画迹转化为墨迹数据
StrokeCollection strokes =(StrokeCollection)(new StrokeCollectionConverter()).ConvertFrom(ink);
//将传送来的墨迹添加到画板中
myink.Strokes = strokes;
}
public void Show_Guess_win(string username) //显示用户猜中
{
if (this.UserName == username)
MessageBox.Show("你猜中了!");
else
MessageBox.Show(username + "猜中了!");
myink.Strokes.Clear();
}
private void room_MouseDown(object sender, MouseButtonEventArgs e) //鼠标点击房间按钮事件
{
try
{
//获得进入的房间名
Image image = e.Source as Image;
client.InRoom(UserName, image.Name);
}
catch (Exception ex)
{
MessageBox.Show("进入房间失败:" + ex.Message);
}
}
private void room_messagesend_Click(object sender, RoutedEventArgs e) //房间聊天框发送按钮事件
{
client.Room_Talk(UserName, this.room_message.Text);
room_message.Text = "";
}
private void room_message_KeyDown(object sender, KeyEventArgs e) //房间聊天框回车发送事件
{
if (e.Key == Key.Enter)
{
client.Room_Talk(UserName, this.room_message.Text);
room_message.Text = "";
}
}
private void InkCanvas_MouseUp(object sender, MouseButtonEventArgs e) //画板笔迹事件,鼠标落下后起来传送笔迹信息
{
//墨迹打包为字符串发送到服务端
StrokeCollection strokes = this.myink.Strokes;
string ink_str = (new StrokeCollectionConverter()).ConvertToString(strokes);
client.sendink(UserName, ink_str);
}
private void guess_messagesend_Click(object sender, RoutedEventArgs e) //房间内猜测聊天框发送按钮事件
{
client.Guess_talk(UserName, this.guess_message.Text);
guess_message.Text = "";
}
private void guess_message_KeyDown(object sender, KeyEventArgs e) //房间内猜测聊天框回车发送事件
{
if (e.Key == Key.Enter)
{
client.Guess_talk(UserName, this.guess_message.Text);
guess_message.Text = "";
}
}
public void refersh_timu_Click(object sender, RoutedEventArgs e)
{
client.Refersh_Timu(UserName);
} //刷新题目按钮事件
public void Show_Refersh_Timu(string username, string Timu)
{
if (this.UserName == username)
{
this.timu.Text = Timu;
this.timu_label.Content = "题目";
}
else
{
int len = Timu.Length;
string str = "";
for (int i = 0; i < len; i++)
{
str += "?";
}
str += " ";
this.timu.Text = str;
this.timu_label.Content = "提示";
}
} //展示刷新题目的结果
public void Show_Cannot_login()
{
MessageBox.Show("用户已存在!请重新登录!");
} //提示用户不能登录
public void InitNewchutiren(string timu)
{
MessageBox.Show("你成为画题人");
this.timu_label.Content = "题目";
this.timu.Text = timu;
this.myink.IsEnabled = true;
tool_Color.IsEnabled = true;
tool_Clear.IsEnabled = true;
this.guess_message.IsEnabled = false;
this.guess_messagesend.IsEnabled = false;
this.refersh_timu.IsEnabled = true;
} //初始化新的出题人界面
private void Roomout_Click(object sender, RoutedEventArgs e)
{
client.RoomOut(UserName);
this.gameroom.Visibility = Visibility.Hidden;
this.lobby.Visibility = Visibility.Visible;
} //退出房间按钮事件
private void Brush_Color_change(object sender, RoutedEventArgs e)
{
DrawingAttributes drawingAttributes = new DrawingAttributes();
drawingAttributes.Color = (Color)ColorConverter.ConvertFromString((e.Source as Button).Background.ToString());
myink.DefaultDrawingAttributes = drawingAttributes;
myink.EditingMode = InkCanvasEditingMode.Ink;
} //画刷颜色改变事件
private void clearButton_Click(object sender, RoutedEventArgs e)
{
myink.EditingMode = InkCanvasEditingMode.EraseByPoint;
} //擦除墨迹按钮事件
public void change_hua_or_cai(bool hua, string username)
{
myink.IsEnabled = hua;
tool_Color.IsEnabled = hua;
tool_Clear.IsEnabled = hua;
guess_message.IsEnabled = !hua;
guess_messagesend.IsEnabled = !hua;
this.refersh_timu.IsEnabled = hua;
client.Refersh_Timu(username);
} //改变画题人和猜题人
}
}



