You are not logged in.

  • Login

1

Friday, December 24th 2010, 2:30pm

Win32-Fenster mit Button

Hallo!
Ich versuche jetzt schon seid 2 Stunden per google herauszufinden, wie man einen Button codet.
Man findet echt nur noch wie man es mit der Toolbox macht.... -.-
Ich will das aber richtig programmieren!
Ich glaube ich weiß nicht so, wo das hin muss....
Weil eigentlich habe ich das erstellen, denke ich jeden falls, drauf....
Das Ergebnis bei meinem Quelltext ist, dass ein Fenster erstellt wird. - Ohne Button...
Hier mal mein versuchter Quelltext: (Sorry, ist noch eine Messagebox usw. dabei....)

C/C++ Quellcode

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
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
 
static TCHAR szWindowClass[] = _T("win32app");
static TCHAR szTitle[] = _T("Lernen043");
#define ID_BEISPIELBUTTON 4001
static TCHAR szBla[] = _T("BUTTON");
static TCHAR szBla2[] = _T("BEISPIELTEXT");
 
HINSTANCE hInst;
 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;
 
    wcex.cbSize			= sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
 
    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);
 
        return 1;
    }
 
    hInst = hInstance;
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );
	HWND hButton;
	hButton = CreateWindow (szBla,
							szBla2,
							BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
							90, 150,
							95, 40,
							hWnd,
							(HMENU)ID_BEISPIELBUTTON,
							hInst,
							NULL
	);
 
    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);
 
        return 1;
    }
 
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);
 
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
 
    return (int) msg.wParam;
}
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 
    switch (message)
    {
	case WM_KEYDOWN:
	{
		switch (wParam)
		{
		case VK_ESCAPE:
		{
		int msgboxID = MessageBox(
        NULL,
        (LPCWSTR)L"Möchten sie das Programm wirklich beenden?",
        (LPCWSTR)L"Programm Schließen",
        MB_ICONWARNING | MB_YESNO);
		switch (msgboxID)
		{
		case IDYES:
			PostQuitMessage (0);
			return(0);
		break;
		case IDNO:
			return(0);
		break;
		}
		}break;
    }break;
    case WM_DESTROY:
	{
        PostQuitMessage(0);
    }break;
    default:
	{
        return DefWindowProc(hWnd, message, wParam, lParam);
    }break;
    }
	}
    return 0;
}

2

Friday, December 24th 2010, 9:51pm

Keine 3min Suche brachte:
http://zetcode.com/tutorials/winapi/

Stichwort: winapi tutorial

Da gibt es auch noch Andere ;)

MfG bcc-fan

3

Friday, December 24th 2010, 10:37pm

Das ist C, nicht C++.....

4

Friday, December 24th 2010, 11:32pm

C oder C++ bei Windows? Da geht Microsoft eh seinen eigenen Weg.

Ja, dein Quelltext ist C für Windows, so wie Microsoft das sieht. Hab das Tutorial nur überflogen, aber das sieht auch nach C für Windows aus.
Das Tutorial ist aber auch für andere Compiler geeignet.

Sollte ich etwas übersehen haben, exakt die Position beschreiben - da wird schon eine Möglichkeit sein.


Der Begriff 'szWindowClass[]' aus der WinAPI ist nicht mit dem 'class' aus C++ eng verwandt :!:
Als Windows erschaffen wurde hatte Microsoft mit C++, so wie wir das heute verstehen, nicht viel am Hut :rolleyes:

Ausserdem ist der Suchbegriff 'WinAPI tutorial' nicht nur auf C oder C++ beschränkt. Da gibt es auch noch andere Sprachen.

MfG bcc-fan

This post has been edited 1 times, last edit by "bcc-fan" (Dec 24th 2010, 11:44pm)


5

Saturday, December 25th 2010, 3:44pm

Hat sich aber erledigt. Hab die Größe des Fensters im Verhältnis der Position des Buttons nicht beachtet.
Mal eine Frage:
Wenn ich eine ganz normal Textbox hinzufüge, schließt sich mein Programm sofort. Das lässt sich zwar beheben wenn man die Zeile 144 löscht, wäre aber schon etwas unschön. Muss ich die Textbox erst zwingend verarbeiten?

Similar threads

Social bookmarks