www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Win32 Api: How create Open/"Save as" Dialog?

reply Marcone <marcone email.com> writes:
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.
Jan 10 2020
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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
Jan 10 2020
parent reply Marcone <marcone email.com> writes:
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?
Jan 10 2020
parent reply Mike Parker <aldacron gmail.com> writes:
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
Jan 10 2020
parent Marcone <marcone email.com> writes:
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.
Jan 11 2020
prev sibling next sibling parent reply =?UTF-8?B?UmVuw6k=?= Heldmaier <rene.heldmaier gmail.com> writes:
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 ;)
Jan 10 2020
parent Marcone <marcone email.com> writes:
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.
Jan 11 2020
prev sibling next sibling parent reply Marcone <marcone email.com> writes:
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 } }
Jan 11 2020
parent John Chapman <johnch_atms hotmail.com> writes:
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)]);
     }
 }
Jan 11 2020
prev sibling parent reply Marcone <marcone email.com> writes:
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. }
Jan 11 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 11 January 2020 at 12:22:25 UTC, Marcone wrote:
     wchar[1024] szFileName = 0;
     ofn.lpstrFile = cast(LPWSTR) szFileName;
You shouldn't cast there, just use `szFileName.ptr` instead.
     ofn.nMaxFile = MAX_PATH;
and this should be the length of the array. It may require a case nMaxFile = cast(DWORD) szFileName.length;
Jan 11 2020
parent Marcone <marcone email.com> writes:
On Saturday, 11 January 2020 at 14:21:25 UTC, Adam D. Ruppe wrote:
 On Saturday, 11 January 2020 at 12:22:25 UTC, Marcone wrote:
     wchar[1024] szFileName = 0;
     ofn.lpstrFile = cast(LPWSTR) szFileName;
You shouldn't cast there, just use `szFileName.ptr` instead.
     ofn.nMaxFile = MAX_PATH;
and this should be the length of the array. It may require a case nMaxFile = cast(DWORD) szFileName.length;
Changed! New code with changes working very well: import std; import core.sys.windows.windows; pragma(lib, "comdlg32"); // Function askopenfilename() string askopenfilename(const(wchar)* filter = "All Files (*.*)\0*.*\0"){ OPENFILENAME ofn; wchar[1024] szFileName = 0; ofn.lpstrFile = szFileName.ptr; ofn.lpstrFilter = filter; ofn.nMaxFile = cast(DWORD) szFileName.length; 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. }
Jan 12 2020