nabi
nabi

Reputation: 1

Windows API OPENFILENAME extension filter problem

I have this openfile() function with OPENFILENAME structure, the lpstrFilter does not filter file types at all, the Dialog Box shows all types of files which I want to prevent, it should normally show only selected file types according to the extension filter , and also when the user select another file type for example .PNG files , it should update the files in the Dialog box and show only PNG files which is not the case , so what is wrong in this code?, is there any specific Flags that help solve the problem?

void openfile() {        
    ZeroMemory(&opn, sizeof(opn));
    opn.lStructSize = sizeof(opn);
    opn.hwndOwner = hWindow;
    opn.lpstrFile = tz1;
    opn.lpstrFile[0] = '\0';
    opn.nMaxFile = sizeof(tz1);
    opn.lpstrFilter = "JPG - JPEG File\0*.JPG\0PNG File\0*.PNG\0BMP - Bitmat File\0*.BMP\0";
    opn.nFilterIndex = 2;
    opn.lpstrFileTitle = NULL;
    opn.lpstrTitle = "Select an Image";
    opn.nMaxFileTitle = 0;
    opn.lpstrInitialDir = NULL; 
    opn.hInstance = hInstance;
    opn.lpstrDefExt = ("JPG");
    opn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
    opn.lpfnHook = NULL;
    bfile = 0;
    if (GetOpenFileName(&opn)) {
        
        if (access(tz1, F_OK) == -1){
        // showing some file does not exist message
         }
  } // if Get
} // openfile()

Upvotes: 0

Views: 308

Answers (1)

nabi
nabi

Reputation: 1

I am posting my solution here it might help those who have the same problem, maybe there is a better solution but this is what I tried, I added a lpfnHook function to refresh the files in the OPENFILENAME Dialog Window.

void openfile() { 
    ZeroMemory(&opn, sizeof(opn));
    opn.lStructSize = sizeof(opn);
    opn.hwndOwner = hDlg; // HWND of the OPENFILENAME Dialog Window
    opn.lpstrFile = tz1;
    opn.lpstrFile[0] = '\0';
    opn.nMaxFile = sizeof(tz1);
    opn.lpstrFilter = "JPG - JPEG File\0*.JPG\0PNG File\0*.PNG\0BMP - Bitmat File\0*.BMP\0\0";
    opn.nFilterIndex = fidx; // could be 1,2 or 3
    opn.lpstrFileTitle = NULL;
    opn.lpstrTitle = "Select an Image";
    opn.nMaxFileTitle = 0;
    opn.lpstrInitialDir = NULL; // NULL is original
    opn.hInstance = hInstance;
    opn.lpstrDefExt = NULL;
    opn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
    opn.lpfnHook = Refresh;     // The lpfnHook function   
    bfile = 0;
    if (GetOpenFileName(&opn)) {
            if (access(tz1, F_OK) == -1){
            // file does not exist message
        } 
        
    } // if Get
} // openfile()



UINT_PTR CALLBACK Refresh(HWND hDlog, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message)
    {
      
    case WM_NOTIFY:
        
            if (opn.nFilterIndex !=fidx)
            {
                fidx = opn.nFilterIndex;
                PostMessage(hDlg, WM_KEYDOWN, VK_F5, 0);
                
             }
            break;
       }
    return TRUE;
} // end of HOOK


  

Upvotes: 0

Related Questions