You are not logged in.

  • Login

1

Friday, November 16th 2007, 4:52pm

WinApi MoveWindow() lässt edit control verschwinden

Source code

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
#include <windows.h>

LPCSTR MainClassName = "Texteditor";

// Zum Empfangen und Auswerten der messages
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    // Generelle Fensterstruktur registrieren
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(0));
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName = MAKEINTRESOURCE(1);
    wc.lpszClassName = MainClassName;
    wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(0), IMAGE_ICON, 16, 16, 0);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Konnte das Hauptfenster nicht registrieren!", "Fehler!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Fenster erstellen
    HWND hWnd = CreateWindowEx(
        WS_EX_OVERLAPPEDWINDOW,
        MainClassName,
        "Titletext",
        WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    // Textbox ins Fenster setzen
    HWND hEdit = CreateWindowEx(
        NULL,
        "EDIT",
        "Gib was ein",
        WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
        0, 0, 400, 300,
        hWnd,
        NULL,
        NULL,
        NULL
    );

    // Fenster anzeigen
    ShowWindow(hWnd, iCmdShow);


    // Auf Messages reagieren
    MSG wmsg;
    while( GetMessage(&wmsg, NULL, 0, 0) )
    {
        TranslateMessage(&wmsg);
        DispatchMessage(&wmsg);
    }

    return wmsg.wParam;
}

HWND hEdit;

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    char string[255];

    switch (iMsg)
    {
        case WM_SIZE:
            MessageBox(NULL, "noch siehst du das edit control", "noch zu sehen", MB_OK);
            hEdit = GetTopWindow(hWnd);
            MoveWindow(hEdit, 0, 0, wParam, lParam, true);
            MessageBox(NULL, "und weg ist es", "jetzt nicht mehr", MB_OK);
        break;
        case WM_CLOSE:
            DestroyWindow(hWnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case 11:
                    LoadString(GetModuleHandle(NULL), 21, string, sizeof(string));
                    MessageBox(hWnd, string, "Öffnen", MB_ICONINFORMATION);
                break;
                case 12:
                    LoadString(GetModuleHandle(NULL), 22, string, sizeof(string));
                    MessageBox(hWnd, string, "Speichern", MB_ICONINFORMATION);
                break;
                case 13:
                    DestroyWindow(hWnd);
                break;
            }
        break;
    }

    // An Windows weitergeben und dessen Antwort als Rückgabewert zurück
    return DefWindowProc(hWnd, iMsg, wParam, lParam);
}

Die letzten 2 Messageboxes sagen alles.
Der code in WM_SIZE soll für eine Breiten- und Höhenanpassung zum
top-level Fenster sorgen. Stattdessen verschwindet es einfach...
Inwiefern muss ich umdenken?

2

Friday, November 16th 2007, 9:20pm

Hier ein Link zu einem WinApi-Tutorial: http://winprog.org/tutorial/

Denke da werden einige Hinweise gegeben.
Meist gibt es mehrere Lösungsmöglichkeiten.

WM_CREATE ergänzen
dann Zeile 44 bis 55 "verschieben"
hab es nicht getestet aber versuch das mal.

MfG bcc-fan

This post has been edited 1 times, last edit by "bcc-fan" (Nov 16th 2007, 9:31pm)


3

Saturday, November 17th 2007, 4:47pm

Ich habe den Fehler bereits gefunden.
Die neue Höhe und Breite des Fensters wird als lParam (long) übergeben.
Mit LOWORD holt man die linken bytes zuner Word raus, die
die neue Breite angibt. Mit HIWORD() die rechten bytes,
die eine WORD für die neue Höhe eingibt.

Source code

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
#include <windows.h>

LPCSTR MainClassName = "Texteditor";

// Zum Empfangen und Auswerten der messages
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK EnumChildProc(HWND hwndChild, LPARAM lParam) ;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    // Generelle Fensterstruktur registrieren
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.lpszClassName = MainClassName;
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName = MAKEINTRESOURCE(1);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(0));
    wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(0), IMAGE_ICON, 16, 16, 0);
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Konnte das Hauptfenster nicht registrieren!", "Fehler!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Fenster erstellen
    HWND hWnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        MainClassName,
        "Titletext",
        WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    // Textbox ins Fenster setzen
    HWND hEdit = CreateWindowEx(
        NULL,
        "EDIT",
        "Gib was ein",
        WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
        0, 0, 400, 300,
        hWnd,
        NULL,
        NULL,
        NULL
    );

    // Fenster anzeigen
    ShowWindow(hWnd, iCmdShow);


    // Auf Messages reagieren
    MSG wmsg;
    while( GetMessage(&wmsg, NULL, 0, 0) )
    {
        TranslateMessage(&wmsg);
        DispatchMessage(&wmsg);
    }

    return wmsg.wParam;
}


HWND hEdit;

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    char string[255];

    switch (iMsg)
    {
		case WM_SIZE:
			hEdit = GetTopWindow(hWnd); 
			MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), true);
        break;
        case WM_CLOSE:
            DestroyWindow(hWnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case 11:
                    LoadString(GetModuleHandle(NULL), 21, string, sizeof(string));
                    MessageBox(hWnd, string, "Öffnen", MB_ICONINFORMATION);
                break;
                case 12:
                    LoadString(GetModuleHandle(NULL), 22, string, sizeof(string));
                    MessageBox(hWnd, string, "Speichern", MB_ICONINFORMATION);
                break;
                case 13:
                    DestroyWindow(hWnd);
                break;
            }
        break;
    }

    // An Windows weitergeben und dessen Antwort als Rückgabewert zurück
    return DefWindowProc(hWnd, iMsg, wParam, lParam);
}


Das handle zum edit control definiere ich global, damit es
zu keiner cross inizialisierung kommt.

Social bookmarks