Skip to content

Commit

Permalink
Messagebox to center + Spellcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
tamquy999 committed Nov 5, 2019
1 parent bab9e10 commit 3bb4abc
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 33 deletions.
1 change: 1 addition & 0 deletions CaroLAN v3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="Form2.Designer.cs">
<DependentUpon>Form2.cs</DependentUpon>
</Compile>
<Compile Include="MessageBoxEx.cs" />
<Compile Include="Player.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
6 changes: 4 additions & 2 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public Form1()
SOCKET = new SocketManager(this);
haveAvatar = false;

this.ActiveControl = textBox1;

string IPlocal = SOCKET.GetLocalIPv4(NetworkInterfaceType.Wireless80211);

if (IPlocal == null || IPlocal == "")
Expand Down Expand Up @@ -65,7 +67,7 @@ private void btn_Connect_Click(object sender, EventArgs e)
bool Checked = SOCKET.ConnectServer();
if (!Checked)//Connect unsuccess
{
MessageBox.Show("Can't connect to Server");
MessageBoxEx.Show(this, "Can't connect to Server");
Visible = true;
return;
}
Expand Down Expand Up @@ -98,7 +100,7 @@ private void btn_HostGame_Click(object sender, EventArgs e)
bool Checked = SOCKET.CreateServer();
if (!Checked)
{
MessageBox.Show("Can't Create Server");
MessageBoxEx.Show(this, "Can't Create Server");
return;
}
else
Expand Down
38 changes: 23 additions & 15 deletions Form2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ public void Endgame()
timer1.Stop();
if (CHESSBOARD.isTurnForMe)
{
MessageBox.Show("You looseeeeeeeee !!!");
MessageBoxEx.Show(this, "You looseeeeeeeee !!!");
player2_score++;
label_player2_score.Text = player2_score.ToString();
}
else
{
MessageBox.Show("You Winnnnnnnnnnn !!!");
MessageBoxEx.Show(this, "You Winnnnnnnnnnn !!!");
player1_score++;
label_player1_score.Text = player1_score.ToString();
}
Expand All @@ -144,14 +144,15 @@ public void ConvertSocketDataReceived(SocketData Data)

break;
case (int)SocketCommand.NOTIFY:
MessageBox.Show(Data.message.ToString());
MessageBoxEx.Show(this, Data.message.ToString());
break;
case (int)SocketCommand.NEW_GAME:
this.Invoke((MethodInvoker)(() =>
{
Reset_ProgressBar();
//Reset_ProgressBar();
DialogResult result = MessageBoxEx.Show(this, "Your partner want to make new game, Accept?", "", MessageBoxButtons.YesNo);
//CHESSBOARD.NewGame();
if (MessageBox.Show("Your partner want to make new game, Continue?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
if (result == DialogResult.Yes)
{
SocketData data = new SocketData((int)SocketCommand.ACCEPT, new Point());
SOCKET.Send(data);
Expand All @@ -161,30 +162,36 @@ public void ConvertSocketDataReceived(SocketData Data)
{
SocketData data = new SocketData((int)SocketCommand.IGNORE, new Point());
SOCKET.Send(data);
MessageBox.Show("New game request is aborted.");
//MessageBoxEx.Show(this, "New game request is aborted.");
}
}));
break;
case (int)SocketCommand.END_GAME:
break;
case (int)SocketCommand.QUIT:
timer1.Stop();
if (MessageBox.Show("Opponent left the game. Do you want to quit?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
this.Invoke((MethodInvoker)(() =>
{
Close();
}
DialogResult result2 = MessageBoxEx.Show(this, "Opponent left the game. Do you want to quit?", "", MessageBoxButtons.YesNo);
if (result2 == DialogResult.Yes)
{
Close();
}
}));
break;
case (int)SocketCommand.ACCEPT:
this.Invoke((MethodInvoker)(() =>
{
CHESSBOARD.NewGame();
MessageBox.Show("Your partner accept new game");
MessageBoxEx.Show(this, "Your partner accepted new game request");
Text = "Caro Game";
}));
break;
case (int)SocketCommand.IGNORE:
this.Invoke((MethodInvoker)(() =>
{
MessageBox.Show("Your partner ignore new game");
MessageBoxEx.Show(this, "Your partner ignored new game request");
Text = "Caro Game";
}));
break;
}
Expand Down Expand Up @@ -218,12 +225,13 @@ private void NewMatchToolStripMenuItem_Click(object sender, EventArgs e)
//CHESSBOARD.NewGame();
SocketData data = new SocketData((int)SocketCommand.NEW_GAME, new Point());
SOCKET.Send(data);
MessageBox.Show("Waitting Request...");
//MessageBoxEx.Show(this, "Waitting for respond ...");
Text = "Caro Game (Waitting for new game respond ...)";
}

private void QuitGameToolStripMenuItem_Click(object sender, EventArgs e)
{
if(MessageBox.Show("Do you want to quit ?","Quit Game",MessageBoxButtons.YesNo) == DialogResult.Yes)
if(MessageBoxEx.Show(this, "Do you really want to quit ?","Quit Game",MessageBoxButtons.YesNo) == DialogResult.Yes)
{
form1.Close();
SocketData socDat = new SocketData((int)SocketCommand.QUIT, new Point());
Expand All @@ -237,12 +245,12 @@ private void QuitGameToolStripMenuItem_Click(object sender, EventArgs e)

private void AboutCaroGameToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(Resource1.Gamerule, "Game Rules");
MessageBoxEx.Show(this, Resource1.Gamerule, "Game Rules");
}

private void AboutDevTeamToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(Resource1.About_DevTeam,"About");
MessageBoxEx.Show(this, Resource1.About_DevTeam, "About");
}
}
}
229 changes: 229 additions & 0 deletions MessageBoxEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaroLAN_v2
{
class MessageBoxEx
{
private static IWin32Window _owner;
private static HookProc _hookProc;
private static IntPtr _hHook;

public static DialogResult Show(string text)
{
Initialize();
return MessageBox.Show(text);
}

public static DialogResult Show(string text, string caption)
{
Initialize();
return MessageBox.Show(text, caption);
}

public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
{
Initialize();
return MessageBox.Show(text, caption, buttons);
}

public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
Initialize();
return MessageBox.Show(text, caption, buttons, icon);
}

public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton)
{
Initialize();
return MessageBox.Show(text, caption, buttons, icon, defButton);
}

