A
danke nochmal an alle, hat perfekt funktioniert!
für alle die evtl. dasselbe Problem haben hier nochmal der komplette getestete Code:
#include<iostream>
#include<Windows.h>
#include<WindowsX.h>
#include<cstdio>
using namespace std;
HWND hauptfenster_handle;
HWND button1_handle;
HINSTANCE hi;
LRESULT CALLBACK HauptWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch(message)
{
case WM_COMMAND: //Wenn die Message ein Befehl ist löse das aus:
if (HIWORD(wparam) == BN_CLICKED)
//Wenn es sich um einen Button_click handelt
{
if (LOWORD(wparam) == 1) //das sind die oben ernannten Parameter, 1 & 2 hatten wir ja
{
cout<<"Button1 geklickt!\n";
//Meldung von Button-Klick 1 anzeigen
}
}
break;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
DWORD WINAPI hauptfenster(LPVOID IpParameter)
{
WNDCLASS wc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH) GetStockObject(LTGRAY_BRUSH);
wc.hCursor=LoadCursor(hi, IDC_ARROW);
wc.hIcon=LoadIcon(hi, IDI_WINLOGO);
wc.hInstance=hi;
wc.lpfnWndProc=HauptWndProc;
wc.lpszClassName=TEXT("Fensterklasse");
wc.lpszMenuName=0;
wc.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wc);
hauptfenster_handle=CreateWindow(TEXT("Fensterklasse"), TEXT("Hauptfenster"), WS_VISIBLE|WS_POPUP, 100, 100, 140, 20, 0, 0, hi, 0);
UpdateWindow(hauptfenster_handle);
ShowWindow(hauptfenster_handle, SW_NORMAL);
MSG msg;
while(GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
DWORD WINAPI button1(LPVOID IpParameter)
{
button1_handle=CreateWindow(TEXT("BUTTON"),TEXT("Button 1"),WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 0,0 ,140,20,hauptfenster_handle,(HMENU) 1, hi, NULL);
UpdateWindow(button1_handle);
ShowWindow(button1_handle, SW_NORMAL);
MSG msg;
while(GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
int main()
{
DWORD hauptfensterthread_id;
HANDLE hauptfensterthread_handle=CreateThread(0, 0, hauptfenster, 0, 0, &hauptfensterthread_id);
Sleep(1000);
DWORD button1thread_id;
HANDLE button1thread_handle=CreateThread(0, 0, button1, 0, 0, &button1thread_id);
Sleep(1000);
SetParent(button1_handle, hauptfenster_handle);
RECT r;
GetWindowRect(button1_handle, &r);
SetWindowPos(hauptfenster_handle, HWND_NOTOPMOST, r.left, r.top, r.right-r.left, r.bottom-r.top, 0);
SetWindowPos(button1_handle, HWND_TOPMOST, r.left, r.top, r.right-r.left, r.bottom-r.top, 0);
WaitForSingleObject(hauptfensterthread_handle, INFINITE);
WaitForSingleObject(button1thread_handle, INFINITE);
}