Justify alignment in c# ritchtextbox control!!!
-
hi dear stewart thanks for your reply may i ask you to wrtie sendmessage methode code for me? best regards hassan azizi
[DllImport("user32.dll")]
private static extern int SendMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int msg,
int wParam,
ref PARAMFORMAT2 format);This signature is specifically for your needs. This will marshal things correctly without having to do it yourself. A generic
SendMessage
declaration would look like:[DllImport("user32.dll")]
private static extern IntPtr SendMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int msg,
IntPtr wParam,
IntPtr lParam);If you're not sure how to do this, you're going to have to learn. Interop with unmanaged code also requires some knowledge with the unmanaged APIs you're dealing with - in this case, the Windows Common Controls. For instance, you're going to have to declare a managed structure for the unmanaged
PARAFORMAT2
structure documented in the Platform SDK for the Windows Common Controls.Microsoft MVP, Visual C# My Articles
-
[DllImport("user32.dll")]
private static extern int SendMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int msg,
int wParam,
ref PARAMFORMAT2 format);This signature is specifically for your needs. This will marshal things correctly without having to do it yourself. A generic
SendMessage
declaration would look like:[DllImport("user32.dll")]
private static extern IntPtr SendMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int msg,
IntPtr wParam,
IntPtr lParam);If you're not sure how to do this, you're going to have to learn. Interop with unmanaged code also requires some knowledge with the unmanaged APIs you're dealing with - in this case, the Windows Common Controls. For instance, you're going to have to declare a managed structure for the unmanaged
PARAFORMAT2
structure documented in the Platform SDK for the Windows Common Controls.Microsoft MVP, Visual C# My Articles
hi would i pass handle of richtextbox to sendmessage function? thanx hassan azizi
-
hi would i pass handle of richtextbox to sendmessage function? thanx hassan azizi
Yes, using its
Handle
property.Microsoft MVP, Visual C# My Articles
-
Yes, using its
Handle
property.Microsoft MVP, Visual C# My Articles
hi - how i can find numeric values for API params like EM_SETPARAFORMAT ans so? - how i can find refrence for windows dlls(like kernel32,shell32,user32) methods? - have i to set all parameters value of PARAFORMAT2 structure or not? thanx
-
hi - how i can find numeric values for API params like EM_SETPARAFORMAT ans so? - how i can find refrence for windows dlls(like kernel32,shell32,user32) methods? - have i to set all parameters value of PARAFORMAT2 structure or not? thanx
You can find all this information in the Platform SDK, both in the documentation and in the C/C++ header files, like winuser.h and commctrl.h (most common defs). You should also check on http://pinvoke.net[^] for the managed signatures for unmanaged functions.
Microsoft MVP, Visual C# My Articles
-
You can find all this information in the Platform SDK, both in the documentation and in the C/C++ header files, like winuser.h and commctrl.h (most common defs). You should also check on http://pinvoke.net[^] for the managed signatures for unmanaged functions.
Microsoft MVP, Visual C# My Articles
well done i found it : #define EM_SETPARAFORMAT (WM_USER + 71) but what is tha value of WM_USER in my use? thanx so much stewart
-
well done i found it : #define EM_SETPARAFORMAT (WM_USER + 71) but what is tha value of WM_USER in my use? thanx so much stewart
That's also a pre-prof def you'll find in winuser.h. I'd recommend indexing your various include folders to make them easier to find, or use a good text editor like VIM[^] along with ctags[^] to easily find tokens such as this while typing code in the text editor.
Microsoft MVP, Visual C# My Articles
-
That's also a pre-prof def you'll find in winuser.h. I'd recommend indexing your various include folders to make them easier to find, or use a good text editor like VIM[^] along with ctags[^] to easily find tokens such as this while typing code in the text editor.
Microsoft MVP, Visual C# My Articles
ifound that : #define WM_USER 0x0400 i use send message like below : .......... public const int EM_SETPARAFORMAT = 1095; [DllImport("user32.dll")] private static extern int SendMessage(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] int msg, int wParam, ref PARAFORMAT2 format); [StructLayout(LayoutKind.Sequential)] public struct PARAFORMAT2 { public UInt16 cbSize; public UInt64 dwMask; public double wNumbering; public double wEffects; public long dxStartIndent; public long dxRightIndent; public long dxOffset; public double wAlignment; public short cTabCount; public long[] rgxTabs; public long dySpaceBefore; public long dySpaceAfter; public long dyLineSpacing; public short sStyle; public Byte bLineSpacingRule; public Byte bOutlineLevel; public double wShadingWeight; public double wShadingStyle; public double wNumberingStart; public double wNumberingStyle; public double wNumberingTab; public double wBorderSpace; public double wBorderWidth; public double wBorders; }; PARAFORMAT2 Format = new PARAFORMAT2(); Format.dwMask = 0x0008; Format.wAlignment = 0x0004; Format.wNumbering = 0; Format.cbSize = (UInt16)Marshal.SizeOf(Format); Format.wEffects = 0x00400000; Format.rgxTabs = new long[]{0}; Format.cTabCount = 1; Format.bOutlineLevel= 0; SendMessage(PickControl.SelectedControl.Handle,EM_SETPARAFORMAT,0,ref Format); ........... but it's execution takes no effect to my richtextbox! and sendmessage returns zero value! what is my problem?
-
ifound that : #define WM_USER 0x0400 i use send message like below : .......... public const int EM_SETPARAFORMAT = 1095; [DllImport("user32.dll")] private static extern int SendMessage(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] int msg, int wParam, ref PARAFORMAT2 format); [StructLayout(LayoutKind.Sequential)] public struct PARAFORMAT2 { public UInt16 cbSize; public UInt64 dwMask; public double wNumbering; public double wEffects; public long dxStartIndent; public long dxRightIndent; public long dxOffset; public double wAlignment; public short cTabCount; public long[] rgxTabs; public long dySpaceBefore; public long dySpaceAfter; public long dyLineSpacing; public short sStyle; public Byte bLineSpacingRule; public Byte bOutlineLevel; public double wShadingWeight; public double wShadingStyle; public double wNumberingStart; public double wNumberingStyle; public double wNumberingTab; public double wBorderSpace; public double wBorderWidth; public double wBorders; }; PARAFORMAT2 Format = new PARAFORMAT2(); Format.dwMask = 0x0008; Format.wAlignment = 0x0004; Format.wNumbering = 0; Format.cbSize = (UInt16)Marshal.SizeOf(Format); Format.wEffects = 0x00400000; Format.rgxTabs = new long[]{0}; Format.cTabCount = 1; Format.bOutlineLevel= 0; SendMessage(PickControl.SelectedControl.Handle,EM_SETPARAFORMAT,0,ref Format); ........... but it's execution takes no effect to my richtextbox! and sendmessage returns zero value! what is my problem?
Do you know anything about native and managed data types? Your structure is completely wrong, thus it's not being marshalled correctly which explains why you're not seeing anything change. A
LONG
in C is not aInt64
(orlong
alias, which is recommended for readibility) but instead aInt32
(orint
). The structure should be defined like this:[StructLayout(LayoutKind.Sequential)]
public struct ParaFormat2
{
[MarshalAs(UnmanagedType.SysUInt)] public IntPtr cbSize;
[MarshalAs(UnmanagedType.U4)] public int dwMask;
[MarshalAs(UnmanagedType.U2)] public short wNumbering;
[MarshalAs(UnmanagedType.U2)] public short wEffects;
public int dxStartIndent; // A LONG is a 32-bit signed integer!
public int dxRightIndent;
public int dxOffset;
[MarshalAs(UnmanagedType.U2)] public short wAlignment;
public short cTagCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)] public int[] rgxTabs;
public int dySpaceBefore;
public int dySpaceAfter;
public int dylineSpacing;
public short sStyle;
public byte blineSpacingRule;
public byte bOutlineLevel;
[MarshalAs(UnmanagedType.U2)] public short wShadingWeight;
[MarshalAs(UnmanagedType.U2)] public short wShadingStyle;
[MarshalAs(UnmanagedType.U2)] public short wNumberingStart;
[MarshalAs(UnmanagedType.U2)] public short wNumberingStyle;
[MarshalAs(UnmanagedType.U2)] public short wNumberingTab;
[MarshalAs(UnmanagedType.U2)] public short wBorderSpace;
[MarshalAs(UnmanagedType.U2)] public short wBorderWidth;
[MarshalAs(UnmanagedType.U2)] public short wBorders;
}The
cbSize
is defined as anIntPtr
and marshalled asUnmanagedType.SysUInt
because aUINT
is defined as anunsigned int
, whereint
is a platform-specific type. For 32-bit OSes, it's 32 bits. For 64-bit OSes, it's 64 bits. The OS, of course, is dependent on the bit-width of the processor. To create the struct you need, do something like this:ParaFormat2 format = new ParaFormat2();
format = new IntPtr(Marshal.SizeOf(typeof(ParaFormat2)));
format.dwMask = 8; // PFM_ALIGNMENT mask
format.wAlignment = 4; // PFA_JUSTIFY
SendMessage(richTextBox1.Handle, 1095, 0, ref format);Microsoft MVP,
-
Do you know anything about native and managed data types? Your structure is completely wrong, thus it's not being marshalled correctly which explains why you're not seeing anything change. A
LONG
in C is not aInt64
(orlong
alias, which is recommended for readibility) but instead aInt32
(orint
). The structure should be defined like this:[StructLayout(LayoutKind.Sequential)]
public struct ParaFormat2
{
[MarshalAs(UnmanagedType.SysUInt)] public IntPtr cbSize;
[MarshalAs(UnmanagedType.U4)] public int dwMask;
[MarshalAs(UnmanagedType.U2)] public short wNumbering;
[MarshalAs(UnmanagedType.U2)] public short wEffects;
public int dxStartIndent; // A LONG is a 32-bit signed integer!
public int dxRightIndent;
public int dxOffset;
[MarshalAs(UnmanagedType.U2)] public short wAlignment;
public short cTagCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)] public int[] rgxTabs;
public int dySpaceBefore;
public int dySpaceAfter;
public int dylineSpacing;
public short sStyle;
public byte blineSpacingRule;
public byte bOutlineLevel;
[MarshalAs(UnmanagedType.U2)] public short wShadingWeight;
[MarshalAs(UnmanagedType.U2)] public short wShadingStyle;
[MarshalAs(UnmanagedType.U2)] public short wNumberingStart;
[MarshalAs(UnmanagedType.U2)] public short wNumberingStyle;
[MarshalAs(UnmanagedType.U2)] public short wNumberingTab;
[MarshalAs(UnmanagedType.U2)] public short wBorderSpace;
[MarshalAs(UnmanagedType.U2)] public short wBorderWidth;
[MarshalAs(UnmanagedType.U2)] public short wBorders;
}The
cbSize
is defined as anIntPtr
and marshalled asUnmanagedType.SysUInt
because aUINT
is defined as anunsigned int
, whereint
is a platform-specific type. For 32-bit OSes, it's 32 bits. For 64-bit OSes, it's 64 bits. The OS, of course, is dependent on the bit-width of the processor. To create the struct you need, do something like this:ParaFormat2 format = new ParaFormat2();
format = new IntPtr(Marshal.SizeOf(typeof(ParaFormat2)));
format.dwMask = 8; // PFM_ALIGNMENT mask
format.wAlignment = 4; // PFA_JUSTIFY
SendMessage(richTextBox1.Handle, 1095, 0, ref format);Microsoft MVP,
thanks stewart it worked nice! but i have another problem : my language is right to left when i send message to justify text align the richtextbox RightToLeft property becomes to RightToLeft.NO but it's first value was RightToLeft.Yes! and setting that again to RightToLeft.Yes does not affect the text! it only works properly when RightToLeft property value is RightToLeft.No! what can i do? how can i have deep study on native and managed data types and marshalling? do i need special resources to do this study? best wishes
-
thanks stewart it worked nice! but i have another problem : my language is right to left when i send message to justify text align the richtextbox RightToLeft property becomes to RightToLeft.NO but it's first value was RightToLeft.Yes! and setting that again to RightToLeft.Yes does not affect the text! it only works properly when RightToLeft property value is RightToLeft.No! what can i do? how can i have deep study on native and managed data types and marshalling? do i need special resources to do this study? best wishes
Study the Platform SDK and the .NET Framework SDK. Get some experience writing native C/C++ applications which use Windows messaging, perhaps even extending the common controls.
Microsoft MVP, Visual C# My Articles
-
thanks stewart it worked nice! but i have another problem : my language is right to left when i send message to justify text align the richtextbox RightToLeft property becomes to RightToLeft.NO but it's first value was RightToLeft.Yes! and setting that again to RightToLeft.Yes does not affect the text! it only works properly when RightToLeft property value is RightToLeft.No! what can i do? how can i have deep study on native and managed data types and marshalling? do i need special resources to do this study? best wishes