Change button text from a thread?

This is a discussion on Change button text from a thread? within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I've got an MFC dialog, with a button. The buttons control variable is called m_macroStatus. I can call m_macroStatus.SetWindowTextW ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Change button text from a thread?

    Hello,

    I've got an MFC dialog, with a button. The buttons control variable is called m_macroStatus.

    I can call m_macroStatus.SetWindowTextW ( _T("text") ) from within a function created by visual studio 2k5. However, when I try do to that from an AfxThread, it says it has no clue what m_macroStatus is, and that SetWindowTextW does not exist.

    How can I change the text of a button from within an AfxThread (called by AfxBeginThread()).

    Any help will be appreciated.

    Thanks in advance,

    Matt N
    ~guitarist809~

  2. #2
    EOP
    EOP is offline
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    You should not do this!

    Send an user message to the dialog instead.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    There is no CWnd::SetWindowTextW() in MFC.

    You can use PostMessage(hWnd, WM_SETTEXT,...) from the thread.

    gg

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Another way:
    Pass the windows handle to the thread (m_object.m_hWnd) and then create a CWnd object from CWnd::FromHandle. From that object, you may call SetWindowText (it's TCHAR).
    Only note that this will create a CWnd object, not CYourDialog object (so casting it to CYourDialog is WRONG!).
    This is MFC's recommended way. However, do note that MFC uses SendMessage which can be dangerous in a multi-threaded environment.
    The best way is to use PostMessage or make your own system to save instructions for the main thread message loop to perform some work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Thanks for posting, everyone.

    I am thoroughly lost. I got to
    Code:
    PostMessageA(m_triggerKey.m_hWnd, WM_SETTEXT);
    But, now, how do I actually change the text? (I had to use PostMessageA because PostMessage was in Unicode and didn't take an hWnd).

    Thanks,

    Matt N


    ***EDIT***
    After a lot of researching, I came up with this.
    Code:
    SendMessageA(m_triggerKey.m_hWnd, WM_SETTEXT, 0, (LPARAM)"Text!");
    It works, but if this is dangerous, please let me know
    Last edited by guitarist809; 05-03-2008 at 03:13 PM.
    ~guitarist809~

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    Got to learn to use the manual, specially for windows programming.

    http://msdn.microsoft.com/en-us/libr...44(vs.85).aspx

    http://msdn.microsoft.com/en-us/library/ms632644.aspx

    The WM_SETTEXT reference talks about using SendMessage(), but you can use it with PostMessage() as well.

    gg

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    There are some big gotchas in MFC about calling functions that alter controls or anything they display from within a thread. In order to do this properly you will have to derive from the thread class in MFC. Just creating a thread using AFXBeginThread() will not work. AFXBeginThread() is only for worker threads that do not touch the GUI. User interface threads must derive from CThread or your application will freeze when the thread tries to update the GUI. The documentation on MFC explains this very well under the index topic multi-threading in MFC.

    I do not know if this applies to the newer MFC that ships with MSVS 2008.
    Last edited by VirtualAce; 05-03-2008 at 10:12 PM.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    >> It works, but if this is dangerous, please let me know
    Change "SendMesageA" to "PostMessageA" in your worker thread.

    gg

  9. #9
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Codeplug View Post
    >> It works, but if this is dangerous, please let me know
    Change "SendMesageA" to "PostMessageA" in your worker thread.

    gg
    I've tried this, but when it's called, nothing happens.
    ~guitarist809~

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    Does it return FALSE? What is GetLastError() if it does?

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 10:34 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. MFC: Change the color of static text with button press?
    By BrianK in forum Windows Programming
    Replies: 2
    Last Post: 06-16-2004, 11:03 PM
  4. text on owner-drawn button?
    By SyntaxBubble in forum Windows Programming
    Replies: 5
    Last Post: 02-17-2002, 12:08 PM
  5. How To Change Caption's On A Button When It Is Click ?
    By SonicWave in forum Windows Programming
    Replies: 1
    Last Post: 09-19-2001, 02:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21