225 lines
8.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Gui.Forms
{
public partial class MessageForm : Form
{
public enum MessageType
{
GENERAL,
INFO,
WARNING,
ERROR,
QUESTION
}
public enum MessageFormType
{
// Summary:
// The message box contains an OK button.
OK = 0,
//
// Summary:
// The message box contains OK and Cancel buttons.
OKCancel = 1,
//
// Summary:
// The message box contains Abort, Retry, and Ignore buttons.
YesNoCancel = 3,
//
// Summary:
// The message box contains Yes and No buttons.
YesNo = 4
}
public MessageForm(string message, string caption, MessageType msgtype, MessageFormType buttons, bool RTL,string localYes, string localNo, string localCancel, string localOK, bool handCursorOnButtons)
{
InitializeComponent();
this.RightToLeft = (RTL) ? RightToLeft.Yes : RightToLeft.No;
this.RightToLeftLayout = (RTL) ? true : false;
lblErrorMsg.Text = message;
this.Text = caption;
switch (msgtype)
{
case MessageType.INFO: pctbxIcon.Image = global::AMT_SW_GUI.Properties.Resources.scs_32px_info; break;
case MessageType.ERROR: pctbxIcon.Image = global::AMT_SW_GUI.Properties.Resources.scs_32px_error; btnOk.Text = global::AMT_SW_GUI.Properties.Resources.CLOSE; break;
case MessageType.QUESTION: pctbxIcon.Image = global::AMT_SW_GUI.Properties.Resources.scs_welcome32_help; break;
case MessageType.GENERAL: pctbxIcon.Visible = false; lblErrorMsg.Location = new Point(pctbxIcon.Location.X, lblErrorMsg.Location.Y); break;
default: break;
}
Size size = pctbxIcon.Size;
pctbxIcon.Size = size;
btnCancel.Location = new Point(btnCancel.Location.X, 3);
btnYes.Location = new Point(btnYes.Location.X, 3);
btnNo.Location = new Point(btnNo.Location.X,3);
btnOk.Location = new Point(btnOk.Location.X, 3);
int newHeight = innerPaneMessage.Height + innerPaneMessage.Padding.Bottom +
innerPaneMessage.Padding.Top + innerPaneMessage.Margin.Top +
innerPaneMessage.Margin.Bottom + pnlButtons.Height +
tblMessageBox.Margin.Bottom * tblMessageBox.RowCount +
tblMessageBox.Margin.Top * tblMessageBox.RowCount;
this.MinimumSize = new Size(this.Width, newHeight);
int centerPxl = this.Size.Width / 2;
int intBtnWidth = btnYes.Width;
int intBtnDist = intBtnWidth / 10;
if (this.Parent == null)
StartPosition = FormStartPosition.CenterScreen;
switch (buttons)
{
case MessageFormType.OK:
btnCancel.Visible = btnNo.Visible = btnYes.Visible = false;
btnOk.Location = new Point(centerPxl - (intBtnWidth / 2), btnOk.Location.Y);
break;
case MessageFormType.OKCancel:
btnNo.Visible = btnYes.Visible = false;
if (!RTL)
{
btnOk.Location = new Point(centerPxl - (intBtnWidth) - (intBtnDist / 2), btnOk.Location.Y);
btnCancel.Location = new Point(centerPxl + (intBtnDist / 2), btnOk.Location.Y);
}
else
{
btnCancel.Location = new Point(centerPxl - (intBtnWidth) - (intBtnDist / 2), btnOk.Location.Y);
btnOk.Location = new Point(centerPxl + (intBtnDist / 2), btnOk.Location.Y);
}
break;
case MessageFormType.YesNo:
btnCancel.Visible = btnOk.Visible = false;
if (!RTL)
{
btnYes.Location = new Point(centerPxl - (intBtnWidth) - (intBtnDist / 2), btnOk.Location.Y);
btnNo.Location = new Point(centerPxl + (intBtnDist / 2), btnOk.Location.Y);
}
else
{
btnNo.Location = new Point(centerPxl - (intBtnWidth) - (intBtnDist / 2), btnOk.Location.Y);
btnYes.Location = new Point(centerPxl + (intBtnDist / 2), btnOk.Location.Y);
}
break;
case MessageFormType.YesNoCancel:
btnOk.Visible = false;
btnNo.Location = new Point(centerPxl - (intBtnWidth / 2), btnNo.Location.Y);
if (!RTL)
{
btnYes.Location = new Point(centerPxl - (int)(intBtnWidth * 1.5) - (intBtnDist / 2), btnYes.Location.Y);
btnCancel.Location = new Point(centerPxl + (int)(intBtnWidth * 0.5) + (intBtnDist / 2), btnCancel.Location.Y);
}
else
{
btnCancel.Location = new Point(centerPxl - (int)(intBtnWidth * 1.5) - (intBtnDist / 2), btnYes.Location.Y);
btnYes.Location = new Point(centerPxl + (int)(intBtnWidth * 0.5) + (intBtnDist / 2), btnCancel.Location.Y);
}
break;
}
btnCancel.Text = localCancel;
btnYes.Text = localYes;
btnNo.Text = localNo;
btnOk.Text = localOK;
if (handCursorOnButtons == true)
{
btnYes.Cursor = Cursors.Hand;
btnNo.Cursor = Cursors.Hand;
btnCancel.Cursor = Cursors.Hand;
btnOk.Cursor = Cursors.Hand;
}
else
{
btnYes.Cursor = Cursors.Default;
btnNo.Cursor = Cursors.Default;
btnCancel.Cursor = Cursors.Default;
btnOk.Cursor = Cursors.Default;
}
}
private void btnClose_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Yes;
Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
this.Close();
}
private void btnNo_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.No;
this.Close();
}
public static MessageType GetType(MessageBoxIcon icon)
{
if (icon == MessageBoxIcon.Question)
return MessageType.QUESTION;
if (icon == MessageBoxIcon.Warning)
return MessageType.WARNING;
if (icon == MessageBoxIcon.Error)
return MessageType.ERROR;
return MessageType.INFO;
}
public static MessageFormType GetButtons(MessageBoxButtons buttons)
{
if (buttons == MessageBoxButtons.OK)
return MessageFormType.OK;
if (buttons == MessageBoxButtons.OKCancel)
return MessageFormType.OKCancel;
if (buttons == MessageBoxButtons.YesNo)
return MessageFormType.YesNo;
return MessageFormType.YesNoCancel;
}
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int message, int wParam, IntPtr lParam);
[DllImport("user32.dll", ExactSpelling = true)]
private static extern IntPtr GetWindow(IntPtr hwnd, int cmd);
private IntPtr GetTopLevelOwner(Control c)
{
IntPtr hwndOwner = c.Handle;
IntPtr hwndCurrent = c.Handle;
while (hwndCurrent != (IntPtr)0)
{
hwndCurrent = GetWindow(hwndCurrent, GW_OWNER);
if (hwndCurrent != (IntPtr)0) { hwndOwner = hwndCurrent; }
}
return hwndOwner;
}
private const int WM_SETICON = 0x80;
private const int GW_OWNER = 4;
private const int ICON_SMALL = 0;
private const int ICON_BIG = 1;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
SendMessage(GetTopLevelOwner(this), WM_SETICON, ICON_BIG, this.Icon.Handle);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
SendMessage(GetTopLevelOwner(this), WM_SETICON, ICON_BIG, new IntPtr(0));
}
}
}