Solved

COLORREF with SetTextColor()

Posted on 1998-07-09
12
1,429 Views
Last Modified: 2013-11-19
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
Comment
Question by:ichor
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1318962
The question is not clear.
0
 

Author Comment

by:ichor
ID: 1318963
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
 
LVL 23

Expert Comment

by:chensu
ID: 1318964
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:ichor
ID: 1318965
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
 
LVL 23

Expert Comment

by:chensu
ID: 1318966
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
 
LVL 2

Expert Comment

by:warmcat
ID: 1318967
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
 
LVL 1

Expert Comment

by:chacko
ID: 1318968
After you change the color, try Invalidate and UpdateWindow.
0
 

Author Comment

by:ichor
ID: 1318969
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
 
LVL 1

Accepted Solution

by:
IgorGrebnev earned 150 total points
ID: 1318970
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
 

Author Comment

by:ichor
ID: 1318971
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
 
LVL 1

Expert Comment

by:IgorGrebnev
ID: 1318972
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
 

Author Comment

by:ichor
ID: 1318973
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expand macro to include alternate header placement and naming 5 71
Window placement 17 80
Not needed 13 109
Way to decrease size of apk file 9 65
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now