문제

INT_PTR CALLBACK ConnectDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc = GetDC(hDlg);
    int wmId, wmEvent;
    static HWND hIpControl;
    static HWND hPort;
    static LPWSTR lpIPAddress = (LPWSTR)malloc(sizeof(LPWSTR));
    static LPWSTR lpPort       = (LPWSTR)malloc(sizeof(LPWSTR));
    static char* IPArgtoFn;
    static size_t IPAddressLength; 
    static size_t PortLength;
    static POINT pt;
    hIpControl = GetDlgItem(hDlg, IDC_IPADDRESS1);
    hPort      = GetDlgItem(hPort, IDC_EDIT2);
    switch (message)
    {
    case WM_INITDIALOG:
        {

        return (INT_PTR)TRUE;
        break;
        }
    case WM_COMMAND:
        {

        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        switch (wmId)
            {
            case IDCANCEL:
                EndDialog(hDlg, wmId);
                break;
            case IDCONNECT:
                IPAddressLength = GetWindowTextLength(hIpControl) + 1;
                PortLength = SendMessage(hPort, WM_GETTEXTLENGTH, 0, 0);
                GetWindowText(hIpControl, lpIPAddress, IPAddressLength);

                pt.x = 10; pt.y = 10;
                wcstombs(IPArgtoFn, lpIPAddress, IPAddressLength);
                //TextOut(hdc, 10, 10, lpIPAddress , IPAddressLength);
                mySocket.ConnectToServer(IPArgtoFn, (int)lpPort, hdc, pt); 

                return (INT_PTR)FALSE;
                break;


            default:
                return DefWindowProc(hDlg, message, wParam, lParam);
                break;
            }
        }
    }
    return (INT_PTR)FALSE;
    }

I know I have the right control selected. Its identifier is IDC_EDIT2 and I know that is right. But whenever I try to retrieve the length of the edit control and save it to the variable PortLength, the value is always 0 when I debug it. I have already tried using GetWindowTextLength with works fine with the IP control but when I use it with the edit control, the length is always 0 no matter how long of a string I enter in the box.

도움이 되었습니까?

해결책

  1. GetDlgItem(hPort, ...) should be GetDlgItem(hDlg,...) for retrieving any child control handles.

  2. Allocate your memory correctly.

  3. Return FALSE only when you do NOT handle the message when using a dialog proc. This tells the invoker to forward to DefDlgProc() so you dont' have to. Dialog callbacks are different than Window callbacks on this regard.

  4. Almost/All of the local vars in this don't need to be statics. Not saving anything there really. Ironically, the child control handles are actually decent candidates to be statics, ironic in the sense that they're somewhat the heart of the issue to begin with.

다른 팁

The immediate error is in this line:

hPort = GetDlgItem(hPort, IDC_EDIT2);

This should be:

hPort = GetDlgItem(hDlg, IDC_EDIT2);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top