Importer:Error:WasEmpty



  • Hallo,

    ich versuch mich zum ersten mal mit nem Childwnd in nem neuen Thread,
    zur Probe hab ich mal ein kleines Testprogramm geschrieben.
    Leider gibt es da ein Problem,
    Messages vom Child zum Parent gehen wunderbar, aber andersrum kommt irgentwie nix an.
    Ich verwende PostThreadMessage(...
    Hier mal der Code:

    LRESULT CALLBACK ChildProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int wmId, wmEvent;
    	PAINTSTRUCT ps;
    	HDC hdc;
    	static HFONT font;
    	static HPEN pen;
    	static HWND Button1,Button2;
    
    	switch (message)
    	{
    	case WM_CREATE:
    		font=CreateFont(20,0,0,0,FW_NORMAL,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
    			DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"Arial");
    		Button1=CreateWindow("BUTTON","Info",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
    			20,240,160,30,hWnd,(HMENU)ID_BUTTON1,hInst,NULL);
    		Button2=CreateWindow("BUTTON","Destroy",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
    			20,20,160,30,hWnd,(HMENU)ID_BUTTON2,hInst,NULL);
    		SendMessage(Button1,WM_SETFONT,(WPARAM)font,TRUE);
    		SendMessage(Button2,WM_SETFONT,(WPARAM)font,TRUE);
    		pen=CreatePen(PS_SOLID,2,RGB(255,255,0));
    		return 0;
    	case WM_COMMAND:
    		wmId    = LOWORD(wParam);
    		wmEvent = HIWORD(wParam);
    		switch (wmId)
    		{
    		case ID_BUTTON1:{	HWND prnt=GetParent(hWnd);
    							SendMessage(prnt,WM_COMMAND,(WPARAM)LOWORD(INSTRUCTION1),0);	}break;
    		case ID_BUTTON2:{	HWND prnt=GetParent(hWnd);
    							SendMessage(prnt,WM_COMMAND,(WPARAM)LOWORD(ID_BUTTON2),0);	
    							DestroyWindow(hWnd);PAINTED=FALSE;								}break;
    		case INSTRUCTION2:	MessageBox(NULL,"geht....?","",MB_OK);							break;
    		case IDM_\1:
    			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
    			break;
    		case IDM_EXIT:
    			DestroyWindow(hWnd);
    			break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    		}
    		break;
    	case WM_PAINT:{	
    			int i,a=20,b=60,c=160;
    			HGDIOBJ original;
    			hdc = BeginPaint(hWnd, &ps);
    original=	SelectObject(hdc,pen);
    		MoveToEx(hdc,a,b,NULL);
    		for(i=0;i<9;i++){
    			LineTo(hdc,a+c,b);Sleep(100);
    			LineTo(hdc,a+c,b+c);Sleep(100);
    			LineTo(hdc,a,b+c);Sleep(100);
    			b=b+10;
    			LineTo(hdc,a,b);Sleep(100);
    			a=a+10;
    			c=c-20;		}
    		SelectObject(hdc,original);
    		EndPaint(hWnd, &ps);
    				  }break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    void NewThreadFunc(HWND Parent){
    	t_data threadD;
    
    	threadD=(t_data)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(T_data));
    
    				if(threadD==NULL){
    					MessageBox(NULL,"T_data allocation error","ERROR",MB_OK|MB_ICONERROR);}
    
    	threadD->hWnd=Parent;
    
    	toThread=	CreateThread(NULL,0,SimThreadProc,threadD,0,&ChildWndThreadID);
    
    				if(toThread==NULL){
    					MessageBox(NULL,"Thread Creation Error","ERROR",MB_OK|MB_ICONERROR);}
    
    	CloseHandle(toThread);
    }
    
    DWORD WINAPI SimThreadProc(LPVOID lpParam){
    
    	HWND hWnd,nWnd;
    	t_data OtD;
    	OtD=(t_data)lpParam;
    	hWnd=OtD->hWnd;
    	MSG msg;
    	BOOL B_return;
    
    	nWnd=CreateWindow(CWndClass,CWndClass,WS_CHILD|WS_VISIBLE|WS_DLGFRAME,20,60,205,280,hWnd,NULL,hInst,NULL);
    
    	if(!nWnd)ExitThread(2);
    
    	while (B_return=GetMessage(&msg, NULL, 0,0)!=0)
    	{
    		if (B_return==-1){	ExitThread(1);	}
    		else{
    				TranslateMessage(&msg);
    				DispatchMessage(&msg);	}}
    
    return msg.wParam;}
    

    Main-WndProc

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int wmId, wmEvent;
    	PAINTSTRUCT ps;
    	HDC hdc;
    	static HFONT font;
    	static HWND ClientArea,Button1,Button2,Editbox;
    
    	switch (message)
    	{
    	case WM_CREATE:
    		font=CreateFont(20,0,0,0,FW_NORMAL,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
    			DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"Arial");
    		Button1=CreateWindow("BUTTON","Start Paint",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
    			20,20,205,30,hWnd,(HMENU)ID_BUTTON1,hInst,NULL);
    		Button2=CreateWindow("BUTTON","Send Message",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
    			20,400,205,30,hWnd,(HMENU)ID_BUTTON2,hInst,NULL);
    		Editbox=CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|WS_DLGFRAME|ES_CENTER,
    			20,360,205,28,hWnd,NULL,hInst,NULL);
    		SendMessage(Button1,WM_SETFONT,(WPARAM)font,TRUE);
    		SendMessage(Editbox,WM_SETFONT,(WPARAM)font,TRUE);
    			return 0;
    	case WM_COMMAND:
    		wmId    = LOWORD(wParam);
    		wmEvent = HIWORD(wParam);
    		switch (wmId)
    		{
    		case ID_BUTTON1:	if(!PAINTED){	PAINTED=TRUE;
    											NewThreadFunc(hWnd);
    							SetWindowText(Editbox,"PAINTING....");}
    							break;
    		case ID_BUTTON2:{
    						BOOL RS=PostThreadMessage(ChildWndThreadID,WM_COMMAND,(WPARAM)LOWORD(INSTRUCTION2),0);
    						if(!RS){MessageBox(hWnd,"PostThreadMsg - failed!","",MB_OK);}
    							SetWindowText(Editbox,"EMPTY");
    							PAINTED=FALSE;	}break;
    		case INSTRUCTION1:	SetWindowText(Editbox,"Message Empfangen");break;
    		case IDM_\1:
    			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
    			break;
    		case IDM_EXIT:
    			DestroyWindow(hWnd);
    			break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    		}
    		break;
    	case WM_PAINT:
    		hdc = BeginPaint(hWnd, &ps);
    		EndPaint(hWnd, &ps);
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    

    Wenn ich im Hauptfenster auf Button2 klicke soll übers Childfenster ne Messagebox ausgelöst werden.
    Wo liegt mein Fehler? Oder isses vieleicht kompletter Käse was ich da gemacht hab?
    Wär schön wenn mir jemand helfen kann.
    Viele Dank schonmal für die Antworten
    Grüße


Log in to reply