Solved

API-Call to change Color of Textbox

Posted on 1998-09-09
4
357 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 104
IdTCPClient1->Disconnect(); not working 3 86
object oriented programming comparison 5 86
How to convert Structure to vector of byte and vice versa 5 31
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

733 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