PDA

View Full Version : سوال: خواص word در برنامه



sds1920
چهارشنبه 05 آبان 1389, 09:13 صبح
سلام خدمت همه ي دوستان.
من برنامه اي دارم بايد يك ويرايشگر متن داشته باشه.
ولي يك سري خواص Word مثل justify كردن متن(لبه هاي تمام خطوط برابر باشه) رو نياز داره.
توي TextBox كه هر كاري كردم نشد.
اگه قطعه كدي، كامپوننتي يا هر شيوه ديگري كه به ذهنتون مي رسه رو ارائه كنيد ممنون مي شم.

sokote_bi_payan
چهارشنبه 05 آبان 1389, 10:46 صبح
به نام خدا
سلام
دوست من شما می تونید از RichTextBox استفاده کنید
موفق باشید

sds1920
پنج شنبه 06 آبان 1389, 20:05 عصر
یه بار استفاده کردم ولی نشد.
میشه یه کم بیشتر توضیح بدید.
ممنون میشم.

sokote_bi_payan
شنبه 08 آبان 1389, 08:51 صبح
همونطور که می دونید RichTextbox خاصیت alignment Justify نداره من یک مقاله ای تو این http://geekswithblogs.net/pvidler/archive/2003/10/15/182.aspx یک مقاله در این مورد پیدا کردم و براتون کمی شرح می دم.
ابتدا یک کلاس می سازید به اسم : AdvRichTextBox.cs
بعد این کد و کامل کپی کنید تو اون کلاس:


//// In the name of ALLAH
////
//// Mahdi Zolghadr (Siavash37324@yahoo.com) - www.HanaService.ir

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

/// <summary>
/// Represents a standard <see cref="RichTextBox"/> with some
/// minor added functionality.
/// </summary>
/// <remarks>
/// AdvRichTextBox provides methods to maintain performance
/// while it is being updated. Additional formatting features
/// have also been added.
/// </remarks>
public class AdvRichTextBox : RichTextBox
{
/// <summary>
/// Maintains performance while updating.
/// </summary>
/// <remarks>
/// <para>
/// It is recommended to call this method before doing
/// any major updates that you do not wish the user to
/// see. Remember to call EndUpdate when you are finished
/// with the update. Nested calls are supported.
/// </para>
/// <para>
/// Calling this method will prevent redrawing. It will
/// also setup the event mask of the underlying richedit
/// control so that no events are sent.
/// </para>
/// </remarks>
public void BeginUpdate()
{
// Deal with nested calls.
++updating;

if (updating > 1)
return;

// Prevent the control from raising any events.
oldEventMask = SendMessage(new HandleRef(this, Handle),
EM_SETEVENTMASK, 0, 0);

// Prevent the control from redrawing itself.
SendMessage(new HandleRef(this, Handle),
WM_SETREDRAW, 0, 0);
}

/// <summary>
/// Resumes drawing and event handling.
/// </summary>
/// <remarks>
/// This method should be called every time a call is made
/// made to BeginUpdate. It resets the event mask to it's
/// original value and enables redrawing of the control.
/// </remarks>
public void EndUpdate()
{
// Deal with nested calls.
--updating;

if (updating > 0)
return;

// Allow the control to redraw itself.
SendMessage(new HandleRef(this, Handle),
WM_SETREDRAW, 1, 0);

// Allow the control to raise event messages.
SendMessage(new HandleRef(this, Handle),
EM_SETEVENTMASK, 0, oldEventMask);
}

/// <summary>
/// Gets or sets the alignment to apply to the current
/// selection or insertion point.
/// </summary>
/// <remarks>
/// Replaces the SelectionAlignment from
/// <see cref="RichTextBox"/>.
/// </remarks>
public new TextAlign SelectionAlignment
{
get
{
PARAFORMAT fmt = new PARAFORMAT();
fmt.cbSize = Marshal.SizeOf(fmt);

// Get the alignment.
SendMessage(new HandleRef(this, Handle),
EM_GETPARAFORMAT,
SCF_SELECTION, ref fmt);

// Default to Left align.
if ((fmt.dwMask & PFM_ALIGNMENT) == 0)
return TextAlign.Left;

return (TextAlign)fmt.wAlignment;
}

set
{
PARAFORMAT fmt = new PARAFORMAT();
fmt.cbSize = Marshal.SizeOf(fmt);
fmt.dwMask = PFM_ALIGNMENT;
fmt.wAlignment = (short)value;

// Set the alignment.
SendMessage(new HandleRef(this, Handle),
EM_SETPARAFORMAT,
SCF_SELECTION, ref fmt);
}
}

/// <summary>
/// This member overrides
/// <see cref="Control"/>.OnHandleCreated.
/// </summary>
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);

