Solved

using Setbkcolor???

Posted on 1997-04-03
5
1,580 Views
Last Modified: 2012-05-04
How do I use the setbkcolor API for a textbox by passing a window handle?
This is what I am doing:
1.declaring the setbkcolor and getDC  API's at the module level.
2.At a form load, I pass the handle (hWnd) of my textbox to the GetDC API, to convert the hWnd handle to a hDC as follows:
l = getDC(text.hWnd) (BTW, l is a global long variable)
3. I call SetBkColor API as follows:
setBkcolor(l, RGB(255,0,0): this returns the value 255 meaning the code is being executed; but does not change the backcolor of my textbox to Red as it is supposed to.
What may be wrong??
Stepwise Suggestions welcome!!
SC.
0
Comment
Question by:schhaya
5 Comments
 
LVL 1

Expert Comment

by:andyb013197
ID: 1425032
Why is the getDC in the form load? Wouldnt it be better to put it next to the setbkcolor api call? Storing hdc's in variables for a period of time is generally frowned upon becuase they may change... Especially since you are doing this in the form load, your returned hdc may not be what you expect.

It is also worth splitting out the rgb function (and maybe the text.hwnd) from the api call and assigning it to long instead. VB can do some strange things with parameters as functions.


0
 

Author Comment

by:schhaya
ID: 1425033
The getDC(hwnd) API returns the hDC of the object whose hWnd is passed as an argument to the API. The windowfromDC(hDC) API gives back the hWnd from the hDC. When I verify the hWnd  using the WindowfromDC, it returns the exact same hWnd of the object as before. Therefore, I dont think the hDC is changing due to which the setBkcolor is not carried out. Anyways, I tried out your suggestion and put the GetDC in the immediately previous step to calling the SetBkcolor API. Still no result. As I said before, the code itself is being executed; it returns the value of 255 (which is the long value of the color Red); but does not set the backcolor of the object to Red.
0
 
LVL 1

Expert Comment

by:BabyFace
ID: 1425034
I suppose that you wish to change the backcolor right? Like the property of the Text1.Backcolor.
Well you ought to know that API func. SetBkColor isn't supposed to set the background color property like the func. names describes. I think. The describtion for the func. is this: Sets the background color for the specified device context. The background color is used to fill the gaps in hatched brushses, dashed pens, and characters if the background mode is OPAQUE. It is also used during bitmap color conversions.
So I think by the describtion of the function it's not supposed to.
0
 

Author Comment

by:schhaya
ID: 1425035

0
 

Accepted Solution

by:
Lucas041697 earned 100 total points
ID: 1425036
The information in this article applies to:

 - Microsoft Windows Software Development Kit (SDK) for Windows
   versions 3.1 and 3.0

 - Microsoft Win32 Application Programming Interface (API) included with:

    - Microsoft Windows NT versions 3.5 and 3.51
    - Microsoft Windows 95 version 4.0



SUMMARY
The syntax for the SetBkColor function is documented in the Microsoft Windows Software Development Kit (SDK) as follows:

   DWORD SetBkColor(HDC hDC, COLORREF crColor);


SetBkColor sets the current background color of the specified device context (DC) to the color that the crColor parameter references, or to the nearest physical color if the device cannot represent the RGB color value that the crColor parameter specifies. In other words, SetBkColor cannot be used to set the background to a dithered color and defaults to the physical color that is closest to the requested crColor value.

MORE INFORMATION
This behavior can cause unexpected results for an application that changes the background color of a control to a color that cannot be represented by a color provided by the display device.
Specifically, when an application specifies a dithered color for the background of an edit control, and specifies the same color for the text background, Windows paints the control in two distinct colors.
For example, using the standard VGA display driver, the following call, in which COLOR_INACTIVEBORDER is a green/gray specified by RGB(64, 128, 128), sets the background color to gray (RGB(128, 128, 128)) rather than the dithered green/gray that is desired:

   SetBkColor(wParam, GetSysColor(COLOR_INACTIVEBORDER));



To illustrate, if the application uses the function call while

processing the WM_CTLCOLOR message to change the color of an edit control, the window background is painted green/gray, and the text background defaults to the nearest physical color, which is gray. This produces a gray rectangle inside a green/gray rectangle rather than the desired green/gray for the entire edit control.
This behavior can also occur with other controls such as option buttons and list boxes. However, an application can avoid this problem by using the SetBkMode function to set the background mode to TRANSPARENT. This allows the dithered brush pattern to show through beneath the text to achieve the desired results. That solution is not practical with a multiline edit control because if text is inserted, and the background mode has been set to TRANSPARENT, the text that is pushed to the right by the inserted text leaves its image behind. The result is text superimposed on top of other text, which quickly becomes unreadable.
To partially work around this situation for a multiline edit control, use the GetNearestColor function to determine the nearest physical color to the desired color, as in the code fragment below. In this case, the entire edit control is gray:

   case WM_CREATE:
      {
      HDC hDC;
      hDC = GetDC(hWnd);
      hGrayBrush = CreateSolidBrush(GetNearestColor(hDC,
            RGB(64, 128, 128)));
      ReleaseDC(hWnd, hDC);
      hWndEdit = CreateWindow( ... ES_MULTILINE ... );
      }
      break;

   case WM_CTLCOLOR:
      if (HIWORD(lParam) == CTLCOLOR_EDIT)
         {
         // The following call creates the nearest physical
         // color; therefore, it will be the same as the
         // hGrayBrush created above.
         SetBkColor(wParam, RGB(64, 128, 128));
         SetTextColor(wParam, RGB(255, 0, 0)); // red text
         return (DWORD)hGrayBrush;
         }
      else
        return DefWindowProc(hWnd, identifier, wParam, lParam);
      break;

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now