SetWindowText() method doesn't work if called from outside

Hello there

In CWindow.cpp, I instantiate a Controller. In the constructor of the class 'Controller', the method 'changeTextField' is called that should set the value of the textfield with the method 'SetWindowsText'. Unfortunately, the 'SetWindowsText' method doesn't work - it is ignored. The value of the textfield is "myTextField" instead of "changed". Can somebody of you tell me why?


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include <windows.h>
#include <CWindow.h>
#include <Controller.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    CWindow cWindow;
    HWND meinTextFeld=cWindow.initializeNow(hInstance,hPrevInstance,lpCmdLine,nCmdShow);


}



CWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "CWindow.h"
#include <windows.h>


#include <WINDOWSX.H>
#include <Controller.h>

const char g_szClassName[] = "myWindowClass";




// Step 4: the Window Procedure
LRESULT CALLBACK CWindow::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HBRUSH Dlg_OnCtlColor(HWND hwnd, HDC hdc, HWND hwndChild, int type);
    BOOL bRet = TRUE;
    switch(msg)
    {


        case WM_CTLCOLORSTATIC:
            HDC texthdc;
            HWND hwndtext;
			texthdc = (HDC) wParam;
			hwndtext = (HWND) lParam;

			SetBkMode ( texthdc, TRANSPARENT );
			SetTextColor( texthdc, RGB(255, 0, 0));
            HBRUSH hbr;
			hbr = (HBRUSH)GetStockObject( NULL_BRUSH );
			return (LRESULT)hbr;
			break;

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

CWindow::CWindow()
{


}
HWND CWindow::initializeNow(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow){
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
//        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "You",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 420,
        NULL, NULL, hInstance, NULL);



   meinTextFeld = CreateWindowExA(NULL,      //no extended style
                     "EDIT",
                      NULL,           //no title
                      WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,
                      10,
                      10,
                      500,
                      40,
                      hwnd,
                      (HMENU)502,
                      (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),//the module instance
                      NULL);






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

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);



    BOOL bRet;
    Controller c();


    while( (bRet = GetMessage( &Msg, NULL, 0, 0 )) != 0)
    {

        if (bRet == -1)
        {

            // handle the error and possibly exit
        }
        else
        {

            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
    }


    UnregisterClass( TEXT("My Window"),wc.hInstance );
    return meinTextFeld;
}
void CWindow::change(){
    SetWindowText(meinTextFeld, "changed");
}
CWindow::~CWindow()
{
    //dtor
}


Controller.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Controller.h"
#include <string>
#include <iostream>

#include <vector>
#include <CWindow.h>
using namespace std;

Controller::Controller(CWindow *cw)
{

    cw->change();



}
Controller::~Controller()
{
    //dtor
}


Thanks in advance
theRunner
I just found an answer for that:

http://support.microsoft.com/kb/125687/en-us
Topic archived. No new replies allowed.