0

There is a Windows Forms application with DPI support enabled. Following label is properly aligned when I change display scaling and run the app again. I need to change its location depending to some condition, so I did it in C++ by setting Location property of lbl_multi_component_brine_. In that case DPI support doesn't work for some reason. The label is always shown on that location no matter display scaling. Does it mean that DPI support only works when location is specified in Resource file? Does it mean that I should calculate coordinates taking into account the DPI and set in C++ or there is some solution that will skip that calculations.

  <data name="lbl_multi_component_brine_.AutoSize" type="System.Boolean, mscorlib">
    <value>True</value>
  </data>
  <data name="lbl_multi_component_brine_.Location" type="System.Drawing.Point, System.Drawing">
    <value>19, 10</value>
  </data>
  <data name="lbl_multi_component_brine_.Size" type="System.Drawing.Size, System.Drawing">
    <value>92, 13</value>
  </data>
  <data name="lbl_multi_component_brine_.TabIndex" type="System.Int32, mscorlib">
    <value>148</value>
  </data>
  <data name="lbl_multi_component_brine_.Text" xml:space="preserve">
    <value>Brine</value>
  </data>
  <data name="&gt;&gt;lbl_multi_component_brine_.Name" xml:space="preserve">
    <value>lbl_multi_component_brine_</value>
  </data>
  <data name="&gt;&gt;lbl_multi_component_brine_.Type" xml:space="preserve">
    <value>System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </data>
  <data name="&gt;&gt;lbl_multi_component_brine_.Parent" xml:space="preserve">
    <value>panel_mc_brine_</value>
  </data>
  <data name="&gt;&gt;lbl_multi_component_brine_.ZOrder" xml:space="preserve">
    <value>0</value>
  </data>
Ashot
  • 10,807
  • 14
  • 66
  • 117

1 Answers1

0

I fixed that by considering DPI every time I set coordinate in C++ using this function to transform it:

static int DPI_POS(int pos, System::Windows::Forms::Control^ control) 
{
  System::Drawing::Graphics^ g = control->CreateGraphics();
  return int(pos * g->DpiX / 96);
}
Ashot
  • 10,807
  • 14
  • 66
  • 117
  • 1
    You can use the [Control.LogicalToDeviceUnits Method](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.logicaltodeviceunits?view=windowsdesktop-6.0) to accompish this. Also, if you stay with your code, you should dispose of the Graphics object. – TnTinMn Oct 08 '22 at 16:06
  • Good point! I used that. – Ashot Oct 08 '22 at 21:17