简体   繁体   中英

Can't Send custom message from one control to another

I have a situation here and i don't know what is wrong. I have a parent and 2 sunclassed children.Both are edit types. Now i am using the first control(first child) to get input from the user and it all works fine, then i send a message to the parent through a custom message and than i want to forward the message to the second control yet when i use

SendMessage();

nothing happends....

my custom message is defined like this :

  #define WM_USERMESSAGE    0x0401

Another question i have is how do i send strings? 'cause when i send the message to the father through lParam it says its value is 72 but i dont see any refrence to my text that i have sent.

these are the code segments: child 1 sending message to father after getting input:

 case WM_KEYUP:
                {
                    switch (wParam) {
                        case VK_RETURN:
                        {
                            length = GetWindowTextLength(hwnd);
                            GetWindowText(hwnd, buf,length);
                            SetWindowText(hwnd,NULL);
                            tempa = SendMessage(GetParent(hwnd), WM_USERMESSAGE,sizeof(buf),*buf);
                            return 0;
                        }
                    }
                return 0;
                }

Father gets message and trys to forward it:

  case WM_USERMESSAGE:
            {
                int tempb = SendMessage(nhwnd, WM_USERMESSAGE, wParam, (LPARAM)"Hi");
            }

and the child number 2 is listening for the message:

   case WM_USERMESSAGE:
            {
            SetWindowText(window, "hi");//(TCHAR*)lParam);
            return 0;
            }

(now as you see i used static strings to check if the functions work but i want to change them so i could recive information from the messages) Thanks for the help in advance!

Your initial SendMessage() from the first edit to the parent window is not sending the string data correctly. You are dereferencing your buffer pointer, so you are only sending the first character (72 is the ASCII 'H' character). You need to get rid of that dereference and pass the buffer pointer as-is. And if you are going to send the buffer length (which you don't use), you need to send the length that GetWindowText() returns, not the full size of your buffer, so the receiver knows exactly how many characters are actually in the buffer.

Try this:

case WM_KEYUP:
{
    switch (wParam)
    {
        case VK_RETURN:
        {
            ZeroMemory(buf, sizeof(buf));
            length = GetWindowTextLength(hwnd);
            if (length > 0)
                length = GetWindowText(hwnd, buf, min(length+1, sizeof(buf)));
            SetWindowText(hwnd, NULL);
            tempa = SendMessage(GetParent(hwnd), WM_USERMESSAGE, length, (LPARAM)buf);
            return 0;
        }
    }
    return 0;
}

.

case WM_USERMESSAGE:
{
    int tempb = SendMessage(nhwnd, WM_USERMESSAGE, wParam, lParam);
    return 0;
}

.

case WM_USERMESSAGE:
{
    SetWindowText(window, (LPTSTR)lParam);
    return 0;
}

Assuming all of your HWNDs are valid, then the forwarding should work fine. If the message is not making it all of the way, then one of your HWNDs is not valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM