C++ 的 GetScreenSize 函数; "no suitable user defined conversion"(双窗体)

GetScreenSize function for C++; "no suitable user defined conversion" (Winforms)

尝试用 C++ 编写 Winforms 程序,到目前为止一切正常,虽然我无法自动获取屏幕尺寸,有谁知道如何解决这个问题?

  // Utility to get screen size of the user using the application
  public:
      static int GetScreenSize() 
      {
          int x_min, y_min, x_max, y_max;
          x_min = y_min = int::MaxValue;
          x_max = y_max = int::MaxValue;

          for each(Screen screen in Screen::AllScreens)
          {
              // equiv of 'var' (C#) is 'auto' in C++
              auto bounds = screen.Bounds;
              x_min = Math::Min(x_min, bounds.X);
              y_min = Math::Min(y_min, bounds.Y);
              x_max = Math::Max(x_max, bounds.X);
              y_max = Math::Max(y_max, bounds.Y);
          }
      }

Automatic Screen size function

Error; "no suitable user-defined conversion from "System::Windows::Forms::Screen ^" to "System::Windows::Forms::Screen" exists"

按照其他人的建议,我们应该用Screen^ screen代替Screen screen来解决错误。

您可以尝试使用以下代码来获取使用该应用程序的用户的屏幕尺寸。

public:
        static String^ GetScreenSize()
        {
            int x_min, y_min, x_max, y_max;
            x_min = y_min = int::MaxValue;
            x_max = y_max = int::MaxValue;

            for each (Screen^ screen in Screen::AllScreens)
            {
                // equiv of 'var' (C#) is 'auto' in C++
                auto bounds = screen->Bounds;
                x_min = Math::Min(x_min, bounds.X);
                y_min = Math::Min(y_min, bounds.Y);
                x_max = Math::Max(x_max, bounds.X);
                y_max = Math::Max(y_max, bounds.Y);
            }
            String^ result= String::Format("X min is {0}, Y min is {1},X max is {2},Y max is {3}", x_min, y_min, x_max, y_max);
            return result;
        }
#pragma endregion
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
        String^ result=GetScreenSize();
        MessageBox::Show(result);
    }

注意:我把return类型从int改成了string,可以得到结果

结果: