• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Opening an Input Dialog in Global Setup?

Explorer ,
May 29, 2024 May 29, 2024

Copy link to clipboard

Copied

Working on some tests recently-- trying to open a dialog box in Global Setup for the user to enter a value, and then store said value. 

The box opens, but each time it defaults to "Dialog was Cancelled," without having the opportunity to enter any values. 

I'm not sure if this is an issue with the dialog code, or perhaps theres some caveat to doing this in global setup. Any suggestions would be appreciated! 

This is the code I'm using for the rough test;

// Dialog procedure function
INT_PTR CALLBACK NumberDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	static int* pNumber;

	switch (message)
	{
	case WM_INITDIALOG:
		pNumber = (int*)lParam;
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK)
		{
			BOOL success;
			int number = GetDlgItemInt(hDlg, 1001, &success, TRUE);
			if (success)
			{
				*pNumber = number;
				EndDialog(hDlg, IDOK);
			}
			else
			{
				MessageBoxA(hDlg, "Please enter a valid number.", "Error", MB_OK | MB_ICONERROR);
			}
			return (INT_PTR)TRUE;
		}
		else if (LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, IDCANCEL);
			return (INT_PTR)TRUE;
		}
		break;

	case WM_CLOSE:
		EndDialog(hDlg, IDCANCEL);
		return (INT_PTR)TRUE;
	}
	return (INT_PTR)FALSE;
}

// Function to create the dialog template
DLGTEMPLATE* CreateDialogTemplate()
{
	const size_t templateSize = 1024;
	BYTE* dlgTemplateMem = (BYTE*)GlobalAlloc(GPTR, templateSize);
	if (!dlgTemplateMem)
	{
		MessageBoxA(NULL, "GlobalAlloc failed.", "Error", MB_OK | MB_ICONERROR);
		return NULL;
	}

	DLGTEMPLATE* dlgTemplate = (DLGTEMPLATE*)dlgTemplateMem;
	ZeroMemory(dlgTemplate, templateSize);

	dlgTemplate->style = DS_SETFONT | WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_MODALFRAME;
	dlgTemplate->dwExtendedStyle = 0;
	dlgTemplate->cdit = 3; // Number of controls
	dlgTemplate->x = 10;
	dlgTemplate->y = 10;
	dlgTemplate->cx = 200;
	dlgTemplate->cy = 100;

	BYTE* dlgItems = (BYTE*)dlgTemplate + sizeof(DLGTEMPLATE) + sizeof(WORD) * 2; // Add space for font information

	// Set font information
	*(WORD*)dlgItems = 8; // Font size
	dlgItems += sizeof(WORD);
	memcpy(dlgItems, L"MS Shell Dlg\0", 13 * sizeof(wchar_t)); // Font name
	dlgItems += 13 * sizeof(wchar_t);

	// Define "Number:" static text
	DLGITEMTEMPLATE* dlgItem = (DLGITEMTEMPLATE*)dlgItems;
	dlgItem->style = WS_VISIBLE | WS_CHILD | SS_LEFT;
	dlgItem->dwExtendedStyle = 0;
	dlgItem->x = 10;
	dlgItem->y = 10;
	dlgItem->cx = 50;
	dlgItem->cy = 10;
	dlgItem->id = (WORD)-1;
	dlgItems += sizeof(DLGITEMTEMPLATE);
	*(WORD*)dlgItems = 0xFFFF;
	dlgItems += sizeof(WORD);
	*(WORD*)dlgItems = 0x0082; // STATIC class atom
	dlgItems += sizeof(WORD);
	memcpy(dlgItems, L"Number:\0", 8 * sizeof(wchar_t)); // Text
	dlgItems += 8 * sizeof(wchar_t);
	*(WORD*)dlgItems = 0; // Null-terminator
	dlgItems += sizeof(WORD);

	// Define edit control
	dlgItem = (DLGITEMTEMPLATE*)dlgItems;
	dlgItem->style = WS_VISIBLE | WS_CHILD | WS_BORDER | ES_NUMBER;
	dlgItem->dwExtendedStyle = 0;
	dlgItem->x = 10;
	dlgItem->y = 30;
	dlgItem->cx = 180;
	dlgItem->cy = 20;
	dlgItem->id = 1001;
	dlgItems += sizeof(DLGITEMTEMPLATE);
	*(WORD*)dlgItems = 0xFFFF;
	dlgItems += sizeof(WORD);
	*(WORD*)dlgItems = 0x0081; // EDIT class atom
	dlgItems += sizeof(WORD);
	*(WORD*)dlgItems = 0; // No text
	dlgItems += sizeof(WORD);
	*(WORD*)dlgItems = 0; // Null-terminator
	dlgItems += sizeof(WORD);

	// Define "OK" button
	dlgItem = (DLGITEMTEMPLATE*)dlgItems;
	dlgItem->style = WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON;
	dlgItem->dwExtendedStyle = 0;
	dlgItem->x = 40;
	dlgItem->y = 70;
	dlgItem->cx = 50;
	dlgItem->cy = 14;
	dlgItem->id = IDOK;
	dlgItems += sizeof(DLGITEMTEMPLATE);
	*(WORD*)dlgItems = 0xFFFF;
	dlgItems += sizeof(WORD);
	*(WORD*)dlgItems = 0x0080; // BUTTON class atom
	dlgItems += sizeof(WORD);
	memcpy(dlgItems, L"OK\0", 3 * sizeof(wchar_t)); // Text
	dlgItems += 3 * sizeof(wchar_t);
	*(WORD*)dlgItems = 0; // Null-terminator
	dlgItems += sizeof(WORD);

	// Define "Cancel" button
	dlgItem = (DLGITEMTEMPLATE*)dlgItems;
	dlgItem->style = WS_VISIBLE | WS_CHILD;
	dlgItem->dwExtendedStyle = 0;
	dlgItem->x = 110;
	dlgItem->y = 70;
	dlgItem->cx = 50;
	dlgItem->cy = 14;
	dlgItem->id = IDCANCEL;
	dlgItems += sizeof(DLGITEMTEMPLATE);
	*(WORD*)dlgItems = 0xFFFF;
	dlgItems += sizeof(WORD);
	*(WORD*)dlgItems = 0x0080; // BUTTON class atom
	dlgItems += sizeof(WORD);
	memcpy(dlgItems, L"Cancel\0", 7 * sizeof(wchar_t)); // Text
	dlgItems += 7 * sizeof(wchar_t);
	*(WORD*)dlgItems = 0; // Null-terminator
	dlgItems += sizeof(WORD);

	return dlgTemplate;
}

