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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. EM_SETCHARFORMAT problem

EM_SETCHARFORMAT problem

Scheduled Pinned Locked Moved C#
jsonhelpannouncement
5 Posts 2 Posters 5 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.
  • C Offline
    C Offline
    c guy3811
    wrote on last edited by
    #1

    I am trying to set the back color of indavidual charachters in a RichTextBox by using SendMessage() and the charformat2 structure. This works setting the forecolor of the charachters, but the back color will not work. I know that my version of RichEdit supports backcolors because they will show up if I paste, just not with the code. I thought that maybe it was assigning the wrong size to cbSize, but I dont know why it would do that. I am missing something very simple here im sure. I have tried this with the API call and have the same result. The message returns a non zero value, meaning it was successful, but the RichTextBox doesnt reflect that. ... private const int CFM_BACKCOLOR=0x4000000; ..... CHARFORMAT2 cf2=new CHARFORMAT2(); cf2.dwMask=CFM_BACKCOLOR; cf2.cbSize=Marshal.SizeOf(cf2); cf2.crBackColor=ColorTranslator.ToWin32(Color.Red); IntPtr lParam=Marshal.AllocCoTaskMem(Marshal.SizeOf(cf2)); Marshal.StructureToPtr(cf2, lParam, false); Message msg=Message.Create(this.Handle, EM_SETCHARFORMAT, (IntPtr)SCF_SELECTION, lParam); DefWndProc(ref msg); Marshal.FreeCoTaskMem(lParam); Thanks!

    M 1 Reply Last reply
    0
    • C c guy3811

      I am trying to set the back color of indavidual charachters in a RichTextBox by using SendMessage() and the charformat2 structure. This works setting the forecolor of the charachters, but the back color will not work. I know that my version of RichEdit supports backcolors because they will show up if I paste, just not with the code. I thought that maybe it was assigning the wrong size to cbSize, but I dont know why it would do that. I am missing something very simple here im sure. I have tried this with the API call and have the same result. The message returns a non zero value, meaning it was successful, but the RichTextBox doesnt reflect that. ... private const int CFM_BACKCOLOR=0x4000000; ..... CHARFORMAT2 cf2=new CHARFORMAT2(); cf2.dwMask=CFM_BACKCOLOR; cf2.cbSize=Marshal.SizeOf(cf2); cf2.crBackColor=ColorTranslator.ToWin32(Color.Red); IntPtr lParam=Marshal.AllocCoTaskMem(Marshal.SizeOf(cf2)); Marshal.StructureToPtr(cf2, lParam, false); Message msg=Message.Create(this.Handle, EM_SETCHARFORMAT, (IntPtr)SCF_SELECTION, lParam); DefWndProc(ref msg); Marshal.FreeCoTaskMem(lParam); Thanks!

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      You'll also have to clear the CFE_AUTOBACKCOLOR flag or your background color will be ignored. Regards, mav

      C 1 Reply Last reply
      0
      • M mav northwind

        You'll also have to clear the CFE_AUTOBACKCOLOR flag or your background color will be ignored. Regards, mav

        C Offline
        C Offline
        c guy3811
        wrote on last edited by
        #3

        Do you mean to set dwEffects=0? I tried that before I posted and nothing. If that isnt what you meant could you elaborate? Thanks!

        M 1 Reply Last reply
        0
        • C c guy3811

          Do you mean to set dwEffects=0? I tried that before I posted and nothing. If that isnt what you meant could you elaborate? Thanks!

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          You have to set dwMask to CFM_BACKCOLOR|CFM_COLOR and be sure NOT to set CFE_AUTOBACKCOLOR in dwEffects. Here's the description of the flags you can use: CHARFORMAT2 struct on MSDN[^] Regards, mav

          C 1 Reply Last reply
          0
          • M mav northwind

            You have to set dwMask to CFM_BACKCOLOR|CFM_COLOR and be sure NOT to set CFE_AUTOBACKCOLOR in dwEffects. Here's the description of the flags you can use: CHARFORMAT2 struct on MSDN[^] Regards, mav

            C Offline
            C Offline
            c guy3811
            wrote on last edited by
            #5

            I tried all of that before I posted, and tried it again after you posted, and still, nothing. I tried dwEffects=0, dwEffects=CFE_PROTECTED, not setting dwEffects, etc. Nothing has worked. Like I said, the call to DefWndProc is returning a non zero value, so it is working, just not how I want it to. There has to be something I am missing somewhere... I am going to list all my constants, structure, etc, and perhaps someone can figure out what is going on. FYI, I have also tried the API call and it does the same thing. [StructLayout(LayoutKind.Sequential)] private struct CHARFORMAT2 { public int cbSize; public int dwMask; public int dwEffects; public int yHeight; public int yOffset; public int crTextColor; public byte bCharSet; public byte bPitchAndFamily; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public string szFaceName; public int wWeight; public int sSpacing; public int crBackColor; public int lcid; public int dwReserved; public int sStyle; public int wKerning; public byte bUnderlineType; public byte bAnimation; public byte bRevAuthor; public byte bReserved1; }//struct charformat2 //constants EM_SETCHARFORMAT=0x444; CFM_BACKCOLOR=0x4000000; CFM_COLOR=0x40000000; SCF_SELECTION=0x1; //Call to format, tried CFM_BACKCOLOR and CFM_BACKCOLOR|CFM_COLOR, etc, // only forecolor changes CHARFORMAT2 cf2=new CHARFORMAT2(); cf2.dwMask=CFM_BACKCOLOR; cf2.cbSize=Marshal.SizeOf(cf2); cf2.crBackColor=ColorTranslator.ToWin32(Color.Red); IntPtr lParam=Marshal.AllocCoTaskMem(Marshal.SizeOf(cf2)); Marshal.StructureToPtr(cf2, lParam, false); Message msg=Message.Create(this.Handle, EM_SETCHARFORMAT, (IntPtr)SCF_SELECTION, lParam); DefWndProc(ref msg); Marshal.FreeCoTaskMem(lParam);

            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