setDialogItem method

int setDialogItem({
  1. required int style,
  2. required int x,
  3. required int y,
  4. required int cx,
  5. required int cy,
  6. required int id,
  7. int dwExtendedStyle = 0,
  8. int windowSystemClass = 0,
  9. String windowClass = '',
  10. String text = '',
  11. List<int> creationDataBytes = const [],
})

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

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

  • DLGITEMTEMPLATE structure
  • Control class specification
  • Control text
  • Optional creation data

Returns the number of 16-bit WORDs written.

The returned value can be used to locate the next dialog item template.

Preconditions:

  • The target memory must be writable and properly aligned.
  • The caller must ensure sufficient space for all variable-length fields.
  • Strings are written as UTF-16 and NUL-terminated.

Implementation

int setDialogItem({
  required int style,
  required int x,
  required int y,
  required int cx,
  required int cy,
  required int id,
  int dwExtendedStyle = 0,
  int windowSystemClass = 0,
  String windowClass = '',
  String text = '',
  List<int> creationDataBytes = const [],
}) {
  const dlgItemTemplateSize = 9; // Size in WORDs of DLGITEMTEMPLATE.

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

  // Fixed DLGITEMTEMPLATE header.
  ptr.cast<DLGITEMTEMPLATE>().ref
    ..style = style
    ..dwExtendedStyle = dwExtendedStyle
    ..x = x
    ..y = y
    ..cx = cx
    ..cy = cy
    ..id = id;
  idx += dlgItemTemplateSize;

  // Immediately following each DLGITEMTEMPLATE structure is a class array
  // that specifies the window class of the control. If the first element of
  // this array is any value other than 0xFFFF, the system treats the array as
  // a null-terminated Unicode string that specifies the name of a registered
  // window class. If the first element is 0xFFFF, the array has one
  // additional element that specifies the ordinal value of a predefined
  // system class.
  //
  // The ordinal can be one of the following atom values:
  // - 0x0080: Button
  // - 0x0081: Edit
  // - 0x0082: Static
  // - 0x0083: List box
  // - 0x0084: Scroll bar
  // - 0x0085: Combo box
  if (windowClass.isNotEmpty) {
    idx += (PWSTR((ptr + idx).cast<Utf16>()).setString(windowClass) / 2)
        .ceil();
  } else {
    ptr[idx++] = 0xFFFF;
    ptr[idx++] = windowSystemClass;
  }

  // Following the class array is a title array that contains the initial text
  // or resource identifier of the control.
  idx += (PWSTR((ptr + idx).cast<Utf16>()).setString(text) / 2).ceil();

  // The creation data array begins at the next WORD boundary after the title
  // array. This creation data can be of any size and format. If the first
  // word of the creation data array is nonzero, it indicates the size, in
  // bytes, of the creation data (including the size word).
  if (creationDataBytes.isNotEmpty) {
    ptr[idx++] = creationDataBytes.length + 1;
    (ptr + idx)
      ..cast<BYTE>()
      ..asTypedList(creationDataBytes.length).setAll(0, creationDataBytes);
    idx += (creationDataBytes.length / 2).ceil();
  } else {
    ptr[idx++] = 0x0000;
  }

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

  return idx;
}