www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Win32api: How to send a Struct in DialogBox?

reply Marcone <marcone email.com> writes:
struct Teste {
     string nome = "Paul";
     int idade = 33;
}

extern(Windows):
BOOL DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM 
lParam) nothrow
{
     // I want to be able to use the struct object here.
     // writeln(test.nome);
     return false;
}

void chamaJanela(){
     Test test; // How to send this object to the DlgMain function?
     DialogBox(null, MAKEINTRESOURCE(IDD_DIALOG1), null, &DlgMain);
}
Oct 13 2020
parent reply starcanopy <starcanopy protonmail.com> writes:
On Tuesday, 13 October 2020 at 22:26:35 UTC, Marcone wrote:
 struct Teste {
     string nome = "Paul";
     int idade = 33;
 }

 extern(Windows):
 BOOL DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM 
 lParam) nothrow
 {
     // I want to be able to use the struct object here.
     // writeln(test.nome);
     return false;
 }

 void chamaJanela(){
     Test test; // How to send this object to the DlgMain 
 function?
     DialogBox(null, MAKEINTRESOURCE(IDD_DIALOG1), null, 
 &DlgMain);
 }
Instead of calling DialogBox, you might try DialogBoxParam[A|W].
Before displaying the dialog box, the function passes an 
application-defined
value to the dialog box procedure as the lParam parameter of the 
WM_INITDIALOG
message. An application can use this value to initialize dialog 
box controls.
- https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-dialogboxparamw
Oct 13 2020
next sibling parent Marcone <marcone email.com> writes:
On Tuesday, 13 October 2020 at 22:46:36 UTC, starcanopy wrote:
 On Tuesday, 13 October 2020 at 22:26:35 UTC, Marcone wrote:
 [...]
Instead of calling DialogBox, you might try DialogBoxParam[A|W].
[...]
- https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-dialogboxparamw
Can you make a example with my code?
Oct 13 2020
prev sibling parent reply Marcone <marcone email.com> writes:
DialogBoxParam receive need "int". How convert struct object 
reference to int and cast to struct type after?

I will cast using:

Teste * params = cast(Teste*) LOWORD(lParam);

writeln(params.nome);
writeln(params.idade);


But here, how can send struct?

DialogBoxParam(null, MAKEINTRESOURCE(IDD_DIALOG1), null, 
&DlgMain, &test); // Error, need "int" but I am sending Test*
Oct 13 2020
parent reply Marcone <marcone email.com> writes:
Solved:

Teste * params = cast(Teste*) lParam;

writeln(params.nome);
writeln(params.idade);


But here, how can send struct?

DialogBoxParam(null, MAKEINTRESOURCE(IDD_DIALOG1), null, 
&DlgMain, cast(int) &test); // Error, need "int" but I am sending 
Test*
Oct 13 2020
parent starcanopy <starcanopy protonmail.com> writes:
On Wednesday, 14 October 2020 at 00:07:10 UTC, Marcone wrote:
 Solved:

 Teste * params = cast(Teste*) lParam;

 writeln(params.nome);
 writeln(params.idade);


 But here, how can send struct?

 DialogBoxParam(null, MAKEINTRESOURCE(IDD_DIALOG1), null, 
 &DlgMain, cast(int) &test); // Error, need "int" but I am 
 sending Test*
Sorry, I was going to create an example for you, but something came up. Thankfully, it looks like you've solved it in your own way (You might want to cast &test to LPARAM instead of int.), but if you have any more D/WinApi questions, you might want to check here: https://github.com/AndrejMitrovic/DWinProgramming.
Oct 13 2020