setDialog method

int setDialog({
  1. required int style,
  2. required int cdit,
  3. required int cx,
  4. required int cy,
  5. int dwExtendedStyle = 0,
  6. int x = 0,
  7. int y = 0,
  8. int windowSystemClass = 0,
  9. String windowClass = '',
  10. String title = '',
  11. String fontName = '',
  12. int fontSize = 0,
})

Writes a DLGTEMPLATE and its variable-length fields into memory.

The template is written starting at this pointer and includes, in order:

  • DLGTEMPLATE structure
  • Menu specification
  • Window class specification
  • Dialog title
  • Optional font size and typeface (if DS_SETFONT is set)

Returns the number of 16-bit WORDs written.

The returned value can be used as an offset (in WORDs) to write the first DLGITEMTEMPLATE.

Preconditions:

  • The target memory must be writable and large enough to hold all fields.
  • All strings are encoded as UTF-16 and NUL-terminated.
  • The resulting structure is DWORD-aligned as required by Win32.

Implementation

int setDialog({
  required int style,
  required int cdit,
  required int cx,
  required int cy,
  int dwExtendedStyle = 0,
  int x = 0,
  int y = 0,
  int windowSystemClass = 0,
  String windowClass = '',
  String title = '',
  String fontName = '',
  int fontSize = 0,
}) {
  const dlgTemplateSize = 9; // Size in WORDs of DLGTEMPLATE.

  final ptr = cast<WORD>();
  var idx = 0;

  // Fixed DLGTEMPLATE header.
  ptr.cast<DLGTEMPLATE>().ref
    ..style = style
    ..dwExtendedStyle = dwExtendedStyle
    ..cdit = cdit
    ..x = x
    ..y = y
    ..cx = cx
    ..cy = cy;
  idx += dlgTemplateSize;

  // Immediately following the DLGTEMPLATE structure is a menu array that
  // identifies a menu resource for the dialog box. If the first element of
  // this array is 0x0000, the dialog box has no menu and the array has no
  // other elements. Since Dart does not currently support embedding a
  // resource file in a Windows executable, the other settings are moot.
  ptr[idx++] = 0x0000;

  // Following the menu array is a class array that identifies the window
  // class of the dialog box. If the first element of the array is 0x0000, the
  // system uses the predefined dialog box class for the dialog box and the
  // array has no other elements. If the first element is 0xFFFF, the array
  // has one additional element that specifies the ordinal value of a
  // predefined system window class. If the first element has any other value,
  // the system treats the array as a null-terminated Unicode string that
  // specifies the name of a registered window class.
  if (windowClass.isNotEmpty) {
    idx += (PWSTR((ptr + idx).cast<Utf16>()).setString(windowClass) / 2)
        .ceil();
  } else {
    if (windowSystemClass != 0) {
      ptr[idx++] = 0xFFFF;
      ptr[idx++] = windowSystemClass;
    } else {
      ptr[idx++] = 0x0000;
    }
  }

  // Following the class array is a title array that specifies a
  // null-terminated Unicode string that contains the title of the dialog box.
  // If the first element of this array is 0x0000, the dialog box has no title
  // and the array has no other elements.
  if (title.isEmpty) {
    ptr[idx++] = 0x0000;
  } else {
    idx += (PWSTR((ptr + idx).cast<Utf16>()).setString(title) / 2).ceil();
  }

  // Following the title array is a 16-bit point size value and the typeface
  // array, in that order, if the style member specifies the DS_SETFONT style.
  // These are set for the dialog box. The typeface array is a null-terminated
  // Unicode string.
  if ((style & DS_SETFONT == DS_SETFONT) && (fontName.isNotEmpty)) {
    ptr[idx++] = fontSize;
    idx += (PWSTR((ptr + idx).cast<Utf16>()).setString(fontName) / 2).ceil();
  }

  // Each DLGTEMPLATE structure in the template must be aligned on a DWORD
  // boundary.
  if ((ptr.address + idx) % 2 != 0) {
    ptr[idx++] = 0x0000;
  }

  return idx;
}