2011-09-07 61 views
2

我試圖爲我的視頻播放器提供工具提示。我使用Windows媒體播放器嵌入到C#的Winform應用程序中(AxWMPLib.AxWindowsMediaPlayer)以播放視頻。我創建了一個顯示媒體當前位置的控件。這個控制是透明的。控制的代碼如下:不重疊的透明控件

namespace player.Controls 
{ 
public partial class TransparentToolTip : System.Windows.Forms.UserControl 
{ 
    public enum PointerLocation : byte { ... } 

    #region Private data 
    // ... 
    #endregion 

    Timer Wriggler = new Timer(); 
    int iInterval = 100; 
    protected void TickHandler(object sender, EventArgs e) 
    { 
     this.InvalidateEx(); 
    } 

    private void _SetStyle() 
    { 
     this.SetStyle((ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.CacheText | ControlStyles.ContainerControl), true); 
     this.SetStyle(ControlStyles.Selectable, false); 
     this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.Opaque, true);    
     this.UpdateStyles(); 
    } 
    private void _SetTimer(int _Interval) 
    { 
     Wriggler.Tick += new EventHandler(TickHandler); 
     this.Wriggler.Interval = _Interval; 
     this.Wriggler.Enabled = true; 
    }  

    public TransparentToolTip() 
    {    
     InitializeComponent(); 
     _SetStyle(); 
     _SetTimer(iInterval);      
    } 
    public TransparentToolTip(System.ComponentModel.IContainer container) 
    {    
     container.Add(this);    
     InitializeComponent();    
     _SetStyle(); 
     _SetTimer(iInterval); 
    }   

    protected override CreateParams CreateParams 
    { 
     get 
     { 
      CreateParams cp = base.CreateParams; 
      cp.ExStyle |= (0x00000020 | 0x00000008); // WS_EX_TRANSPARENT = 0x00000020, WS_EX_TOPMOST = 0x00000008    
      return cp; 
     } 
    } 
    #region Extra Properties 
    // ... 
    #endregion 

    // Drawing    
    protected void InvalidateEx() 
    { 
     if (Parent == null) 
      return; 
     Rectangle rc = new Rectangle(this.Location, this.Size); 
     Parent.Invalidate(rc, true); 
    } 
    protected override void OnPaintBackground(PaintEventArgs pevent) 
    { 

    } 
    protected override void OnPaint(PaintEventArgs pe) 
    {   
     pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias; 
     pe.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     pe.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; 
     pe.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; 

     Rectangle rect = new Rectangle(this.ClientRectangle.X, this.ClientRectangle.Y, this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1);    
     SolidBrush brushBackground = new SolidBrush(ColorBackground); 
     SolidBrush brushBorder = new SolidBrush(ColorBorder); 
     using (GraphicsPath graphicsPath = ToolTipBody(...)) 
     { 
      using (Pen p = new Pen(brushBorder, BorderSize)) 
      { 
       pe.Graphics.FillPath(brushBackground, graphicsPath); // background 
       pe.Graphics.DrawPath(p, graphicsPath); // borders 
      } 
     }    

     TextFormatFlags flags = // some flags; 
     TextRenderer.DrawText(pe.Graphics, ToolTipText, Font, new Rectangle(this.ClientRectangle.X, this.ClientRectangle.Y, this.ClientRectangle.Width, this.ClientRectangle.Height - TriangleSizeSide), ToolTipColor, System.Drawing.Color.Transparent, flags); 
     brushBorder.Dispose(); 
     brushBackground.Dispose(); 

     base.OnPaint(pe); 
    } 
    // Form mapping tips 
    private GraphicsPath ToolTipBody(...) 
    { 
     // some code 
     return graphicsPath; 
    } 
} 

我試圖通過axWindowsMediaPlayer對象顯示此工具提示。但我的控制權被媒體播放器重疊。我試着使用SetWindowPos但這不起作用:

static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); 
const int SWP_NOSIZE = 0x0001; 
const int SWP_NOMOVE = 0x0002; 
const int SWP_SHOWWINDOW = 0x0040; 
//... 
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] 
[return: MarshalAs(UnmanagedType.Bool)] 
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int flags); 
//... 
SetWindowPos(transparentToolTip1.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); 

注:如果不改變的新控制它吸引了媒體播放器的的CreateParams。但它變得不透明。

是否有任何想法可以正確使用

+0

請編輯您的問題並提供一個有意義的標題。 – Mat

+0

請修復問題的標題/ –

+0

抱歉。已經完成了。 – Brain89

回答

1

我的猜測是透明的事情正在發生,因爲你定義的控制風格爲ControlStyles.Opaque。然後,將標誌設置爲WS_EX_TRANSPARENT,您又將其設置爲透明樣式。

關於重疊的問題:如果您使用Visual Studio Windows窗體設計器設計窗體,也許您的控件是沿着AxWMPLib.AxWindowsMediaPlayer組件排列的。在顯示之前,請嘗試在控制集合中嘗試bring to frontcontrol the ChildrenIndex

希望它有幫助。

+0

方法[Control.BringToFront](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.bringtofront.aspx)和[Control.ControlCollection.SetChildIndex](http:// msdn .microsoft.com/en-us/library/system.windows.forms.control.controlcollection.setchildindex.aspx)不能解決問題。自定義控件仍然在視頻背後。所以我決定使用具有設置屬性** TransparencyKey **和** Opacity **的自定義表單,而不是使用自定義控件 – Brain89