using System.Data; using System.Drawing; using System.Windows.Forms; namespace Dongke.WinForm.Controls.InvoiceLayout { internal partial class TextItemSetting : Setting { #region 成员变量 private TextAlignment _textAlign = TextAlignment.LeftOrTop; // 文本对齐方式 private TextAlignment _textAlignVertical = TextAlignment.LeftOrTop; // 文本垂直对齐方式 private ItemStyle _itemStyle = ItemStyle.Text; // 项目类别 private string _defaultValue = string.Empty; // 默认值 private string _textFormat = string.Empty; // 文本格式 private string _culture = string.Empty; // culture private bool _isInit = true; private bool _isNew = false; private bool _isDataBinding = false; private DataTable _sysFormat; private DataTable _sysDefaultValue; #endregion #region 属性 /// /// 是否自动文本换行 /// public bool Wrap { get { return this.ckbWrap.Checked; } set { this.ckbWrap.Checked = value; } } /// /// 文本字体 /// public Font TextFont { get { return txtPreview.Font; } set { txtPreview.Font = value; } } /// /// 文本颜色 /// public Color TextColor { get { return txtPreview.ForeColor; } set { txtPreview.ForeColor = value; } } /// /// 文本内容 /// public string TextPreview { get { return txtPreview.Text; } set { txtPreview.Text = value; } } /// /// 文本对齐方式 /// public TextAlignment TextAlign { get { return _textAlign; } set { if (_textAlign != value) { _textAlign = value; } } } /// /// 文本垂直对齐方式 /// public TextAlignment TextAlignVertical { get { return _textAlignVertical; } set { if (_textAlignVertical != value) { _textAlignVertical = value; } } } /// /// 行距 /// public float LineSpace { get { return System.Convert.ToSingle(numLineSpace.Value); } set { decimal d = System.Convert.ToDecimal(value); if (d < numLineSpace.Minimum) { d = numLineSpace.Minimum; } else if (numLineSpace.Maximum < d) { d = numLineSpace.Maximum; } numLineSpace.Value = d; } } /// /// 字间距 /// public float CharacterSpace { get { return System.Convert.ToSingle(numCharSpace.Value); } set { decimal d = System.Convert.ToDecimal(value); if (d < numCharSpace.Minimum) { d = numCharSpace.Minimum; } else if (numCharSpace.Maximum < d) { d = numCharSpace.Maximum; } numCharSpace.Value = d; } } /// /// X /// public float TextLocationY { get { return System.Convert.ToSingle(numLoctionY.Value); } set { decimal d = System.Convert.ToDecimal(value); if (d < numLoctionY.Minimum) { d = numLoctionY.Minimum; } else if (numLoctionY.Maximum < d) { d = numLoctionY.Maximum; } numLoctionY.Value = d; } } /// /// Y /// public float TextLocationX { get { return System.Convert.ToSingle(numLoctionX.Value); } set { decimal d = System.Convert.ToDecimal(value); if (d < numLoctionX.Minimum) { d = numLoctionX.Minimum; } else if (numLoctionX.Maximum < d) { d = numLoctionX.Maximum; } numLoctionX.Value = d; } } /// /// 宽 /// public float TextWidth { get { return System.Convert.ToSingle(numWidth.Value); } set { decimal d = System.Convert.ToDecimal(value); if (d < numWidth.Minimum) { d = numWidth.Minimum; } else if (numWidth.Maximum < d) { d = numWidth.Maximum; } numWidth.Value = d; } } /// /// 高 /// public float TextHeight { get { return System.Convert.ToSingle(numHeight.Value); } set { decimal d = System.Convert.ToDecimal(value); if (d < numHeight.Minimum) { d = numHeight.Minimum; } else if (numHeight.Maximum < d) { d = numHeight.Maximum; } numHeight.Value = d; } } /// /// 单行文字数 /// public int CharacterCount { get { return System.Convert.ToInt32(numCharCount.Value); } set { decimal d = System.Convert.ToDecimal(value); if (d < numCharCount.Minimum) { d = numCharCount.Minimum; } else if (numCharCount.Maximum < d) { d = numCharCount.Maximum; } numCharCount.Value = d; } } /// /// 文本默认值 /// public string DefaultValue { get { return ddlDefaultValue.Text; } set { _defaultValue = value; } } /// /// 文本格式 /// public string TextFormat { get { return this.ddlTextFormat.Text; } set { _textFormat = value; } } /// /// 一维码格式 /// public string BarcodeFormat { get { return ddlBarcodeFormat.Text; } set { ddlBarcodeFormat.Text = value; if (ddlBarcodeFormat.SelectedIndex == -1) { ddlBarcodeFormat.SelectedIndex = 0; } } } /// /// 二维码级别 /// public string QRCodeLevel { get { return ddlQRCodeLevel.Text; } set { ddlQRCodeLevel.Text = value; if (ddlQRCodeLevel.SelectedIndex == -1) { ddlQRCodeLevel.SelectedIndex = 0; } } } /// /// Culture /// public string Culture { get { return _culture; } set { _culture = value; } } /// /// 项目类别 /// public ItemStyle ItemStyle { get { return _itemStyle; } set { _itemStyle = value; } } /// /// 新建 /// public bool IsNew { get { return _isNew; } set { _isNew = value; } } /// /// 来源绑定 /// public bool IsDataBinding { get { return this._isDataBinding; } set { this._isDataBinding = value; this.ddlDefaultValue.Enabled = value; this.ddlTextFormat.Enabled = value; //this.lblPreview.Text = value ? "预览" : "打印内容"; } } /// /// 项目名 /// public string ItemName { get { return txtItemName.Text; } set { txtItemName.Text = value; } } #endregion #region 构造函数 /// /// 构造函数 /// public TextItemSetting() { InitializeComponent(); InitializeDDL(); TextWidth = LayoutCommon.DefaultTextItemSizeWidth; TextHeight = LayoutCommon.DefaultTextItemSizeHeight; } #endregion #region 函数 /// /// 获取系统字体 /// private void InitializeDDL() { _sysFormat = LayoutCommon.SYSFormat.Copy(); _sysDefaultValue = LayoutCommon.SYSDefaultValue.Copy(); ddlDefaultValue.DisplayMember = "InitialValueName"; ddlDefaultValue.ValueMember = "InitialValueName"; ddlDefaultValue.DataSource = _sysDefaultValue; ddlTextFormat.DisplayMember = "FormatName"; ddlTextFormat.ValueMember = "FormatID"; ddlTextFormat.DataSource = _sysFormat; } #endregion 函数 #region 事件处理 /// /// 文本设置 /// /// 指定的对象 /// 提供的事件数据 private void TextItemSetting_Shown(object sender, System.EventArgs e) { try { // 条码文本时,才显示条码属性 //if (_itemStyle == ItemStyle.Barcode) //{ // this.chkPic.Checked = true; // this.chkM.Visible = true; // this.chkFixedRatio.Visible = true; //} //else //{ // this.chkPic.Checked = false; // this.chkM.Visible = false; // this.chkFixedRatio.Visible = false; //} if (_itemStyle == ItemStyle.Barcode || _itemStyle == ItemStyle.QRCode || _itemStyle == ItemStyle.LinearGraph ) { this.btnSelectColor.Enabled = false; //this.btnFont.Enabled = false; this.chkM.Visible = false; this.chkFixedRatio.Visible = false; this.ddlDefaultValue.Enabled = false; this.ddlTextFormat.Enabled = false; } this.chkM.Visible = false; this.chkFixedRatio.Visible = false; if (_itemStyle == ItemStyle.Barcode) { ddlBarcodeFormat.Visible = true; if (ddlBarcodeFormat.SelectedIndex == -1) { ddlBarcodeFormat.SelectedIndex = 0; } this.ckbWrap.Text = "是否显示条码文本"; } if (_itemStyle == ItemStyle.QRCode) { ddlQRCodeLevel.Visible = true; if (ddlQRCodeLevel.SelectedIndex == -1) { ddlQRCodeLevel.SelectedIndex = 0; } } if (_textAlign == TextAlignment.Evenness) { numCharSpace.Enabled = false; numCharSpace.Value = 0; numCharCount.Enabled = false; numCharCount.Value = 0; } else { if (0 < numCharCount.Value) { rbtnLeft.Checked = true; grbTextAlignment.Enabled = false; numCharSpace.Enabled = false; decimal characterSpace = LayoutCommon.EqualityChars(numWidth.Value, numCharCount.Value, txtPreview.Font); if (characterSpace < numCharSpace.Minimum) { characterSpace = numCharSpace.Minimum; } else if (numCharSpace.Maximum < characterSpace) { characterSpace = numCharSpace.Maximum; } numCharSpace.Value = LayoutCommon.Truncate(characterSpace, numCharSpace.DecimalPlaces); } } if (_sysFormat != null) { if (_itemStyle == ItemStyle.Date) { _sysFormat.DefaultView.RowFilter = "SignDispNo = 0"; } else if (_itemStyle == ItemStyle.Sign) { _sysFormat.DefaultView.RowFilter = "DateDispNo = 0"; } else { _sysFormat.DefaultView.RowFilter = string.Empty; } } if (grbTextAlignment.Enabled) { switch (_textAlign) { case TextAlignment.LeftOrTop: rbtnLeft.Checked = true; txtPreview.TextAlign = HorizontalAlignment.Left; break; case TextAlignment.RightOrBottom: rbtnRight.Checked = true; txtPreview.TextAlign = HorizontalAlignment.Right; break; case TextAlignment.Center: rbtnCenter.Checked = true; txtPreview.TextAlign = HorizontalAlignment.Center; break; case TextAlignment.Evenness: rbtnEvenness.Checked = true; txtPreview.TextAlign = HorizontalAlignment.Left; break; } } if (grbTextAlignmentV.Enabled) { switch (_textAlignVertical) { case TextAlignment.LeftOrTop: rbtnTop.Checked = true; break; case TextAlignment.RightOrBottom: rbtnBottom.Checked = true; break; case TextAlignment.Center: rbtnCenterV.Checked = true; break; default: rbtnTop.Checked = true; break; } } if (_isNew || !_isDataBinding) { if (_sysFormat != null) { ddlTextFormat.SelectedIndex = 0; } if (_sysDefaultValue != null) { ddlDefaultValue.SelectedIndex = 0; } } else { if (_sysFormat != null) { if (_itemStyle == ItemStyle.Date) { // 日期 DataRow[] drs = _sysFormat.Select (string.Format("Format = '{0}' AND ('{1}' = '' OR Culture = '{1}') AND DateDispNo <> 0", _textFormat, _culture)); if (drs != null && 0 < drs.Length) { ddlTextFormat.SelectedValue = drs[0]["FormatID"]; } else { ddlTextFormat.Text = _textFormat; } } else if (_itemStyle == ItemStyle.Sign) { // 符号 ddlTextFormat.ValueMember = "FormatName"; ddlTextFormat.SelectedValue = _textFormat; if (ddlTextFormat.SelectedIndex < 0) { ddlTextFormat.Text = _textFormat; } } else if (_itemStyle == InvoiceLayout.ItemStyle.Barcode || _itemStyle == InvoiceLayout.ItemStyle.QRCode || _itemStyle == InvoiceLayout.ItemStyle.LinearGraph ) { } else { ddlTextFormat.ValueMember = "Format"; ddlTextFormat.SelectedValue = _textFormat; if (ddlTextFormat.SelectedIndex < 0) { ddlTextFormat.Text = _textFormat; } } } ddlDefaultValue.Text = _defaultValue; } } finally { _isInit = false; } } /// /// 设置文本颜色 /// /// 指定的对象 /// 提供的事件数据 private void btnSelectColor_Click(object sender, System.EventArgs e) { colorDialog.Color = txtPreview.ForeColor; if (colorDialog.ShowDialog() == DialogResult.OK) { txtPreview.ForeColor = colorDialog.Color; } } /// /// 设置文本格式 /// /// 指定的对象 /// 提供的事件数据 private void ddlTextFormat_SelectedIndexChanged(object sender, System.EventArgs e) { _culture = string.Empty; DataRowView dataRow = ddlTextFormat.SelectedItem as DataRowView; if (dataRow != null) { if (0 != System.Convert.ToInt32(dataRow["DateDispNo"])) { _itemStyle = ItemStyle.Date; _textFormat = dataRow["Format"].ToString(); _culture = dataRow["Culture"].ToString(); } else if (0 != System.Convert.ToInt32(dataRow["SignDispNo"])) { _itemStyle = ItemStyle.Sign; _textFormat = dataRow["FormatName"].ToString(); } else { _itemStyle = ItemStyle.Text; _textFormat = dataRow["Format"].ToString(); } } else { //if (_itemStyle != ItemStyle.Barcode) //{ // _itemStyle = ItemStyle.Other; //} _textFormat = ddlTextFormat.Text; _culture = null; } } /// /// 设置文本对齐方式 /// /// 指定的对象 /// 提供的事件数据 private void rbtnTextAlignment_CheckedChanged(object sender, System.EventArgs e) { if (_isInit) { return; } if (rbtnLeft.Checked) { txtPreview.TextAlign = HorizontalAlignment.Left; _textAlign = TextAlignment.LeftOrTop; } else if (rbtnRight.Checked) { txtPreview.TextAlign = HorizontalAlignment.Right; _textAlign = TextAlignment.RightOrBottom; } else if (rbtnCenter.Checked) { txtPreview.TextAlign = HorizontalAlignment.Center; _textAlign = TextAlignment.Center; } else { txtPreview.TextAlign = HorizontalAlignment.Left; _textAlign = TextAlignment.Evenness; } if (rbtnTop.Checked) { _textAlignVertical = TextAlignment.LeftOrTop; } else if (rbtnBottom.Checked) { _textAlignVertical = TextAlignment.RightOrBottom; } else if (rbtnCenterV.Checked) { _textAlignVertical = TextAlignment.Center; } else { _textAlignVertical = TextAlignment.LeftOrTop; } if (rbtnEvenness.Checked) { numCharSpace.Enabled = false; numCharSpace.Value = 0; numCharCount.Enabled = false; numCharCount.Value = 0; } else { numCharSpace.Enabled = true; numCharCount.Enabled = true; } } /// /// 设置单行文字数 /// /// 指定的对象 /// 提供的事件数据 private void numCharCount_ValueChanged(object sender, System.EventArgs e) { if (_isInit) { return; } if (0 < numCharCount.Value) { rbtnLeft.Checked = true; grbTextAlignment.Enabled = false; numCharSpace.Enabled = false; decimal characterSpace = LayoutCommon.EqualityChars( numWidth.Value, numCharCount.Value, txtPreview.Font); if (characterSpace < numCharSpace.Minimum) { characterSpace = numCharSpace.Minimum; } else if (numCharSpace.Maximum < characterSpace) { characterSpace = numCharSpace.Maximum; } numCharSpace.Value = LayoutCommon.Truncate(characterSpace, numCharSpace.DecimalPlaces); } else { grbTextAlignment.Enabled = true; if (!rbtnEvenness.Checked) { numCharSpace.Enabled = true; } } } /// /// 设置文本字体。 /// /// 指定的对象 /// 提供的事件数据 private void txtPreview_FontChanged(object sender, System.EventArgs e) { if (_isInit) { return; } if (0 < numCharCount.Value) { decimal characterSpace = LayoutCommon.EqualityChars( numWidth.Value, numCharCount.Value, txtPreview.Font); if (characterSpace < numCharSpace.Minimum) { characterSpace = numCharSpace.Minimum; } else if (numCharSpace.Maximum < characterSpace) { characterSpace = numCharSpace.Maximum; } numCharSpace.Value = LayoutCommon.Truncate(characterSpace, numCharSpace.DecimalPlaces); } } /// /// 设置文本字体。 /// /// 指定的对象 /// 提供的事件数据 private void btnFont_Click(object sender, System.EventArgs e) { fontDialog.Font = TextFont; if (fontDialog.ShowDialog() == DialogResult.OK) { TextFont = fontDialog.Font; } } private void chkPic_CheckedChanged(object sender, System.EventArgs e) { chkM.Enabled = chkPic.Checked; chkFixedRatio.Enabled = chkPic.Checked; } #endregion 事件处理 private void btnOK_Click(object sender, System.EventArgs e) { } } }