我正在尝试使用我发现使用Win32的代码。但是,我收到了这个错误:
当前上下文中不存在名称“Win32”。
我错过了什么?你怎么称呼/声明Win32?
public class TransparentTextBox : TextBox
{
PictureBox pictureBox = new PictureBox();
public TransparentTextBox()
{
pictureBox.Dock = DockStyle.Fill;
this.Controls.Add(pictureBox);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case Win32.WM_PAINT:
Bitmap bmpCaptured =
new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
Bitmap bmpResult =
new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
Rectangle r =
new Rectangle(0, 0, this.ClientRectangle.Width,
this.ClientRectangle.Height);
CaptureWindow(this, ref bmpCaptured);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
ImageAttributes imgAttrib = new ImageAttributes();
ColorMap[] colorMap = new ColorMap[1];
colorMap[0] = new ColorMap();
colorMap[0].OldColor = Color.White;
colorMap[0].NewColor = Color.Transparent;
imgAttrib.SetRemapTable(colorMap);
Graphics g = Graphics.FromImage(bmpResult);
g.DrawImage(bmpCaptured, r, 0, 0, this.ClientRectangle.Width,
this.ClientRectangle.Height, GraphicsUnit.Pixel, imgAttrib);
g.Dispose();
pictureBox.Image = (Image)bmpResult.Clone();
break;
case Win32.WM_HSCROLL:
case Win32.WM_VSCROLL:
this.Invalidate(); // repaint
// if you use scrolling then add these two case statements
break;
}
}
答案 0 :(得分:5)
这些没有在.NET框架中定义。编写此代码的人将其定义在另一个类中。它们来自Win32 API,这就是它们被命名为Win32的原因。您需要搜索每个定义,并找出它们应该是什么。
我可以告诉你,Win32.WH *是窗口消息,它们只是整数值。
修改:pinvoke.net包含window messages的完整列表。
答案 1 :(得分:4)
我怀疑你已经抓住了依赖this Win32 helper class的代码。
将其添加到项目中应解决Win32缺失问题。
答案 2 :(得分:0)
是的,做大卫所说的你说你已经做过的事(pInvoke)。然后查看您找到的来源,查看Win32.WM_PAINT
,Win32.WM_HSCROLL
和Win32.WM_VSCROLL
的定义位置。根据我的经验,这些只是在源中手工定义,只是数字,以使其更容易阅读。当然这些数字对应于他们在Win32 API中实际定义的数字,但程序员在源代码中重新定义它们以避免直接使用数字,因此使其更难阅读。
答案 3 :(得分:0)
另一方面,如果你想要一个透明的文本框,为什么不参考PresentationCore / PresentationFramework / WindowsFormsIntegration并使用它来创建一个透明的文本框:
using WPFTextBox = System.Windows.Controls.TextBox;
using WPFBrushes = System.Windows.Media.Brushes;
using WPFElementHost = System.Windows.Forms.Integration;
...
var textBox = new WPFTextBox { Background = WPFBrushes.Transparent };
var host = new WPFElementHost { Dock = DockStyle.Fill, Child = textBox };
然后将主机添加到您的容器中,您就可以开始了。比搞乱一堆Win32复杂性容易得多,并且易于包装在自定义类中。
为什么在WPF中实现这些功能时,为什么要击败WinForms呢? Windows 98,ME和2000现在占市场总量的约1%。 (WPF不能在XP以下的任何Windows操作系统上运行)