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 Question

EM_SETCHARFORMAT Question

Scheduled Pinned Locked Moved C#
questionjson
3 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.
  • C Offline
    C Offline
    c guy3811
    wrote on last edited by
    #1

    I posted this once before, but it is about 20 pages back now. I am using EM_SETCHARFORMAT to try and set the background color of charachters in a RichTextBox, but it wont work. I have tried everything I can think of, and nothing. When I try and set the forecolor, that works, and I can paste in charachters with a set backcolor and it works fine, the code just isnt working how I want it to... I tried dwEffects=0, dwEffects=CFE_PROTECTED, not setting dwEffects, etc. Nothing has worked. 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);

    M 1 Reply Last reply
    0
    • C c guy3811

      I posted this once before, but it is about 20 pages back now. I am using EM_SETCHARFORMAT to try and set the background color of charachters in a RichTextBox, but it wont work. I have tried everything I can think of, and nothing. When I try and set the forecolor, that works, and I can paste in charachters with a set backcolor and it works fine, the code just isnt working how I want it to... I tried dwEffects=0, dwEffects=CFE_PROTECTED, not setting dwEffects, etc. Nothing has worked. 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);

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

      Hi again! Now I've tried your code and found that your struct definition is not correct. For example, wWeight and sSpacing are not int (32 bit), but UInt16 (16 bit). Same with sStyle and wKerning. Because your struct declaration is wrong, the bytes are shifted and thus do not work. Regards, mav

      A 1 Reply Last reply
      0
      • M mav northwind

        Hi again! Now I've tried your code and found that your struct definition is not correct. For example, wWeight and sSpacing are not int (32 bit), but UInt16 (16 bit). Same with sStyle and wKerning. Because your struct declaration is wrong, the bytes are shifted and thus do not work. Regards, mav

        A Offline
        A Offline
        arow69d
        wrote on last edited by
        #3

        Thanks. All I had to go by was the struct definition on MSDN's website, and it was for c++. I wasnt sure exactly what the exact conversions were for each of the elements, but I knew it had to be something like that. It definately makes sense though. I appreciate your help! Thanks!

        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