【问题标题】:GetScreenSize function for C++; "no suitable user defined conversion" (Winforms)C++ 的 GetScreenSize 函数; “没有合适的用户定义转换”(Winforms)
【发布时间】:2021-09-01 04:05:46
【问题描述】:

尝试用 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"

【问题讨论】:

  • 几乎不记得任何关于我在很久以前看过的小型托管 C++,当时 MS 认真地试图向我们推销它......这不应该是 for each(Screen^ screen in Screen::AllScreens) 吗?相当等价的example here。我认为您缺少句柄声明符。
  • 错误信息真的告诉你所有你需要的:你的类型(System::Windows::Forms::Screen)和预期的类型(System::Windows::Forms::Screen^)。

标签: visual-studio winforms visual-c++ c++-cli


【解决方案1】:

按照其他人的建议,我们应该使用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);
    }

注意:我把返回类型从int改成了string,这样就可以得到结果了。

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2017-09-25
    • 2016-02-26
    • 2020-01-13
    • 1970-01-01
    相关资源
    最近更新 更多