CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615

    Unhappy Combobox does not add string

    the following window procedure does not add the MIDI Devices on the combo box I want, i'm using CB_ADDSTRING...

    Code:
    LRESULT CALLBACK wndproc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
        int         nDevices;
        MIDIOUTCAPS moutcaps[255]; 
        HWND        hwndDevices, hwndListCaps;
        
        switch (msg)
        {
        case WM_CREATE:
            
            int i;
            
            // enumerar dispositivos MIDI
            nDevices = midiOutGetNumDevs();
            
            if (nDevices == 0)
            {
                MessageBox (0, "No MIDI devices available.", "Error", MB_ICONSTOP);
                exit(EXIT_FAILURE);
            }
            
            hwndDevices = GetDlgItem(hwnd, IDC_DEVICE);
            hwndListCaps= GetDlgItem(hwnd, IDC_LISTCAPS);
            
            for (i = 0; i < nDevices; i++)
            {
                midiOutGetDevCaps(i, &moutcaps[i], sizeof(MIDIOUTCAPS));
                SendMessage(hwndDevices, CB_ADDSTRING, 0, (LPARAM)moutcaps[i].szPname); 
            }
            return 0;
            
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
            
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
            
        }
        
        return DefWindowProc(hwnd, msg, wparam, lparam);
        
    }
    where is the problem?????

  2. #2
    Join Date
    Jun 2003
    Posts
    58
    what is the combo box style ? Dropdown list or simple.
    cause, I a problem like that in past with dropdown Combo box.
    may be you not set the height of the combo box more height when he drop down the list.

  3. #3
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615

    Angry

    No, it's done well in the resouce editor...
    Now I've the same problem
    in another application, here's that (sorry for the long code):

    Code:
    #include <windows.h>
    #include <windowsx.h>
    #include "Mw.h"
    #include "resource.h"
    
    BOOL Cls_OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
    {
        // llenar el combo con mensajes
    
    //  HERE IS WHERE THIS LITTLE THING DOES NOT ADD 
    ANYTHING.. 
    //(altough the Debugger shows that I'm passing valid strings) :mad: :mad: 
    
        for (int i = 0; i < numMessages; i++)
            SendDlgItemMessage (hwnd, IDC_MESSAGES, CB_ADDSTRING, 0, (LPARAM)handler[i].WM_msg); 
    
        return TRUE;
    }
    
    void Cls_OnClose(HWND hwnd)
    {
        DestroyWindow(hwnd);
    }
    
    void Cls_OnDestroy(HWND hwnd)
    {
        PostQuitMessage(0);
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch (msg)
        {
            HANDLE_MSG(hwnd, WM_CREATE,   Cls_OnCreate);
            HANDLE_MSG(hwnd, WM_CLOSE,    Cls_OnClose);
            HANDLE_MSG(hwnd, WM_DESTROY,  Cls_OnDestroy);
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
        }
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
                       PSTR cmdLine, int nCmdShow)
    {
        static char szClassName[] = "_mwizard";
        HWND        hwnd;
        MSG         msg;
        WNDCLASSEX  wcex;
    
        wcex.cbClsExtra     = 0;
        wcex.hInstance      = hInstance;
        wcex.cbSize         = sizeof(WNDCLASSEX);
        wcex.hbrBackground  = (HBRUSH) (COLOR_BTNFACE+1);
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hIcon          = LoadIcon (NULL, IDI_APPLICATION);
        wcex.hIconSm        = LoadIcon (NULL, IDI_APPLICATION);
        wcex.cbWndExtra     = DLGWINDOWEXTRA;
        wcex.lpfnWndProc    = WndProc;
        wcex.lpszClassName  = szClassName;
        wcex.lpszMenuName   = 0;
        wcex.style          = CS_HREDRAW | CS_VREDRAW;
        
        if (!RegisterClassEx(&wcex))
        {
            MessageBox (NULL, "Error in RegisterClassEx", "error", MB_ICONERROR);
            return 0;
        }
        
        hwnd = CreateDialog (hInstance, MAKEINTRESOURCE(IDD_WIZARD), 0, NULL);
    
        ShowWindow (hwnd, SW_NORMAL);
    
        while (GetMessage(&msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return (int)msg.wParam;
    }

  4. #4
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615
    may be this is because I created the main window using a Dialog. That is, creating a resource dialog with a custom class, and using CreateDialog instead of CreateWindowEx.

    can anyone help me with this?
    i've always this problem when using a dialog as a main window.

  5. #5
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    you filled out your WNDPROC structure so it looks like you might be trying to 'subclass' the dialog into behaving like a pure window maybe?

    based on your wndproc i think it's safe to say you are trying to subclass it.

    if this is the case, (if you added a CLASS "szClassName" to your dialog's resource) then you are right to handle the WM_CREATE message but there are other problems.

    if you do subclass then change your CreateDialog line to this....

    hwnd = CreateDialog (hInstance, szClassName, 0, NULL);

    and return DefDlgProc(hwnd, msg, wParam, lParam); not defwindowproc


    and if you have no idea what i'm talking about with this subclassing stuff (but like i say, i'm pretty sure you are doing it)

    then you instead need to handle WM_INITDIALOG and not WM_CREATE.

    otherwise i dont see a glaring problem

  6. #6
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615
    you filled out your WNDPROC structure so it looks like you might be trying to 'subclass' the dialog into behaving like a pure window maybe?
    yes!

    if you do subclass then change your CreateDialog line to this....

    hwnd = CreateDialog (hInstance, szClassName, 0, NULL);
    but the second parameter of createDialog requires the Dialog template? I'm wrong?

    My program does not even display the main window using szClassName string as the 2nd parameter...

  7. #7
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    lpTemplate
    [in] Specifies the dialog box template. This parameter is either the pointer to a null-terminated character string that specifies the name of the dialog box template or an integer value that specifies the resource identifier of the dialog box template. If the parameter specifies a resource identifier, its high-order word must be zero and its low-order word must contain the identifier. You can use the MAKEINTRESOURCE macro to create this value.

    which means it just matches whatever you named it. i name it the same thing as the class name to avoid confusion on my head but you dont necessarily have to do that.

    here is a sample from a prog of mine.....

    this is my the beginning of my dialog in the resource file *manually add the CLASS line to yours*


    MAINAPP DIALOGEX 0, 0, 340, 262
    STYLE DS_NOIDLEMSG | DS_3DLOOK | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION |
    WS_SYSMENU
    CLASS "MAINAPP"


    then my WinMain....

    blah blah fill in the structure....
    wnd.lpszClassName = "MAINAPP";
    RegisterClass(&wnd);

    hwndMain = CreateDialog(hInst, "MAINAPP", NULL, 0);


    either way definately return DefDlgProc and not DefWindowProc

  8. #8
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615
    Ok, that's what I do, but can you process WM_CREATE messages properly? Or you must go with WM_INITDIALOG?

  9. #9
    Join Date
    Jun 2003
    Posts
    58
    BOOL CALLBACK DlgProc(HWND hDlg,UINT msg,WPARAM
    bool bQuit=false;

    wParam,LPARAM lParam)
    {
    switch(msg)
    {
    case WM_COMMAND :
    {
    switch(LOWORD(wParam))
    {
    case IDOK :
    {EndDialog(hDlg,0);bQuit=true
    };break;
    }
    };break;
    }
    return 0;
    }

    int APIENTRY WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
    {
    // TODO: Place code here.
    MSG msg;
    HWND hWnd=CreateDialog(hInstance,MAKEINTRESOURCE(IDD_MAINDIALOG),NULL,DlgProc);
    ShowWindow(hWnd,nCmdShow);
    UpdateWindow(hWnd);
    while(!quit)
    {
    GetMessage(&msg,NULL,0,0);
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return 0;
    }

    may be you can use code like this for your main Dlg.
    the Procedure you can add your code, IDOK is just for exit button.
    Last edited by SilentJackqh; September 5th, 2003 at 01:43 AM.

  10. #10
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    because it's a dialog it normally doesn't respond to WM_CREATE messages at all.

    HOWEVER, you are subclassing your dialog (as we've already gone over) so now it responds to WM_CREATE and NOT WM_INITDIALOG so that part should be fine how you have it.

    if you want the individual controls to respond to a WM_CREATE message you'd have to basically create them manually or subclass them as soon as the main window is created.

    anyways, the problem probably isn't with your code itself at all.

    go to the resource editor and change the style on the combo box to "Drop List" and not Dropdown.

    then single click the button portion of your combo box in the editor, the cursor will change to a resizing one, then go to the bottom of the combo and make it taller *you're actually just resizing it's dropdown size*.
    that's probably the problem at this point. if you did something like

    SendDlgItemMessage(hwnd, IDC_COMBO, CB_SETCURSEL, 0, 0);

    i'm sure you'd see your text that's there, just try resizing the display portion.
    Last edited by filthy_mcnasty; September 5th, 2003 at 10:32 AM.

  11. #11
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615
    Originally posted by filthy_mcnasty
    if you want the individual controls to respond to a WM_CREATE message you'd have to basically create them manually or subclass them as soon as the main window is created.
    subclass as soon as the main window is created?
    You mean using SetWindowLong?=
    can you give an example, with the code above?

    plz use (CODE) (/CODE) tags.

    thank you.

  12. #12
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    did you even try resizing the combo box in the resource editor? i am willing to bet that that is your problem, nothing to do w/ subclassing anything at all. the strings are being added, use CB_SETCURSEL to move through them if you dont believe they are there. just make the drop down box itself larger.

    the approach you're using is fine to populate the list when the program loads.

    but yes you'd do it in the WM_CREATE or WM_INITDIALOG of the parent but there's really no need to do so. it will work without subclassing.

    Code:
    WNDPROC wpOrig;
    
    wpOrig= (WNDPROC)GetWindowLong(hwndBlah, GWL_WNDPROC);
    SetWindowLong(hwndBlah, GWL_WNDPROC, (long)SubClassProc);
    
    long SubClassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
    
    blah blah blah
    default:
      return CallWindowProc(wpOrig, hwnd, msg, wParam, lParam);
    }

  13. #13
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615
    that's right, but the combo is sized appropiately in the resource editor.

    bye

  14. #14
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    31
    indiocolifa,

    Have you found the solution to your problem with combo box? Well if not yet, below is how I did it.

    ------------------------------------------------------------------------------
    Note, I created all windows in the resource editor of MSVC++

    Code:
    #include <windows.h>
    #include <commctrl.h>
    #include "resource.h"
    #pragma comment( lib, "comctl32.lib" )
    
    
    LRESULT CALLBACK DlgProc(HWND hwnd, UINT message, 
    							 WPARAM wParam, LPARAM lParam) {
    
    	RECT rect;
    	int nTxtLen;
    	char szText[50];
    
    	switch (message) {
    		case WM_INITDIALOG:
    			InitCommonControls();
    
    			//Call GetWindowRect to retrieve top, left, width and height
    			//of the window before changing its height
    			GetWindowRect((HWND)GetDlgItem(hwnd,IDC_COMBO1),&rect);
    
    			//Change the height of combo box so that we can add items into it
    			if ((SetWindowPos((HWND)GetDlgItem(hwnd,IDC_COMBO1),	//handle to combo box
    								NULL,								//no need for a window handle for z-order positioning
    								rect.left,							//the original left location of combo box
    								rect.top,							//the original top location of the combo box
    								rect.right-rect.left,				//the original right position of the combo box
    								80,									//the new height. this enabled me to see the text added
    								SWP_NOMOVE|SWP_NOZORDER)) == 0) {	//0 if SetWindowPos is not successful
    				MessageBox(hwnd,"SetWindowPost function failed!","Status",MB_OK|MB_ICONINFORMATION);
    			}
    
    			//Set the focus to the edit box
    			SetFocus((HWND)GetDlgItem(hwnd,IDC_EDIT1));
    
    			break;
    		case WM_COMMAND:
    			switch (wParam) {
    				case IDC_BUTTON1:	//case add to combo is clicked
    					//Get the length of the text
    					nTxtLen = GetWindowTextLength((HWND)GetDlgItem(hwnd,IDC_EDIT1));
    					
    					if (nTxtLen > 0) {
    						//Get the text typed into edit box (IDC_EDIT1)
    						GetWindowText((HWND)GetDlgItem(hwnd,IDC_EDIT1),szText,nTxtLen+1);
    
    						//Add the text to combo box (IDC_COMBO1)
    						SendMessage((HWND)GetDlgItem(hwnd,IDC_COMBO1), CB_ADDSTRING, 0, (LPARAM)szText);
    
    						//Make latest added text as text of combo box
    						SetWindowText((HWND)GetDlgItem(hwnd,IDC_COMBO1),szText);
    
    						//Clear the edit box
    						SetWindowText((HWND)GetDlgItem(hwnd,IDC_EDIT1),NULL);
    
    						//Notification to the user
    						MessageBox(hwnd,TEXT("Added to combo box"),TEXT("Message"),MB_OK|MB_ICONINFORMATION);
    						
    					} else 
    						
    						//Tell the user what to do
    						MessageBox(hwnd,TEXT("Type text to add"),TEXT("Message"),MB_OK|MB_ICONINFORMATION);
    
    					//Set the focus to the edit box
    					SetFocus((HWND)GetDlgItem(hwnd,IDC_EDIT1));
    					break;
    				case IDCANCEL:
    					EndDialog(hwnd,IDCANCEL);
    					break;
    				case IDC_CLOSE:
    					DestroyWindow(hwnd);
    			}
    		default:
    			return FALSE;
    	}
    
    	return FALSE;
    }
    
    // Program entry point
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int) {		
    	
    	// Display our Dialog with our procedure
    	DialogBox( hInstance, MAKEINTRESOURCE( IDD_FORMVIEW ), 
    			   GetDesktopWindow(), ( DLGPROC )DlgProc );
    	return 0;
    }
    The key solution is changing the height of your combo box by calling the SetWindowPos function.
    I am inviting you to join GreenZap. It is just like PAYPAL but it is more rewarding! You get $25 when you join (FREE to sign up) AND you get rewarded also when someone opened an account using your GreenZap promo code! GreenZap is launching June 1st.

    Click here and PREREGISTER

    Have a break. Visit JonelsPlace

  15. #15
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615
    thank you very much. I fixed it months ago, but your response is very valuable ( in fact, i'm not aware of this SetWindowPos solution)

    again,thank you very much

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured