Jump to page: 1 2
Thread overview
Win32 Api: How create Open/"Save as" Dialog?
Jan 10, 2020
Marcone
Jan 10, 2020
Adam D. Ruppe
Jan 10, 2020
Marcone
Jan 10, 2020
Mike Parker
Jan 11, 2020
Marcone
Jan 11, 2020
René Heldmaier
Jan 11, 2020
Marcone
Jan 11, 2020
Marcone
Jan 11, 2020
John Chapman
Jan 11, 2020
Marcone
Jan 11, 2020
Adam D. Ruppe
Jan 12, 2020
Marcone
January 10, 2020
How create "Open" and "Save as" Dialog using "Win32 Api" and Dlang? Please send me a simple example code in Dlang. Thank you very much.
January 10, 2020
On Friday, 10 January 2020 at 14:48:49 UTC, Marcone wrote:
> How create "Open" and "Save as" Dialog using "Win32 Api" and Dlang? Please send me a simple example code in Dlang. Thank you very much.

You just call GetOpenFileName or GetSaveFileName.

here it is in my minigui.d

https://github.com/adamdruppe/arsd/blob/master/minigui.d#L6634
January 10, 2020
On Friday, 10 January 2020 at 14:52:36 UTC, Adam D. Ruppe wrote:
> On Friday, 10 January 2020 at 14:48:49 UTC, Marcone wrote:
>> How create "Open" and "Save as" Dialog using "Win32 Api" and Dlang? Please send me a simple example code in Dlang. Thank you very much.
>
> You just call GetOpenFileName or GetSaveFileName.
>
> here it is in my minigui.d
>
> https://github.com/adamdruppe/arsd/blob/master/minigui.d#L6634

Very complicated. Can you send me the simple clear code?
January 10, 2020
On Friday, 10 January 2020 at 15:06:07 UTC, Marcone wrote:

>
> Very complicated. Can you send me the simple clear code?

https://docs.microsoft.com/en-us/windows/win32/dlgbox/using-common-dialog-boxes
January 11, 2020
On Friday, 10 January 2020 at 14:48:49 UTC, Marcone wrote:
> How create "Open" and "Save as" Dialog using "Win32 Api" and Dlang? Please send me a simple example code in Dlang. Thank you very much.

Have a look at this website: http://www.winprog.org/tutorial/app_two.html
It helped me a lot when i once made a simple gui in C.
The tutorial is in C, but Win32 Api is C anyway... Most of the examples will probably just compile in D.
Win32 Api is really ugly, i think it's a better investment to learn something like GtkD ;)
January 11, 2020
On Friday, 10 January 2020 at 15:44:53 UTC, Mike Parker wrote:
> On Friday, 10 January 2020 at 15:06:07 UTC, Marcone wrote:
>
>>
>> Very complicated. Can you send me the simple clear code?
>
> https://docs.microsoft.com/en-us/windows/win32/dlgbox/using-common-dialog-boxes

Not compile in Dlang.
January 11, 2020
On Saturday, 11 January 2020 at 00:12:03 UTC, René Heldmaier wrote:
> On Friday, 10 January 2020 at 14:48:49 UTC, Marcone wrote:
>> How create "Open" and "Save as" Dialog using "Win32 Api" and Dlang? Please send me a simple example code in Dlang. Thank you very much.
>
> Have a look at this website: http://www.winprog.org/tutorial/app_two.html
> It helped me a lot when i once made a simple gui in C.
> The tutorial is in C, but Win32 Api is C anyway... Most of the examples will probably just compile in D.
> Win32 Api is really ugly, i think it's a better investment to learn something like GtkD ;)

Tha examples in this site don't compile in Dlang. I want Win32 Api becouse I want create very smal window gui for simple programs.
January 11, 2020
On Friday, 10 January 2020 at 14:48:49 UTC, Marcone wrote:
> How create "Open" and "Save as" Dialog using "Win32 Api" and Dlang? Please send me a simple example code in Dlang. Thank you very much.

This code works, but I can't get file Path. Someone can help me?

import std;
import core.sys.windows.windows;
pragma(lib, "comdlg32");

void main(){
    OPENFILENAME ofn;
    wchar* szFileName;
    (cast(byte*)& ofn)[0 .. ofn.sizeof] = 0;
    ofn.lStructSize = ofn.sizeof;
    ofn.hwndOwner = null;
    ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = "txt";
    if(GetOpenFileNameW(&ofn)){
      writeln(szFileName); // Print null
      writeln(ofn.lpstrFile); // Print null
    }
}
January 11, 2020
On Saturday, 11 January 2020 at 10:34:34 UTC, Marcone wrote:
> This code works, but I can't get file Path. Someone can help me?
>
> import std;
> import core.sys.windows.windows;
> pragma(lib, "comdlg32");
>
> void main(){
>     OPENFILENAME ofn;
>     wchar* szFileName;

You need to supply a buffer, not a pointer:

  wchar[MAX_PATH] szFileName;

>     (cast(byte*)& ofn)[0 .. ofn.sizeof] = 0;
>     ofn.lStructSize = ofn.sizeof;
>     ofn.hwndOwner = null;

The above lines are unnecessary as D structs are automatically initialized and lStructSize is already filled in.

>     ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
>     ofn.lpstrFile = szFileName;

It wants a pointer to your buffer:

  ofn.lpstrFile = szFileName.ptr;

>     ofn.nMaxFile = MAX_PATH;
>     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
>     ofn.lpstrDefExt = "txt";
>     if(GetOpenFileNameW(&ofn)){
>       writeln(szFileName); // Print null
>       writeln(ofn.lpstrFile); // Print null

You'll need to slice the buffer to the right length:

  import core.stdc.wchar_ : wcslen;
  writeln(ofn.lpstrFile[0 .. wcslen(ofn.lpstrFile.ptr)]);

>     }
> }


January 11, 2020
On Friday, 10 January 2020 at 14:48:49 UTC, Marcone wrote:
> How create "Open" and "Save as" Dialog using "Win32 Api" and Dlang? Please send me a simple example code in Dlang. Thank you very much.

Solution:

import std;
import core.sys.windows.windows;
pragma(lib, "comdlg32");

// Function askopenfilename()
string askopenfilename(const(wchar)* filter = ""){
    OPENFILENAME ofn;
    wchar[1024] szFileName = 0;
    ofn.lpstrFile = cast(LPWSTR) szFileName;
    ofn.lpstrFilter = filter;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    if (GetOpenFileNameW(&ofn)){
        return to!string(szFileName[0..szFileName.indexOf('\0')]);
    } else {
        return "";
    }
}

void main(){
    writeln(askopenfilename()); // Call without filter.
    writeln(askopenfilename("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0")); // Cal using filter.
}
« First   ‹ Prev
1 2