Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Justify alignment in c# ritchtextbox control!!!

Justify alignment in c# ritchtextbox control!!!

Scheduled Pinned Locked Moved C#
questioncsharp
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Heath Stewart

    The RichTextBox.SelectionAlignment property (of type HorizontalAlignment) doesn't not support justification. However, you can do this by P/Invoking SendMessage and sending the EM_SETPARAFORMAT message with a PARAFORMAT2 structure with the dwMask field set to PFM_ALIGNMENT (0x0008) at least, and the wAlignment set to PFA_JUSTIFY (0x0004). This applies to the whole current paragraph and is only supported in the Rich Edit common control (which is encapsulated by the RichTextBox) 2.0, which probably shouldn't be a problem on modern Windows OSes on which your application would be running; otherwise, you can always deploy a new common controls library with your product.

    Microsoft MVP, Visual C# My Articles

    H Offline
    H Offline
    hassan azizi
    wrote on last edited by
    #3

    hi dear stewart thanks for your reply may i ask you to wrtie sendmessage methode code for me? best regards hassan azizi

    H 1 Reply Last reply
    0
    • H hassan azizi

      hi dear stewart thanks for your reply may i ask you to wrtie sendmessage methode code for me? best regards hassan azizi

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #4

      [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

      H 1 Reply Last reply
      0
      • H Heath Stewart

        [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

        H Offline
        H Offline
        hassan azizi
        wrote on last edited by
        #5

        hi would i pass handle of richtextbox to sendmessage function? thanx hassan azizi

        H 1 Reply Last reply
        0
        • H hassan azizi

          hi would i pass handle of richtextbox to sendmessage function? thanx hassan azizi

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #6

          Yes, using its Handle property.

          Microsoft MVP, Visual C# My Articles

          H 1 Reply Last reply
          0
          • H Heath Stewart

            Yes, using its Handle property.

            Microsoft MVP, Visual C# My Articles

            H Offline
            H Offline
            hassan azizi
            wrote on last edited by
            #7

            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

            H 1 Reply Last reply
            0
            • H hassan azizi

              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

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #8

              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

              H 1 Reply Last reply
              0
              • H Heath Stewart

                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

                H Offline
                H Offline
                hassan azizi
                wrote on last edited by
                #9

                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

                H 1 Reply Last reply
                0
                • H hassan azizi

                  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

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #10

                  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

                  H 1 Reply Last reply
                  0
                  • H Heath 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

                    H Offline
                    H Offline
                    hassan azizi
                    wrote on last edited by
                    #11

                    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?

                    H 1 Reply Last reply
                    0
                    • H hassan azizi

                      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?

                      H Offline
                      H Offline
                      Heath Stewart
                      wrote on last edited by
                      #12

                      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 a Int64 (or long alias, which is recommended for readibility) but instead a Int32 (or int). 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 an IntPtr and marshalled as UnmanagedType.SysUInt because a UINT is defined as an unsigned int, where int 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,

                      H 1 Reply Last reply
                      0
                      • H Heath Stewart

                        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 a Int64 (or long alias, which is recommended for readibility) but instead a Int32 (or int). 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 an IntPtr and marshalled as UnmanagedType.SysUInt because a UINT is defined as an unsigned int, where int 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,

                        H Offline
                        H Offline
                        hassan azizi
                        wrote on last edited by
                        #13

                        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

                        H Z 2 Replies Last reply
                        0
                        • H hassan azizi

                          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

                          H Offline
                          H Offline
                          Heath Stewart
                          wrote on last edited by
                          #14

                          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

                          1 Reply Last reply
                          0
                          • H hassan azizi

                            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

                            Z Offline
                            Z Offline
                            ZyX
                            wrote on last edited by
                            #15

                            Thanks for the Justify module. Have you ever found the solution to the RightToLeft problem? This problem also appears when using RightToLeft fonts. Cheers!

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups