Im trying to write to the list box "Win_1" with SendMessage() but im doing something wrong.

Thanks in advance!

#include <windows.h>
#include <tchar.h>
#include <string>
#include <iostream>
#include <richedit.h>
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"Gdi32.lib")
using namespace std;

#define IDB_TEXT2 101
#define IDB_TEXT3 102
#define GetF 103

HWND hwnd, Win_1, Win_2, Send;
MSG Msg;
HDC hdc;
PAINTSTRUCT ps;
TCHAR *buf = {0};
int length;
size_t found;
string Text, str;

void GetIt()
{
     length = GetWindowTextLength(Win_2);
     buf    = new TCHAR[length+1];
     GetWindowText(GetDlgItem(hwnd,IDB_TEXT3), buf, length+1);
     str   = buf;
     found = str.find("\r\n");
     Text  = str.substr(0,found);

     SendMessage(Win_1, LB_INSERTSTRING, (WPARAM)-1, (LPARAM)Text.c_str());

     delete[] buf;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
  switch(msg){
              case WM_CREATE:
                   Win_1 = CreateWindowEx(0,"ListBox", 0,
                         WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
                         WS_HSCROLL | LBS_DISABLENOSCROLL,
                         20, 20, 550, 460, hwnd, (HMENU)IDB_TEXT2, 0, NULL);

                   Win_2 = CreateWindowEx(0,"Edit","hello",
                         WS_VISIBLE | WS_CHILD |  WS_HSCROLL | WS_VSCROLL |
                         ES_AUTOHSCROLL | ES_WANTRETURN | ES_MULTILINE,
                         20, 500, 550, 180,hwnd,(HMENU) IDB_TEXT3,
                         ((LPCREATESTRUCT)lParam)->hInstance,NULL);
                   Send  = CreateWindowEx(0,"Button","Send",
                         WS_CHILD | WS_VISIBLE | WS_BORDER,
                         20, 700, 550, 20,hwnd,(HMENU)GetF,0,NULL);

                   EnableWindow(Send ,TRUE);
                   break;
              case WM_PAINT:
                   GetIt();
                   break;
              case WM_COMMAND:
                   switch(wParam)
                   {
                    case GetF:
                         InvalidateRect(Win_1,0,FALSE);
                         //system("start http://www.google.com");
                         break;
                   }
              case WM_TIMER:
                  // if (GetAsyncKeyState(VK_F11)) ShowWindow(hwnd,SW_HIDE);
                   //if (GetAsyncKeyState(VK_F12)) ShowWindow(hwnd,SW_SHOW);
                   break;

              case WM_CLOSE:
                   DestroyWindow(hwnd);
                   break;

              case WM_DESTROY:
                   PostQuitMessage(0);
                   break;
              default:
                   return DefWindowProc(hwnd,msg,wParam,lParam);
       }
       return 0;
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
    WNDCLASSEX wc;
    wc.hInstance = hInstance;
    wc.cbClsExtra = 0;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(100,100,100));
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hIcon   = LoadIcon (NULL, IDI_APPLICATION) ;
    wc.lpfnWndProc =  WndProc;
    wc.lpszClassName = "Class";
    wc.lpszMenuName = NULL;
    wc.style = 0;
    wc.hIconSm = NULL;
    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(WS_EX_LEFT|WS_EX_LTRREADING|WS_EX_WINDOWEDGE,
                           "Class",
                           " The_Chatter",
                           WS_OVERLAPPEDWINDOW,
                           CW_USEDEFAULT, CW_USEDEFAULT, 600, 800,
                           HWND_DESKTOP, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0;
    }

    SetTimer(hwnd,1,125,NULL);
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
          TranslateMessage(&Msg);
          DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

Recommended Answers

All 3 Replies

Looks like you are attempting to sendmessage() to your listbox upon reciept of WM_PAINT messages.... and the only time I think your program will get a WM_PAINT is when you invalidate_rect() inside of your WM_COMMAND... So my question is: what is going to trigger the appropriate WM_COMMAND?

Thanks for your response!

What do you mean by its not created?
The list box is displayed in the window and "hello" is written to it.

>>So my question is: what is going to trigger the appropriate WM_COMMAND?

When i push the "Send" button it should call this:

Send  = CreateWindowEx(0,"Button","Send",
                         WS_CHILD | WS_VISIBLE | WS_BORDER,
                         20, 700, 550, 20,hwnd,(HMENU)GetText,0,NULL);

which should call

InvalidateRect(Win_1,0,FALSE);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.