From 3bb4abc86f01a69b075970fa928fac26fad94446 Mon Sep 17 00:00:00 2001 From: tamquy999 Date: Tue, 5 Nov 2019 23:42:43 +0700 Subject: [PATCH] Messagebox to center + Spellcheck --- CaroLAN v3.csproj | 1 + Form1.cs | 6 +- Form2.cs | 38 ++++--- MessageBoxEx.cs | 229 ++++++++++++++++++++++++++++++++++++++++++ Resource1.Designer.cs | 16 +-- Resource1.resx | 16 +-- 6 files changed, 273 insertions(+), 33 deletions(-) create mode 100644 MessageBoxEx.cs diff --git a/CaroLAN v3.csproj b/CaroLAN v3.csproj index 0e50d10..063a8bc 100644 --- a/CaroLAN v3.csproj +++ b/CaroLAN v3.csproj @@ -62,6 +62,7 @@ Form2.cs + diff --git a/Form1.cs b/Form1.cs index 60aad02..3e26792 100644 --- a/Form1.cs +++ b/Form1.cs @@ -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 == "") @@ -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; } @@ -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 diff --git a/Form2.cs b/Form2.cs index 77a9a09..5f6ca01 100644 --- a/Form2.cs +++ b/Form2.cs @@ -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(); } @@ -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); @@ -161,7 +162,7 @@ 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; @@ -169,22 +170,28 @@ public void ConvertSocketDataReceived(SocketData Data) 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; } @@ -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()); @@ -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"); } } } diff --git a/MessageBoxEx.cs b/MessageBoxEx.cs new file mode 100644 index 0000000..b1b4ead --- /dev/null +++ b/MessageBoxEx.cs @@ -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); + } + } +} diff --git a/Resource1.Designer.cs b/Resource1.Designer.cs index 20f476e..05e2a81 100644 --- a/Resource1.Designer.cs +++ b/Resource1.Designer.cs @@ -72,12 +72,12 @@ internal static System.Drawing.Bitmap about { /// /// Looks up a localized string similar to CaroGame v3.0.1 - ///Development Team: - ///Đào Anh Tú (@Qynklee) - ///Nguyễn Ngọc Hưng (@tamquy999) - ///Thanks to: Mr.Ha Chi Trung - ///Google; StackOverflow; Microsoft... - ///This game now is open source in: + ///Developer Team: + /// Đào Anh Tú (@Qynklee) + /// Nguyễn Ngọc Hưng (@tamquy999) + ///Thanks for: Mr. Ha Chi Trung + ///Thanks to: Google, Microsoft, StackOverflow, ... + ///This game is now open source on: ///https://github.com/Qynklee/GameCaroLAN. /// internal static string About_DevTeam { @@ -97,8 +97,8 @@ internal static System.Drawing.Bitmap exit { } /// - /// Looks up a localized string similar to 5 in line to win - ///Each turn plays in 15s ! + /// Looks up a localized string similar to 5 in a line to win + ///Each player has 15s to make their decision ///Have fun!. /// internal static string Gamerule { diff --git a/Resource1.resx b/Resource1.resx index 90d1061..519b1e4 100644 --- a/Resource1.resx +++ b/Resource1.resx @@ -123,20 +123,20 @@ CaroGame v3.0.1 -Development Team: -Đào Anh Tú (@Qynklee) -Nguyễn Ngọc Hưng (@tamquy999) -Thanks to: Mr.Ha Chi Trung -Google; StackOverflow; Microsoft... -This game now is open source in: +Developer Team: + Đào Anh Tú (@Qynklee) + Nguyễn Ngọc Hưng (@tamquy999) +Thanks for: Mr. Ha Chi Trung +Thanks to: Google, Microsoft, StackOverflow, ... +This game is now open source on: https://github.com/Qynklee/GameCaroLAN Resources\exit.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - 5 in line to win -Each turn plays in 15s ! + 5 in a line to win +Each player has 15s to make their decision Have fun!