// Main function
static PF_Err GlobalSetup(PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], PF_LayerDef* output)
{
	try {
		out_data->my_version = PF_VERSION(MAJOR_VERSION, MINOR_VERSION, BUG_VERSION, STAGE_VERSION, BUILD_VERSION);
		out_data->out_flags = PF_OutFlag_DEEP_COLOR_AWARE; // just 16bpc, not 32bpc
		AEGP_SuiteHandler suites(in_data->pica_basicP);
		void* hwndptr = nullptr;
		suites.UtilitySuite6()->AEGP_GetMainHWND(&hwndptr);
		HWND parentHwnd = (HWND)hwndptr;
		int number = 0;

		// Create and display the dialog box
		DLGTEMPLATE* dlgTemplate = CreateDialogTemplate();
		if (dlgTemplate)
		{
			INT_PTR result = DialogBoxIndirectParam(GetModuleHandle(NULL), dlgTemplate, parentHwnd, NumberDialogProc, (LPARAM)&number);

			// Free the allocated memory for the dialog template
			GlobalFree(dlgTemplate);

			if (result == IDOK)
			{
				char message[50];
				sprintf_s(message, "You entered: %d", number);
				MessageBoxA(NULL, message, "Result", MB_OK);
			}
			else
			{
				DWORD dwError = GetLastError();
				char errorMessage[100];
				sprintf_s(errorMessage, "Dialog was cancelled. Error code: %lu", dwError);
				MessageBoxA(NULL, errorMessage, "Result", MB_OK);
			}
		}
		else
		{
			MessageBoxA(NULL, "Failed to create dialog template.", "Error", MB_OK | MB_ICONERROR);
		}
	}
	catch (const std::exception& e) {
		std::cout << e.what() << std::endl;
		return PF_Err_UNRECOGNIZED_PARAM_TYPE;
	}
	return PF_Err_NONE;
}

 

TOPICS
Error or problem , How to , SDK

Views

87

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
no replies

Have something to add?

Join the conversation