Giter Club home page Giter Club logo

Comments (4)

r0x0r avatar r0x0r commented on June 8, 2024

Winforms does not provide modern dialogs, but it seems to be doable by implementing dialogs on top of IFileDialog https://learn.microsoft.com/en-us/windows/win32/shell/common-file-dialog

Microsoft.Win32.OpenFileDialog namespace looks like a promising candidate as well https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.openfiledialog?view=windowsdesktop-8.0&viewFallbackFrom=net-5.0

Contributions welcomed.

from pywebview.

huan1936 avatar huan1936 commented on June 8, 2024

Winforms does not provide modern dialogs, but it seems to be doable by implementing dialogs on top of IFileDialog https://learn.microsoft.com/en-us/windows/win32/shell/common-file-dialog

Microsoft.Win32.OpenFileDialog namespace looks like a promising candidate as well https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.openfiledialog?view=windowsdesktop-8.0&viewFallbackFrom=net-5.0

Contributions welcomed.

I implemented a VistaFolderDialog using IFileDialog and it worked successfully
Although he still has a little problem and can't unadvise

import threading

import clr

clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Threading')
clr.AddReference('System.Reflection')

import System.Windows.Forms as WinForms

from System.Threading import ApartmentState, Thread, ThreadStart

from System.Reflection import Assembly, BindingFlags
from System import UInt32


class VistaDialog:
    foldersFilter = "Folders|\n"
    flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
    windowsFormsAssembly = Assembly.LoadWithPartialName("System.Windows.Forms")
    iFileDialogType = windowsFormsAssembly.GetType("System.Windows.Forms.FileDialogNative+IFileDialog")
    OpenFileDialogType = windowsFormsAssembly.GetType("System.Windows.Forms.OpenFileDialog")
    FileDialogType = windowsFormsAssembly.GetType("System.Windows.Forms.FileDialog")
    createVistaDialogMethodInfo = OpenFileDialogType.GetMethod("CreateVistaDialog", flags)
    onBeforeVistaDialogMethodInfo = OpenFileDialogType.GetMethod("OnBeforeVistaDialog", flags)
    getOptionsMethodInfo = FileDialogType.GetMethod("GetOptions", flags)
    setOptionsMethodInfo = iFileDialogType.GetMethod("SetOptions", flags)
    fosPickFoldersBitFlag = windowsFormsAssembly.GetType(
        "System.Windows.Forms.FileDialogNative+FOS").GetField("FOS_PICKFOLDERS").GetValue(None)

    vistaDialogEventsConstructorInfo = windowsFormsAssembly.GetType(
        "System.Windows.Forms.FileDialog+VistaDialogEvents").GetConstructor(flags, None, [FileDialogType], [])
    adviseMethodInfo = iFileDialogType.GetMethod("Advise")
    unadviseMethodInfo = iFileDialogType.GetMethod("Unadvise")
    showMethodInfo = iFileDialogType.GetMethod("Show")

    def show(self, initialDirectory=None, title=None):
        openFileDialog = WinForms.OpenFileDialog()
        openFileDialog.InitialDirectory = initialDirectory
        openFileDialog.Title = title
        openFileDialog.Filter = self.foldersFilter
        openFileDialog.AddExtension = False
        openFileDialog.CheckFileExists = False
        openFileDialog.DereferenceLinks = True
        openFileDialog.Multiselect = False

        iFileDialog = VistaDialog.createVistaDialogMethodInfo.Invoke(openFileDialog, [])
        VistaDialog.onBeforeVistaDialogMethodInfo.Invoke(openFileDialog, [iFileDialog])
        options = VistaDialog.getOptionsMethodInfo.Invoke(openFileDialog, [])
        options = options.op_BitwiseOr(VistaDialog.fosPickFoldersBitFlag)
        VistaDialog.setOptionsMethodInfo.Invoke(iFileDialog, [options])

        adviseParametersWithOutputConnectionToken = [
            VistaDialog.vistaDialogEventsConstructorInfo.Invoke([openFileDialog]),
            UInt32(0),
        ]
        VistaDialog.adviseMethodInfo.Invoke(iFileDialog, adviseParametersWithOutputConnectionToken)
        result = VistaDialog.showMethodInfo.Invoke(iFileDialog, [None])
        if result == 0:
            return openFileDialog.FileName

        # Unable to unadvise because dwCookie is not updated
        # VistaDialog.unadviseMethodInfo.Invoke(iFileDialog, [adviseParametersWithOutputConnectionToken[1]])
        return ""


def show():
    d = VistaDialog()
    res = d.show()
    print(res)


def create():
    w = WinForms.Form()
    w.Title = "Displaying"

    threading.Thread(target=show).start()
    WinForms.Application().Run(w)


thread = Thread(ThreadStart(create))
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()

from pywebview.

huan1936 avatar huan1936 commented on June 8, 2024

Originally advise should have returned dwCookie, but it did not, so unadvise could not be executed.

from pywebview.

r0x0r avatar r0x0r commented on June 8, 2024

If you can clean it up, feel free to submit a PR.

from pywebview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.