0

I'm using MS visual c++ 2010 express to write a notepad program. I need to be able to save files from my tabbed interface ( each edit control is stored into a vector ). I can't seem to figure out how to save, depending on which edit control is visible to the user ( active tab ). Everything I've tried so far either doesn't save the contents or only saves from the first edit control. When the user adds a new file to the tabbed interface, it added that edit control to the back of the vector. This is my latest attempt:

    std::vector<HWND>vect;

    BOOL SaveTextFileFromEdit( HWND hEdit, LPCTSTR pszFileName )
    {
      HANDLE hFile;
      BOOL bSuccess = FALSE;

      hFile = CreateFile( pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );

      if( hFile != INVALID_HANDLE_VALUE )
      {
        DWORD dwTextLength = GetWindowTextLength( hEdit );

        if( dwTextLegth > 0 )
        {
          DWORD dwBufferSize = dwTextLength + 1;
          LPSTR pszText = ( LPSTR )GlobalAlloc( GPTR, dwBufferSize );

          if( pszText != NULL )
          {
            if( GetWindowText( hEdit, pszText, dwBufferSize ) )
            {
              DWORD dwWritten;

              if( WriteFile( hFile, pszText, dwTextLength, &dwWritten, NULL ) )
                bSuccess = TRUE;
            }
            GlobalFree( pszText );
          }
        }
        CloseHandle( hFile );
      }
      return bSuccess;
    }

    LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {
      case WM_COMMAND:
        switch( LOWORD( wParam ) )
        {
          case ID_FILE_SAVEAS:
            {
              OPENFILENAME ofn;
              char szFileName[ MAX_PATH ] = "";
              ZeroMemory( &ofn, sizeof( ofn ) );

              ofn.lStructSize = sizeof( ofn );
              ofn.hwndOwner   = hwnd;
              ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
              ofn.lpstrFile   = szFileName;
              ofn.nMaxFile    = MAX_PATH;
              ofn.lpstrDefExt = "txt";
              ofn.flags       = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

              if( GetSaveFileName( &ofn ) )
              {
                HWND hEdit, hTabs;
                hTabs = GetDlgItem( hwnd, IDC_MAIN_TAB );
                int curTab = TabCtrl_GetCurSel( hTabs );

                hEdit = GetDlgItem( hTabs, IDC_MAIN_EDIT );
                // This is what i did have: hEdit = GetDlgItem( vect[ curTab ], IDC_MAIN_EDIT );
                // Which wasn't saving anything
                if( SaveTextFileFromEdit( hEdit, szFileName ) )
                {
                  // EVERYTHING IS GOOD
                }
              }
            }
            break;
        }
        break;
    }
    return 0;
}
Vince
  • 2,596
  • 11
  • 43
  • 76

1 Answers1

1

If the vector has each edit control's HWND then you don't need GetDlgItem at all: You already have the HWND that it would return.

hEdit = vect[ curTab ];
ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15