Hi i am trying to make a notepad like program.

The problem is that when i try to display some text nothing happens.
If someone could help me i would really appreciate it.

The function call:

...
SetWindowText(edit, "TEST");
...

Creation of edit:

...
HMENU Menu, SubMenu;

Menu = CreateMenu();
SubMenu = CreatePopupMenu();

AppendMenu(SubMenu, MF_STRING, 9001, "&Open");
AppendMenu(SubMenu, MF_STRING, 9002, "&Save");
AppendMenu(SubMenu, MF_STRING, 9003, "Save &As...");
AppendMenu(Menu, MF_STRING | MF_POPUP,(UINT)SubMenu,"&File");

SetMenu(hWnd, Menu);

edit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_MULTILINE, 0, 0, 100, 100, hWnd, (HMENU)112, GetModuleHandle(0), 0);
if(!edit)
{
    int DEBUG = GetLastError();
    MessageBoxA(0, "Failed to create edit", "ERROR", MB_OK | MB_ICONERROR);
    ExitProcess(1);
}
SetFocus(edit);
...

The edit is being created in the message loop when a WM_CREATE message. is send

Thanks in advance

Here's an old program I dug up that puts text in a edit control when you click the button. Boy, its horrible code, but runs.

#include <windows.h>
#include <stdio.h>
#include <string.h>
HWND hMainWnd;
HINSTANCE ghIns;
UINT iStyle=WS_CHILD|WS_VISIBLE;
HWND hTxtBox;
char szText[50];

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 static HWND hButton;
  
 switch (message)
 {
  case WM_CREATE:
    strcpy(szText,"Fred");   
    hButton=CreateWindow("button","button #1",iStyle,67,15,100,25,hwnd,(HMENU)(1),((LPCREATESTRUCT)lParam)->hInstance,0);
    hTxtBox=CreateWindow("edit",0,iStyle,15,60,210,25,hwnd,(HMENU)(2),((LPCREATESTRUCT)lParam)->hInstance,0);
    return 0;
  case WM_COMMAND:
    switch (LOWORD(wParam))
    {
     case 1:
       SetWindowText(hTxtBox,szText);
       break;
     default:
       break;
    }  
    return 0;
  case WM_CLOSE:
    PostQuitMessage(0);
    return 0;
 }

 return DefWindowProc(hwnd, message, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int iCmdShow)
{
 MSG messages;
 WNDCLASSEX wincl;
 
 ghIns=hInstance;
 wincl.hInstance = hInstance;
 wincl.lpszClassName = "Form1";
 wincl.lpfnWndProc = WindowProcedure;
 wincl.style = CS_HREDRAW | CS_VREDRAW;
 wincl.cbSize = sizeof(WNDCLASSEX);
 wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
 wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
 wincl.lpszMenuName = NULL;
 wincl.cbClsExtra = 0;
 wincl.cbWndExtra = 0;
 wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
 RegisterClassEx(&wincl);       //Register Main Window Class
 hMainWnd=CreateWindow("Form1","Caption = Form1",1040384,0x8000,0x8000,250,200,HWND_DESKTOP,0,hInstance,0);
 ShowWindow(hMainWnd, iCmdShow);
 while(GetMessage(&messages, NULL, 0, 0))
 {
  TranslateMessage(&messages);
  DispatchMessage(&messages);
 }

 return messages.wParam;
}

I prettied it up a bit.

#include <windows.h>
#define IDC_TEXTBOX   1500
#define IDC_BUTTON    1505


LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 HWND hCtrl;
  
 switch (message)
 {
  case WM_CREATE:
    hCtrl=CreateWindowEx(0,"button","button #1",WS_CHILD | WS_VISIBLE,67,15,100,25,hwnd,(HMENU)IDC_BUTTON,((LPCREATESTRUCT)lParam)->hInstance,0);
    hCtrl=CreateWindowEx(0,"edit",0,WS_CHILD | WS_VISIBLE,15,60,210,25,hwnd,(HMENU)IDC_TEXTBOX,((LPCREATESTRUCT)lParam)->hInstance,0);
    return 0;
  case WM_COMMAND:
    switch (LOWORD(wParam))
    {
     case IDC_BUTTON:
       SetWindowText(GetDlgItem(hwnd,IDC_TEXTBOX),"Text In A Text Box");
       break;
     default:
       break;
    }  
    return 0;
  case WM_CLOSE:
    PostQuitMessage(0);
    return 0;
 }

 return DefWindowProc(hwnd, message, wParam, lParam);
}


int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int iCmdShow)
{
 WNDCLASSEX wincl;
 MSG messages;
 HWND hMain;

 wincl.hInstance = hInstance;
 wincl.lpszClassName = "Form3";
 wincl.lpfnWndProc = WindowProcedure;
 wincl.style = CS_HREDRAW | CS_VREDRAW;
 wincl.cbSize = sizeof(WNDCLASSEX);
 wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
 wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
 wincl.lpszMenuName = NULL;
 wincl.cbClsExtra = 0;
 wincl.cbWndExtra = 0;
 wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
 RegisterClassEx(&wincl);       //Register Main Window Class
 hMain=CreateWindow("Form3","Caption = Form3",WS_OVERLAPPEDWINDOW,0x8000,0x8000,250,200,HWND_DESKTOP,0,hInstance,0);
 ShowWindow(hMain, iCmdShow);
 while(GetMessage(&messages, NULL, 0, 0))
 {
  TranslateMessage(&messages);
  DispatchMessage(&messages);
 }

 return messages.wParam;
}

That is SetWindowText() if that's all you want to do. You could also write your own keypress code in a RegisterClassEx window. But then you would have to duplicate all the functionality of the predefined edit control.

Thank you everyone.

It worked when i used. SetWindowText(GetDlgItem(hWnd, 112), buffer); It ignores newlines but i think i can handle that.


Hope you had a wonderful christmas

This question has already been answered. Start a new discussion instead.