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
Solved

API-Call to change Color of Textbox

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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