2009-11-08 26 views
2

我正在尝试使用我发现使用Win32的代码。然而,我得到这个错误:Win32不存在 - 我如何声明或引用它?

The Name 'Win32' does not exist in the current context.

我错过了什么?你如何调用/声明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; 
     } 
    } 
+1

编辑您的问题并添加该代码 – Davorin 2009-11-08 03:16:28

+0

我删除了我的答案,因为它在这里不会有用。我没有太多的运气提出任何有用的东西,要么。 – David 2009-11-08 03:28:13

回答

4

那些没有在.NET框架中定义。无论谁写这个代码,他们都定义在另一个类中。它们来自Win32 API,这就是它们被命名为Win32的原因。您需要搜索每个定义并找出它们应该是什么。

我可以告诉你,Win32.WH *是窗口消息,它们只是整数值。

编辑:pinvoke.net有一个window messages的完整列表。

0

是的,做大卫说你说你已经做了什么(pInvoke)。然后查看您找到的来源并查看Win32.WM_PAINTWin32.WM_HSCROLLWin32.WM_VSCROLL的定义。根据我的经验,这些只是手工定义的来源,只是数字,以便于阅读。当然,这些数字与它们在Win32 API中实际定义的内容相对应,但它们在程序员的源代码中被重新定义,以避免直接使用数字,从而导致难以阅读。

3

我怀疑你抓取的代码依赖于this Win32 helper class

添加到您的项目应该解决即时Win32缺失的问题。

+0

是的,这真的解决了Win32的缺失,但现在你已经错过了特定功能的defenition ...我想我必须搜索agn这些功能... – Mazki516 2009-11-08 04:09:20

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操作系统上运行)