Solved

COLORREF with SetTextColor()

Posted on 1998-07-09
12
1,393 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

13 Experts available now in Live!

Get 1:1 Help Now