double clicking a file should open it
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: double clicking a file should open it

  1. #1
    Join Date
    Jul 2003
    Posts
    731

    double clicking a file should open it

    I am trying to open the file in my application when you double click the file (or likewise from the context menu). Assume a file type of extension .abc which is unique to the pc.

    I ran this through Jeff Prosis (Programming Windows with MFC) that the following lines should do all the work after AddDocTemplate(pDocTemplate); line in InitInstance()

    PHP Code:
    AddDocTemplate(pDocTemplate);

    CWinApp::RegisterShellFileTypes(TRUE);
    CWinApp::EnableShellOpen(); 
    However this doesn't register the file types and the context menu are the same as before and double click doesn't work. Is there anything else need to be done?

    Thanks

  2. #2
    Join Date
    Sep 1999
    Location
    France
    Posts
    393
    Hi,

    I have VB code that does that.
    Perhaps it could help you.

    Code:
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" ( _
               ByVal hKey As Long, _
               ByVal lpSubKey As String, _
               phkResult As Long _
    ) As Long
    
    Private Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" ( _
               ByVal hKey As Long, _
               ByVal lpSubKey As String, _
               ByVal dwType As Long, _
               ByVal lpData As String, _
               ByVal cbData As Long) _
    As Long
    
    Public Const HKEY_CLASSES_ROOT = &H80000000
    Public Const MAX_PATH = 260&
    Public Const REG_SZ = 1
    
    Public Declare Sub SHChangeNotify Lib "shell32.dll" ( _
               ByVal wEventId As Long, _
               ByVal uFlags As Long, _
               dwItem1 As Any, _
               dwItem2 As Any)
    
    Public Const SHCNE_ASSOCCHANGED = &H8000000
    Public Const SHCNF_IDLIST = &H0&
    
    Public Sub Associate(AppKey As String, _
                         AppName As String, _
                         AppFileExt As String, _
                         AppFileName As String, _
                         Command As String, _
                         IcoPath As String)
    Dim sKeyName As String, sKeyValue As String
    Dim lngRep As Long, lphKey As Long
    
    On Error GoTo Fin
    'Create root key
    sKeyName = AppKey
    sKeyValue = "NLFacilities files"
    lngRep = RegCreateKey(HKEY_CLASSES_ROOT, sKeyName, lphKey)
    lngRep = RegSetValue(lphKey, "", REG_SZ, sKeyValue, 0&)
    'Create association
    sKeyName = AppFileExt
    sKeyValue = AppKey
    lngRep = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey)
    lngRep = RegSetValue&(lphKey, "", REG_SZ, sKeyValue, 0&)
    'Create command line
    sKeyName = AppKey
    sKeyValue = Command
    lngRep = RegCreateKey(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
    lngRep = RegSetValue&(lphKey, "shell\open\command", REG_SZ, sKeyValue, MAX_PATH)
    'Create icon
    sKeyName = AppKey
    sKeyValue = IcoPath
    lngRep = RegCreateKey(HKEY_CLASSES_ROOT, sKeyName, lphKey)
    lngRep = RegSetValue(lphKey, "DefaultIcon", REG_SZ, sKeyValue, MAX_PATH)
    'Notify to system the icon update
    SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0
    Fin:
    End Sub
    
    
    
    Public Sub Main()
    Associate "MyProg", _
                "MyProg", _
                ".MYF", _
                "MyProg", _
                "E:\MyProg\MyProg.exe /Arg(%1)", _
                "E:\MyProg\MyProg.ico"
    End Sub

  3. #3
    Join Date
    Jul 2003
    Posts
    731
    thanks for your post but this all stuff is supposed to be done by MFC. On top it, I have atleast manually associated the file types to my application and the double click of course works this way but I am not able to debug the application for double click! Any idea for that?

    I really need to register the files types from my program, any ideas?

    Thanks

  4. #4
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,915
    Your document string must have correct extension field.
    If not DDE will fail.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  5. #5
    Join Date
    Jul 2003
    Posts
    731
    Can you please elaborate on this!

    Do you mean the string resource associated with document template or the document file name.

    thanks

  6. #6
    Join Date
    Jul 2003
    Posts
    731
    Thanks JohnCz, that was the problem.

    Is there anyway we can associate an icon as well? Everything works fine but the the files icon still remains the same ukknown file types. Can we change that as well from our program?

    Thanks!

  7. #7
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,915
    You are welcome.

    Proper document string for a template should display proper icon.
    If you changed icon or properly register application to process DDE icon will not change automatically in explorer since Windows is caching icons in special file. To fix it you would have to delete this file and reboot computer.
    In all version of Windows but XP file name is ShellIconCache found in windows/winnt directory. For XP you can find it in C:\Documents and Settings\Your User Name\Local Settings\Application Data in Iconcache.db
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  8. #8
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Look at the constructor for CSingleDocTemplate and CMultiDocTemplate; especially CMultiDocTemplate, since it seems to have an example of the document string that is not in the CSingleDocTemplate documentation. Note that the first parameter is "nIDResource". Supposedly it can be the id of an icon, right?
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  9. #9
    Join Date
    Jul 2003
    Posts
    731

    Thumbs up

    only rebooting the pc worked but thanks for the additional info.

    regards,

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center