Solved

API-Call to change Color of Textbox

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

785 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