// Enable support for justification.
SendMessage(new HandleRef(this, Handle),
EM_SETTYPOGRAPHYOPTIONS,
TO_ADVANCEDTYPOGRAPHY,
TO_ADVANCEDTYPOGRAPHY);
}

private int updating = 0;
private int oldEventMask = 0;

// Constants from the Platform SDK.
private const int EM_SETEVENTMASK = 1073;
private const int EM_GETPARAFORMAT = 1085;
private const int EM_SETPARAFORMAT = 1095;
private const int EM_SETTYPOGRAPHYOPTIONS = 1226;
private const int WM_SETREDRAW = 11;
private const int TO_ADVANCEDTYPOGRAPHY = 1;
private const int PFM_ALIGNMENT = 8;
private const int SCF_SELECTION = 1;

// It makes no difference if we use PARAFORMAT or
// PARAFORMAT2 here, so I have opted for PARAFORMAT2.
[StructLayout(LayoutKind.Sequential)]
private struct PARAFORMAT
{
public int cbSize;
public uint dwMask;
public short wNumbering;
public short wReserved;
public int dxStartIndent;
public int dxRightIndent;
public int dxOffset;
public short wAlignment;
public short cTabCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public int[] rgxTabs;

// PARAFORMAT2 from here onwards.
public int dySpaceBefore;
public int dySpaceAfter;
public int dyLineSpacing;
public short sStyle;
public byte bLineSpacingRule;
public byte bOutlineLevel;
public short wShadingWeight;
public short wShadingStyle;
public short wNumberingStart;
public short wNumberingStyle;
public short wNumberingTab;
public short wBorderSpace;
public short wBorderWidth;
public short wBorders;
}

[DllImport("user32", CharSet = CharSet.Auto)]
private static extern int SendMessage(HandleRef hWnd,
int msg,
int wParam,
int lParam);

[DllImport("user32", CharSet = CharSet.Auto)]
private static extern int SendMessage(HandleRef hWnd,
int msg,
int wParam,
ref PARAFORMAT lp);
}

/// <summary>
/// Specifies how text in a <see cref="AdvRichTextBox"/> is
/// horizontally aligned.
/// </summary>
public enum TextAlign
{
/// <summary>
/// The text is aligned to the left.
/// </summary>
Left = 1,

/// <summary>
/// The text is aligned to the right.
/// </summary>
Right = 2,

/// <summary>
/// The text is aligned in the center.
/// </summary>
Center = 3,

/// <summary>
/// The text is justified.
/// </summary>
Justify = 4
}


بعد ازاین مرحله تو Design فرمتون برید می بینید که تو تولباکس تون یک کنترل ایجاد شده به همون نام.
کافیست که اونو تو پروژتون اضافه کنید.
حالا خاصیت Justify هم اونجا وجود داره

موفق باشید
یا علی

sokote_bi_payan
شنبه 08 آبان 1389, 08:52 صبح
اینم نمونه برنامه

sds1920
چهارشنبه 22 تیر 1390, 19:56 عصر
اینم نمونه برنامه

این خیلی عالیه و درست کار می کنه فقط برای زبان های مثل فارسی که راست چین (RightToLEft)هستند یک مشکلی داره.وقتی Justify می کنیم Left Justify میشه در صورتی که باید باید Right Justify باشه.عکس زیر رو ببنید خودتون متوجه می شید چی میگم.

72405

اگر به خط آخر که کامل نیست نگاه کنید می بینید که به جای اینکه سمت راست باشه رفته سمت چپ.از دوستان کسی را حلی برای این مشکل نداره؟
ممنون

sds1920
پنج شنبه 23 تیر 1390, 19:56 عصر
نظری،فرمایشی،نصیحتی،چیزی نبود؟:متفکر: