2017-04-17 247 views
0

我使用DWM縮略圖來創建窗口選擇器,但我想添加此縮略圖作爲WPF的StackPanl的孩子我該怎麼做? 因此,在這段代碼中,我有一個名爲'b'的網格,我想添加App Thumbnail作爲它的孩子。添加DWM縮略圖作爲WPF控制子

這是代碼:

 #region dwmapi.dll imports 

    [DllImport("dwmapi.dll", PreserveSig = false)] 
    private static extern bool DwmIsCompositionEnabled(); 

    [DllImport("dwmapi.dll")] 
    private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins); 

    [StructLayout(LayoutKind.Sequential)] 
    struct MARGINS 
    { 
     public int Left; 
     public int Right; 
     public int Top; 
     public int Bottom; 

     public void ExtendToWholeClientArea() 
     { 
      this.Left = -1; 
      this.Right = -1; 
      this.Top = -1; 
      this.Bottom = -1; 
     } 
    } 

    [DllImport("dwmapi.dll")] 
    private static extern int DwmRegisterThumbnail(IntPtr dest, IntPtr src, out IntPtr thumb); 

    [DllImport("dwmapi.dll")] 
    private static extern int DwmUnregisterThumbnail(IntPtr thumb); 

    [DllImport("dwmapi.dll")] 
    private static extern int DwmQueryThumbnailSourceSize(IntPtr thumb, out PSIZE size); 

    [StructLayout(LayoutKind.Sequential)] 
    private struct PSIZE 
    { 
     public int x; 
     public int y; 
    } 

    [DllImport("dwmapi.dll")] 
    private static extern int DwmUpdateThumbnailProperties(IntPtr hThumb, ref DWM_THUMBNAIL_PROPERTIES props); 

    [StructLayout(LayoutKind.Sequential)] 
    private struct DWM_THUMBNAIL_PROPERTIES 
    { 
     public int dwFlags; 
     public RECT rcDestination; 
     public RECT rcSource; 
     public byte opacity; 
     public bool fVisible; 
     public bool fSourceClientAreaOnly; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    private struct RECT 
    { 
     public RECT(int left, int top, int right, int bottom) 
     { 
      Left = left; 
      Top = top; 
      Right = right; 
      Bottom = bottom; 
     } 

     public int Left; 
     public int Top; 
     public int Right; 
     public int Bottom; 
    } 

    private static readonly int DWM_TNP_VISIBLE = 0x8; 
    private static readonly int DWM_TNP_RECTDESTINATION = 0x1; 
    private static readonly int DWM_TNP_SOURCECLIENTAREAONLY = 0x00000010; 

    #endregion 

private void DrawThumbnail(WindowInfo win, int thumbnailIndex){ IntPtr thumbnail = win.Thumbnail; 
     if (thumbnail != IntPtr.Zero) 
      DwmUnregisterThumbnail(thumbnail); 
     int hResult = DwmRegisterThumbnail(mainWindowHandle, win.HWnd, out thumbnail); 
     if (hResult == 0) 
     { 
      PSIZE size; 
      DwmQueryThumbnailSourceSize(thumbnail, out size); 

      DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES(); 
      props.dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_SOURCECLIENTAREAONLY; 
      props.fVisible = true; 
      props.fSourceClientAreaOnly = true; 


      Grid b = new Grid() { Height = ThumbnailSize+20, Width = ThumbnailSize+(WindowLeftOffset), Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#55D3E6EE")), Tag = UnSelectedTag ,VerticalAlignment=VerticalAlignment.Stretch,HorizontalAlignment=HorizontalAlignment.Stretch}; 
      b.MouseLeftButtonDown += B_MouseLeftButtonDown; 

      //Set the region where the live preview will be drawn ======> I want to add this region as a child for "b" grid 
      int left = (thumbnailIndex % MaxThumbnails) * (ThumbnailSize + ThumbnailSpacing)+ WindowLeftOffset; 
      int top = (int)(thumbnailIndex/MaxThumbnails) * (ThumbnailSize + ThumbnailSpacing) + WindowTopOffset; 
      int right = left + ThumbnailSize; 
      int bottom = top + ThumbnailSize; 

      props.rcDestination = new RECT(left, top, right, bottom); 


      Border bor = new Border() { BorderThickness = new Thickness(1), BorderBrush = (SolidColorBrush)(new BrushConverter().ConvertFrom("#550863A2")), CornerRadius= new CornerRadius(2) }; 
      bor.Child = b; 
      bor.SetValue(Canvas.LeftProperty, (double)(((drowingIndex % MaxThumbnails) * (ThumbnailSize + ThumbnailSpacing))+WindowLeftOffset - (WindowLeftOffset/2)-1)); 
      bor.SetValue(Canvas.TopProperty, Math.Floor((double)(drowingIndex/MaxThumbnails)) * (ThumbnailSize + ThumbnailSpacing)); 
      canvas.Children.Add(bor); 

      drowingIndex++; 
      previweStackPanels.Add(b); 

      //Center the live preview 
      if (size.x < size.y) 
      { 
       double ScaleFactor = ThumbnailSize/(double)size.y; 
       int scaledX = (int)(size.x * ScaleFactor); 
       int xOffset = (ThumbnailSize - scaledX)/2; 
       props.rcDestination.Left += xOffset; 
       props.rcDestination.Right -= xOffset; 
      } 
      if (size.y < size.x) 
      { 
       double ScaleFactor = ThumbnailSize/(double)size.x; 
       int scaledY = (int)(size.y * ScaleFactor); 
       int yOffset = (ThumbnailSize - scaledY)/2; 
       props.rcDestination.Top += yOffset; 
       props.rcDestination.Bottom -= yOffset; 
      } 

      DwmUpdateThumbnailProperties(thumbnail, ref props); 
     } 
    } 

回答

0

一個PanelGridStackPanelChildren集合,您可以添加UIElement S:

previweStackPanels.Children.Add(b); 

注意,你不能添加任何東西其他但UIElement對象到Panel雖然。

這意味着你不能添加RECTGrid因爲RECT不是UIElement。你可以添加一個System.Windows.Shapes.Rectangle

+0

no我想添加這個Rectangle'props.rcDestination = new RECT(left,top,right,bottom);'作爲一個孩子.. previweStackPanels只是所有StackPanel的列表 – Ebram

+0

As什麼的孩子? – mm8

+0

of'b' Grid ..所以我有縮略圖運行窗口,我想添加這個窗口的縮略圖作爲一個孩子或網格內容'b' – Ebram