Programmfehler



  • Hallo,

    ich habe einen Dialog erstellt und ein Listenfeld hinzugefügt, welches
    Dateien akzeptiert.(Ich habe den Haken im Resourceneditor gesetzt)

    Doch leider wird das Listenfeld nur grau dargestellet und aktzeptiert keine Dateien. 😕

    Hier der code:

    #include "resource.h"
    #include <windows.h>
    #include <tchar.h>
    #include "resource.h"
    
    WNDPROC wndpList;
    
    //Deklaration der WndProc Funktion und der Funktion fürs Fenster
    LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);
    BOOL CALLBACK MainDlg (HWND,UINT,WPARAM,LPARAM);
    //Fenster handles
    HINSTANCE hInstMain;
    HWND hwndMain;
    
    int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
    {
    	static TCHAR szAppName[]=TEXT("Dialog");
    	HWND hwnd;
    	MSG msg;
    	RECT rect;
    	WNDCLASS wndclass;
    
    	hInstMain=hInstance;
    
    	wndclass.style=0;
    	wndclass.lpfnWndProc=WndProc;
    	wndclass.cbClsExtra=0;
    	wndclass.cbWndExtra=0;
    	wndclass.hInstance=hInstance;
    	wndclass.hIcon=LoadIcon(hInstance,"pIcon");
    	wndclass.hCursor=NULL;
    	wndclass.hbrBackground=NULL;
    	wndclass.lpszMenuName=NULL;
    	wndclass.lpszClassName=szAppName;
    
    	if(!RegisterClass(&wndclass))
    	{
    		MessageBox(NULL,TEXT("Programm works with UNICODE!WinNT is required!"),szAppName,MB_ICONERROR);
    		return 0;
    	}
    
    	hwnd=CreateWindowEx(NULL,szAppName,TEXT("Test"),
    						WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_BORDER ,
    						320,250,
    						CW_USEDEFAULT,CW_USEDEFAULT,
    						NULL,NULL,hInstance,NULL);
    
    	hwndMain=CreateDialog(hInstance,szAppName,hwnd,MainDlg);
    	GetWindowRect(hwndMain,&rect);
    	AdjustWindowRect(&rect,WS_EX_TOOLWINDOW|WS_BORDER,TRUE|WS_EX_ACCEPTFILES );
    	SetWindowPos(hwnd,NULL,0,0,rect.right-rect.left,rect.bottom-rect.top,SWP_NOMOVE);
    	ShowWindow(hwndMain,SW_SHOW);
    	ShowWindow(hwnd,iCmdShow);
    	UpdateWindow(hwnd);
    
    	while(GetMessage(&msg,NULL,0,0))
    	{
    		if(hwndMain==0||!IsDialogMessage(hwndMain,&msg))
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	}
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
    	switch(message)
    	{case WM_CREATE:
    		return 0;
    	case WM_SETFOCUS:
    		SetFocus(hwndMain);
    		return 0;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	case WM_DROPFILES:
    	return(SendMessage(GetParent(hwnd), message, wParam, lParam));
            default:
                break;
    
    	}
    	return DefWindowProc(hwnd,message,wParam,lParam);
    }
    //Der Code fürs Hauptfenster
    BOOL CALLBACK MainDlg (HWND hMain,UINT message,WPARAM wParam,LPARAM lParam)
    {
    
    	switch(message)
    	{	
    
    	case WM_INITDIALOG:
    			wndpList = (WNDPROC)SetWindowLongPtr(GetDlgItem(hMain, IDC_LIST1), GWLP_WNDPROC, (LONG_PTR)WndProc);
         break;
    
    	case WM_DROPFILES:
                {
                  INT_PTR nCnt, nIndex, nSize;
                  LPTSTR  pszFileName;
                  HDROP   hDrop;
    
                    hDrop = (HDROP)wParam;
                    nCnt  = DragQueryFile(hDrop, (UINT)-1, NULL, 0); // Anzahl der Dateien ermitteln
    
                    for(nIndex = 0; nIndex < nCnt; ++nIndex)
                    {
                        // Länge des Dateinamens ermitteln
                        if(0 == (nSize = DragQueryFile(hDrop, nIndex, NULL, 0)))
                            continue;
    
                        pszFileName = new TCHAR[++nSize];
    
                        if(DragQueryFile(hDrop, nIndex, pszFileName, nSize))
                        {
                            // Den String nur dann hinzufügen, wenn er noch nicht vorhanden ist
                            if(0 > SendDlgItemMessage(hMain, IDC_LIST1, LB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)pszFileName))
                                SendDlgItemMessage(hMain, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)pszFileName);
                        }
    
                        delete [] pszFileName;
                    }
                }
               break;
    
    		   //Sollte klar sein
    	case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
    		case IDC_CANCEL:
    			//bei beenden des progs socket destroyen
    
    			DestroyWindow(GetParent(hMain));
    			return 0;
    		}
    
    		break;
    
    	}
    return 0;
    }
    

    Was ist falsch?



  • wndpList = (WNDPROC)SetWindowLongPtr(GetDlgItem(hMain, IDC_LIST1), GWLP_WNDPROC, (LONG_PTR)WndProc);

    muss weg



  • hier das ganze ohne dialogbox:

    #include <windows.h>
    
    #define ID_LISTBOX		1
    #define ID_BUTTON_CLOSE	2
    
    LRESULT CALLBACK WndProc	(HWND, UINT, WPARAM, LPARAM) ;
    LRESULT CALLBACK fnListBox	(HWND, UINT, WPARAM, LPARAM) ;
    
    TCHAR	szAppName[] = TEXT ("test") ;
    WNDPROC	fnWndProc ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    					PSTR szCmdLine, int iCmdShow){
    	HWND			hwnd ;
    	MSG				msg ;
    	WNDCLASSEX		wndclassex ;
    	wndclassex.cbSize			= sizeof (WNDCLASSEX) ;
    	wndclassex.style			= CS_HREDRAW | CS_VREDRAW ;
    	wndclassex.lpfnWndProc		= WndProc ;
    	wndclassex.cbClsExtra		= 0 ;
    	wndclassex.cbWndExtra		= 0 ;
    	wndclassex.hInstance		= hInstance ;
    	wndclassex.hIcon			= LoadIcon (NULL, IDI_APPLICATION) ;
    	wndclassex.hCursor			= LoadCursor (NULL, IDC_ARROW) ;
    	wndclassex.hbrBackground	= (HBRUSH) (COLOR_BTNFACE + 1) ;
    	wndclassex.lpszMenuName		= NULL ;
    	wndclassex.lpszClassName	= szAppName ;
    	wndclassex.hIconSm			= NULL ;
    	RegisterClassEx (&wndclassex) ;
    	hwnd = CreateWindowEx (NULL, szAppName, TEXT ("test"),
    							WS_OVERLAPPEDWINDOW,
    							CW_USEDEFAULT , CW_USEDEFAULT,
    							300, 300,
    							NULL, NULL, hInstance, NULL) ;
    	ShowWindow (hwnd, iCmdShow) ;
    	UpdateWindow (hwnd) ;
    	while (GetMessage (&msg, NULL, 0, 0)){
    		TranslateMessage (&msg) ;
    		DispatchMessage (&msg) ;
    	}
    	return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    	static	HINSTANCE	hInstance ;
    	switch (message){
    		case WM_CREATE :
    			hInstance = ((LPCREATESTRUCT) lParam) -> hInstance ;
    			CreateWindowEx (WS_EX_CLIENTEDGE | WS_EX_ACCEPTFILES, "listbox",
    							TEXT ("Ok"), 
    							WS_CHILD | WS_VISIBLE | LBS_DISABLENOSCROLL | LBS_STANDARD,
    							10, 10,
    							270, 220,
    							hwnd, (HMENU) ID_LISTBOX, hInstance, NULL) ;
    			CreateWindowEx (NULL, "button",
    							TEXT ("Ende"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
    							210, 240,
    							75, 25,
    							hwnd, (HMENU) ID_BUTTON_CLOSE, hInstance, NULL) ;
    			fnWndProc = (WNDPROC) SetWindowLongPtr (GetDlgItem (hwnd, ID_LISTBOX), GWLP_WNDPROC, (LONG_PTR) fnListBox) ;
    			return 0 ;
    		case WM_COMMAND :
    			switch (LOWORD (wParam)){
    				case ID_BUTTON_CLOSE :
    					SendMessage (hwnd, WM_DESTROY, 0, 0) ;
    					break ;
    			}
    			return 0 ;
     		case WM_DESTROY :
    			PostQuitMessage (0) ;
    			return 0 ;
    	}
    	return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    
    LRESULT CALLBACK fnListBox (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    	INT_PTR	nCnt,
    			nIndex,
    			nSize ;		
    	char	*pszFileName = NULL;
    	HDROP	hDrop ;
    	switch(message){
    		case WM_DROPFILES :
    			hDrop = (HDROP) wParam ;
    			nCnt  = DragQueryFile (hDrop, (UINT)-1, NULL, 0) ;
    			for (nIndex = 0; nIndex < nCnt; ++nIndex){
    				if (0 == (nSize = DragQueryFile (hDrop, nIndex, NULL, 0))){
    					break ;
    				}
    				pszFileName = (char*) malloc ((sizeof (char) * nSize) + 1) ;
    				if (DragQueryFile (hDrop, nIndex, pszFileName, nSize)){
    					SendMessage (GetDlgItem (GetParent (hwnd), ID_LISTBOX), LB_ADDSTRING, 0, (long) pszFileName) ;
    				}
    				free (pszFileName) ;
    			}
    			break ;
    	}
    	return (CallWindowProc (fnWndProc, hwnd, message, wParam, lParam)) ;
    }
    


  • Danke, das Löschen der Zeile hat geholfen !


Anmelden zum Antworten