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;
}