• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1757
  • Last Modified:

COLORREF with SetTextColor()

I'm trying to give SetTextColor() a COLORREF value (m_Color) for predefined colors in a simple application.
Right now, I have it as:

SetTextColor(hdc, m_Color);

This, however, does it work.  I have 4 predefined colors (Black, R, G, B), and I'm just trying to change text colors with those.  Any ideas?
0
ichor
Asked:
ichor
  • 5
  • 3
  • 2
  • +2
1 Solution
 
chensuCommented:
The question is not clear.
0
 
ichorAuthor Commented:
Basically, I have made 4 predefined colors for a simple text editor.  I'm trying to make these 4 predefined colors change the color of the font.

Clear?
0
 
chensuCommented:
Not enough.

What are the RGB values for the for colors? What's the problem?

I guess the colors are not in the 20 colors reserved by Windows. In this case, you must create a color palette to do it.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ichorAuthor Commented:
The four colors I have are black, red, green, and blue.

RGB(255,0,0)
RGB(0,255,0)
RGB(0,0,255)

The problem that I'm having...those defined colors (Using the Windows/MFC defined COLORREF type (32 bit) won't make the text change colors.

See...Here's what I've been trying:

SetTextColor(hdc, m_Color);

m_Color is the user-selected, pre-defined color (built into the program.)

This didn't work, so I tried:

SetTextColor(hdc, RGB(255,0,0));

for red, of course...and it didn't work either.  I can't get any of my RGB variables or values to apply to the text.


I'm not sure what else I could say to explain this more...need any more info?
0
 
chensuCommented:
What text are you talking about? The system will use this text color only when writing text to this device context. For example,

CClientDC dc(this);
dc.SetTextColor(RGB(255,0,0));
dc.TextOut(10, 10, CString(_T("ABC")));

The text ABC will be red.
0
 
warmcatCommented:
Ichor - lovely handle -

You cannot use SetTextColor as a one-off initialisation.  You must use it just prior to writing the text in your CView::OnDraw() or whatever.  Is this what you were doing?

-Andy
0
 
chackoCommented:
After you change the color, try Invalidate and UpdateWindow.
0
 
ichorAuthor Commented:
Maybe if I put more code...

In a header file, I have:

const COLORRREF BLUE = RGB(0,0,255);

I have this for each color that I want the text to be made.

I called the current color m_Color.

This is what I have for when it's supposed to change colors:

void CIeditView::OnColorsBlue()
{
      
      m_Color = BLUE;
      HDC hdc;
      SetTextColor(hdc, m_Color);
}

I have also tried replacing the last line with:

SetTextColor(hdc, BLUE);  //didn't work.

and

SetTextColor(hdc, RGB(0,0,255); //didn't work.


I know I'm probably leaving something out...still haven't found out what.

And, I increased this question to 150 points...whoever has an answer that allows me to write text to the screen in color will get their answer graded "excellent."  (Please be as detailed as possible, though.)

Personally, I didn't think this question was very hard...oh well.  =)


-Ichor

0
 
IgorGrebnevCommented:
Dear ichor,
You need explicitly initialize text color before calling of TextOut function. From example I understance that you use MFC, then TextOut is called from OnDraw:

void CIeditView::OnDraw( CDC *pDC )

{ pDC->SetTextColor( m_Color);
  CString message = "Hello Colorful World";
  pDC->TextOut( 10, 10, message );  
}

How you initialize the m_Color.

Declare it in header:
COLORREF m_Color;

In constructor of  init it to the value you like:

CIeditView::CIeditView()
{ m_Color = RGB( 0, 128, 0 ); // Dark Green
}

In mesage response funcitons:

#define BLUE RGB( 0, 0, 255 )
 
void CIeditView::OnColorsBlue()
{
  m_Color = RGB( 0, 0, 255 ); // or m_Color = BLUE;  
  InvalidateRect( NULL 0;     // Cause OnDraw to be called.
}

This example should work.

The main idea that all painting happens in OnDraw funciton and this defaul text color for the pDC is black.
Your mistakes

void CIeditView::OnColorsBlue()
{

  m_Color = BLUE;
  HDC hdc; // Declare HDC. Where is initialisation???

// Even if DC was initialised, you do not draw anything with //it!!!. OnDraw funciton gets another instance of device //contexts!!! SetText color changes settings of this instance of // HDC. They are lost after you call RelaseDC.
  SetTextColor(hdc, m_Color);
 
}

Sincerely yours, Igor  

0
 
ichorAuthor Commented:
Well, I'm not going to try that out (yet), but it seems right...so I'm going to "accept" it.

However, one more question:

That will work with text that's typed (not compiled), correct?

Thanks again!

-Ichor
0
 
IgorGrebnevCommented:
Dear ichor,

What do you mean by compiled or typed text? You may print with TextOut hardcoded text:



void CIeditView::OnDraw( CDC *pDC )

{ pDC->SetTextColor( m_Color);
 CString message = "Hello Colorful World";
 pDC->TextOut( 10, 10, message );
//  Or
 pDC->TextOut( 10, 50, "Second Hello", strlen("Second Hello") );
// Or some string dynamicly assignd in prev operation
 pDC->TextOut( 10, 100, m_PrevAssignedString );      
}

Yours,
Igor
0
 
ichorAuthor Commented:
Well, nothing has worked, and I have decided to abandon efforts for the time being.

If anyone has time (and is willing to help), feel free to email me at:  ichor@sprintmail.com

I'll be most greatfull...It's very frusterating doing a simple task and having it not work...after trying it a dozen different ways.

Thanks again everybody,

-Ichor
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now