Sending keyboard text to a static text box

Posted on 2007-10-12
Last Modified: 2012-06-21
I am trying to create a simple C++ App under WTL that contains a little routine  to pass a few words of text typed from the keyboard so that it appears in a static text box (I dont want to use an Edit box for this if it is possible to avoid it).  I have tried to intercept keyboard commands but because it is a static text box no windows messages eg. WM_CHAR  are generated.  I can see other windows messages with Spy++(WM_GETTEXT etc) sent to the correct window when I press a key but my message handler will not intercept these, (I assume the Windows System has got there first).  I can set the focus and create a flashing cursor etc in the static text box and write a programm created string to it under program control, without any problems.  Any ideas how to do this trivial task please?
Question by:racurrell

    Accepted Solution

    Where is a 'message pick cycle' inside your program. Normally WTL will hide it for you, but you can easily find it. It looks like:

    MSG oMsg
    while(::GetMessage(&oMsg, ...))

    You can catch WM_KEYDOWN just after it's received by GetMessage() function, like this:

    while(::GetMessage(&oMsg, ...))
      if(WM_KEYDOWN == oMsg.message && MyWindowHWND() == oMsg.hwn)
        // do work here

    where MyWindowHWND() is some way to get a window handle of dialog/window in question (one with a static)

    Your messages are eated by ::IsDialogMessage() and/or ::DefWindowProc() functions since active window (static) don't handle WM_KEYDOWN

    Another way (more clean) is to SUBCLASS your static and catch WM_KEYDOWN / WM_CHAR inside subclassed DlgProc() message handler function you will create. But this requires some accurate programming.

    Author Comment

    Hi GregorTheEye, I will try your suggestions, the window is already subclasssed so that the text received on the serial port is displayed in a different colour (that all works OK) so I can try doing the Message catches you suggest there, I will let you know how it goes.  I thought it was probably ::IsDialogMessage() and/or ::DefWindowProc() functions that were getting there first and eating the messages but my experience is a bit limited with that aspect of programming.
    Thanks again.

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    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.

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now