不能实例化一些.net对象
本文关键字:net 对象 实例化 不能 | 更新日期: 2023-09-27 17:51:15
我正在尝试实例化System.Drawing.Graphics
对象,但由于某种原因Visual Studio找不到该类。似乎对于。net库中的一些类是这样的,但并不是所有的类都是这样。
我已经检查了目标框架,并尝试使用名称空间实例化。而且,在Visual Studio菜单中没有解析它的选项,所以看起来Visual Studio甚至不知道这个对象。
示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Media;
namespace Notify.Classes
{
class NotificationDrawer
{
[DllImport("User32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern void ReleaseDC(IntPtr hwnd, IntPtr dc);
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
IntPtr desktopPtr = GetDC(IntPtr.Zero);
Graphics g = Graphics.FromHdc(desktopPtr);
SolidBrush b = new SolidBrush(Color.White);
g.FillRectangle(b, new Rectangle(0, 0, 1920, 1080));
g.Dispose();
ReleaseDC(IntPtr.Zero, desktopPtr);
}
}
}
根据文档,System.Drawing.Graphics
在System.Drawing
程序集中。
确保您有System.Drawing
的引用