Solved

COLORREF with SetTextColor()

Posted on 1998-07-09
12
1,470 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 85
C++ BOOL WINAPI ReadFile fails on windows 10 when reading from USB cable 9 517
sum13 challenge 24 134
Fibonacci challenge 11 136
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 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