public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton, MessageBoxOptions options)
{
Initialize();
return MessageBox.Show(text, caption, buttons, icon, defButton, options);
}

public static DialogResult Show(IWin32Window owner, string text)
{
_owner = owner;
Initialize();
return MessageBox.Show(owner, text);
}

public static DialogResult Show(IWin32Window owner, string text, string caption)
{
_owner = owner;
Initialize();
return MessageBox.Show(owner, text, caption);
}

public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
{
_owner = owner;
Initialize();
return MessageBox.Show(owner, text, caption, buttons);
}

public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
_owner = owner;
Initialize();
return MessageBox.Show(owner, text, caption, buttons, icon);
}

public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton)
{
_owner = owner;
Initialize();
return MessageBox.Show(owner, text, caption, buttons, icon, defButton);
}

public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton, MessageBoxOptions options)
{
_owner = owner;
Initialize();
return MessageBox.Show(owner, text, caption, buttons, icon,
defButton, options);
}

public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);

public delegate void TimerProc(IntPtr hWnd, uint uMsg, UIntPtr nIDEvent, uint dwTime);

public const int WH_CALLWNDPROCRET = 12;

public enum CbtHookAction : int
{
HCBT_MOVESIZE = 0,
HCBT_MINMAX = 1,
HCBT_QS = 2,
HCBT_CREATEWND = 3,
HCBT_DESTROYWND = 4,
HCBT_ACTIVATE = 5,
HCBT_CLICKSKIPPED = 6,
HCBT_KEYSKIPPED = 7,
HCBT_SYSCOMMAND = 8,
HCBT_SETFOCUS = 9
}

[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle lpRect);

[DllImport("user32.dll")]
private static extern int MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

[DllImport("User32.dll")]
public static extern UIntPtr SetTimer(IntPtr hWnd, UIntPtr nIDEvent, uint uElapse, TimerProc lpTimerFunc);

[DllImport("User32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

[DllImport("user32.dll")]
public static extern int UnhookWindowsHookEx(IntPtr idHook);

[DllImport("user32.dll")]
public static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxLength);

[DllImport("user32.dll")]
public static extern int EndDialog(IntPtr hDlg, IntPtr nResult);

[StructLayout(LayoutKind.Sequential)]
public struct CWPRETSTRUCT
{
public IntPtr lResult;
public IntPtr lParam;
public IntPtr wParam;
public uint message;
public IntPtr hwnd;
};

static MessageBoxEx()
{
_hookProc = new HookProc(MessageBoxHookProc);
_hHook = IntPtr.Zero;
}

private static void Initialize()
{
if (_hHook != IntPtr.Zero)
{
throw new NotSupportedException("multiple calls are not supported");
}

if (_owner != null)
{
_hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, _hookProc, IntPtr.Zero, AppDomain.GetCurrentThreadId());
}
}

private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode < 0)
{
return CallNextHookEx(_hHook, nCode, wParam, lParam);
}

CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT));
IntPtr hook = _hHook;

if (msg.message == (int)CbtHookAction.HCBT_ACTIVATE)
{
try
{
CenterWindow(msg.hwnd);
}
finally
{
UnhookWindowsHookEx(_hHook);
_hHook = IntPtr.Zero;
}
}

return CallNextHookEx(hook, nCode, wParam, lParam);
}

private static void CenterWindow(IntPtr hChildWnd)
{
Rectangle recChild = new Rectangle(0, 0, 0, 0);
bool success = GetWindowRect(hChildWnd, ref recChild);

int width = recChild.Width - recChild.X;
int height = recChild.Height - recChild.Y;

Rectangle recParent = new Rectangle(0, 0, 0, 0);
success = GetWindowRect(_owner.Handle, ref recParent);

Point ptCenter = new Point(0, 0);
ptCenter.X = recParent.X + ((recParent.Width - recParent.X) / 2);
ptCenter.Y = recParent.Y + ((recParent.Height - recParent.Y) / 2);


Point ptStart = new Point(0, 0);
ptStart.X = (ptCenter.X - (width / 2));
ptStart.Y = (ptCenter.Y - (height / 2));

ptStart.X = (ptStart.X < 0) ? 0 : ptStart.X;
ptStart.Y = (ptStart.Y < 0) ? 0 : ptStart.Y;

int result = MoveWindow(hChildWnd, ptStart.X, ptStart.Y, width,
height, false);
}
}
}
Loading

0 comments on commit 3bb4abc

Please sign in to comment.