I'm new in WinApi and I have problem with OpenFile function. I want open some other file in my program, whitch means it appear in my edit window. I wrote something like that (that's my open file function)
void OtworzPlik(HWND hwnd)
    {
        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.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
        ofn.lpstrDefExt = "txt";
        if (GetOpenFileName(&ofn))
        {
                HWND hEdit = GetDlgItem(hwnd, ID_MDI_EDIT);
                if (OtworzWEdit(hEdit, szFileName))
                {
                        SendDlgItemMessage(hOknoGlowne, IDC_STATUSBAR, SB_SETTEXT, 0, (LPARAM)"Otwarty...");
                        SendDlgItemMessage(hOknoGlowne, IDC_STATUSBAR, SB_SETTEXT, 1, (LPARAM)szFileName);
                        SetWindowText(hwnd, szFileName);
                }
        }
    }
Later I created next BOOL function:
BOOL OtworzWEdit(HWND hEdit, LPCSTR pszFileName)
    {
        HANDLE hFile;
        BOOL bSuccess = FALSE;
        hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
        if (hFile != INVALID_HANDLE_VALUE)
        {
                DWORD dwFileSize;
                dwFileSize = GetFileSize(hFile, NULL);
                if (dwFileSize != 0xFFFFFFFF)
                {
                        LPSTR pszFileText;
                        pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
                        if (pszFileText != NULL)
                        {
                                DWORD  dwRead;
                                if (ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
                                {
                                        pszFileText[dwFileSize] = 0;
                                        if (SetWindowText(hEdit, pszFileText))
                                                bSuccess = TRUE;
                                }
                                GlobalFree(pszFileText);
                        }
                }
                CloseHandle(hFile);
        }
        return bSuccess;
    }
And mine WM_COMMAND function it's something like that
case ID_PLIK_OTWORZ:
 {
            OtworzPlik(hwnd);
           break;
          }
         break;
I don't know why it wouldn't work, any ideas? The problem what I have is nothing show in my window when I click in my menu open file. Dialog window of course show, but nothing else happend. My application is something like Windows text editor. I hope somebody help understand me the source of the problem.
User contributions licensed under CC BY-SA 3.0