Solved

API-Call to change Color of Textbox

Posted on 1998-09-09
4
358 Views
Last Modified: 2008-02-01
Hi,

I am calling a C++-Function from a Visual Basic App. From within the C++-Function I want to change the textcolor of a Textbox, which is placed in the VB-form. I give the hwnd of the textbox to the C++-function.
Can anyone please write me, which API-Function (SendMessage??) I have to use to change the textcolor.

Thanks,

Thomas Kramer
0
Comment
Question by:thomaskramer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1172345
You need to handle the WM_CTLCOLORSTATIC or WM_CTLCOLORTEXT messages.  Details follow.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1172346
When a static text or edit text window needs to paint itsself, it sends one of these messages to its parent window.  The DC that the window will paint with is passed in the parameters.  The parent window's handler code can alter this DC.  It can set the text color (SetTextColor()) and the background color (SetBkColor()).  It can return a handle to a brush that will be used to paint the background of the window.  Note that this brush is not deleted, so you should not be creating the brush on each call (This is s common mistake).

Let me know if you have questions.
0
 

Author Comment

by:thomaskramer
ID: 1172347
nietod, I am not really sure, how I can realize. Can you please give an example, how to change the color of the textbox from within the C++-Code.
Thanks
0
 
LVL 22

Expert Comment

by:nietod
ID: 1172348
What do you want to change?  the border?  the background?  the text?  
Is this a static text control?  (uneditable text, usually with no border) Or is this an edit box? (editable text).

The following example will produce a red background and green text in an edit box.

// declare brush global handle along with other globals somewhere.
HBRUSH RedBrush = CreateSolidBrush(RGB(255,0,0));

       *     *     *

// Inside the window procedure for the edit box's parent window, you need.
   case WM_CTLCOLOREDIT:
   {
      HDC DCHnd = (HDC *) wParam; // Get handle to DC to be used to draw control.
      SetTextColor(DCHnd,RGB(0,255,0)); // Set text to green.
      SetBkColor(DCHnd,RGB(255,0,0)); // Set background to red (might not be necessary, but I think it is).
      return RedBrush; // Return handle to brush to paint background with.
   }
0

Featured Post

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!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 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