Results 1 to 7 of 7

Thread: Setting textbox colour

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Setting textbox colour

    Hi,

    I have two VB6 applications, and I am working on changing the properties of one using API calls in the other (just experimenting, really). Anyway, I was wandering if there's a way to change the text colour in a textbox using API?

    I had a look with the usual Spy++ when changing the text colour within the program, but that just posts the message 'WM_PAINT'. I also had a look at the SetProp function in MSDN, but I'm not sure where to go with that.

    Any ideas?

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Setting textbox colour

    Well, you'll have to use the SendMessageA API function and, the WM_SETFONT as the message

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Setting textbox colour

    Thanks for the prompt reply. How do I use this?

    Using:
    VB Code:
    1. Debug.Print SendMessage(Text1.hwnd, WM_GETFONT, 0&, 0&)
    2. Debug.Print SendMessage(Text2.hwnd, WM_GETFONT, 0&, 0&)
    gives 889851594 and 889851594 where Text1 has black text and Text2 has red text.

    Could you give me a simple example of just changing the forecolour to red?

    EDIT:
    Just reading around a bit, and it seems like there is no colour associated with font. If I were to use SetTextColor, how do I get the device context of the TextBox?

    EDIT 2:
    Reading around further, came up with the following:
    VB Code:
    1. Dim lngMainTexthDC As Long
    2.    
    3.     lngMainTexthDC = GetDC(txtMainText.hwnd)
    4.     SetTextColor lngMainTexthDC, RGB(255, 0, 0)
    However, this does not work. Still not sure why...
    Last edited by olamm2k; May 16th, 2005 at 04:21 PM.

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Setting textbox colour

    Quote Originally Posted by olamm2k
    EDIT:
    Just reading around a bit, and it seems like there is no colour associated with font. If I were to use SetTextColor, how do I get the device context of the TextBox?

    I do not think the Textbox has one, if it does then it will be Text1.hdc.

    If it is not set then you'd have to creaqte your own.


    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Setting textbox colour

    How do I do this? There's the CreateDC function, but how would I use that to create a device context for an object outside my application?
    VB Code:
    1. Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA"
    2.   (ByVal lpDriverName As String, _
    3.   ByVal lpDeviceName As String,
    4.   ByVal lpOutput As String,
    5.   lpInitData As DEVMODE) As Long

  6. #6
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Setting textbox colour

    Outside your application? I have no idea at all how you could do it.

    I'll see if I can find some code / a tutorial that will help or, hopefuly someone that can answer this will reply before then

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Setting textbox colour

    Okay, thanks.

    Just to amend my second post,
    VB Code:
    1. Dim lngMainTexthDC As Long
    2.    
    3.     lngMainTexthDC = GetDC(txtMainText.hwnd)
    4.     SetTextColor lngMainTexthDC, RGB(255, 0, 0)
    does work, but only when you then use TextOut to write onto the TextBox, which isn't very helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width