-1

Working in c# with System.Windows.Forms objects in .NET Framework 4.6.1, I have implemented a context menu item to copy a text to the Windows clipboard. When this method is entered, the size of the form is reduced by roughly 20%.

        public override void copyXmlTextToClipboard()
        {
            string xmlText = xmlNode.OuterXml;
            System.Windows.Clipboard.SetText(xmlText);
        }

If I remove the reference to the Windows clipboard by commenting the line like this

        public override void copyXmlTextToClipboard()
        {
            string xmlText = xmlNode.OuterXml;
            // System.Windows.Clipboard.SetText(xmlText);
        }

then the method executes without modifying the form size (but of course also doesn't copy anything to the clipboard). So the problem is definitely related to the reference to the clipboard.

The funny thing is that just entering the method causes the problem before the clipboard is actually accessed. I set a breakpoint on the first opening curly bracket of the method. On hitting the breakpoint, the form size is OK, but on the first single-step into the method before executing the first line, the form size is changed.

This is the appearance before entering the method:

Form before entering the method

This is the appearance after the method has been entered, but before any line of code in it has been executed:

Form after entering the method

Can anyone explain this behavior or propose a solution?

Christopher Hamkins
  • 1,442
  • 9
  • 18
  • The reason is explained here: [DPI Awareness - Unaware in one Release, System Aware in the Other](https://stackoverflow.com/a/50276714/7444103) -- Why are you using the Clipboard class from `PresentationFramework` instead of `System.Windows.Forms.Clipboard`? It's not *forbidden*, of course, but you need to reference other assemblies when you have a class that's included in an assembly you already have. – Jimi May 12 '21 at 13:23
  • Thanks, that's the perfect explanation. To tell the truth, I didn't know there was more than one way of accessing the clipboard. – Christopher Hamkins May 12 '21 at 13:49

1 Answers1

0

Following Jimi's comment to my question and his answer in the link, this was the solution:

        public override void copyXmlTextToClipboard()
        {
            string xmlText = xmlNode.OuterXml;
            System.Windows.Forms.Clipboard.SetText(xmlText);
        }

The text is now copied without any changes in UI resolution.

I'll have to read up on DPI Awareness.

Christopher Hamkins
  • 1,442
  • 9
  • 18