AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

about Gdip_BitmapFromHWND and BitBlt

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
hughman



Joined: 11 Feb 2007
Posts: 166

PostPosted: Sat Sep 05, 2009 6:33 am    Post subject: about Gdip_BitmapFromHWND and BitBlt Reply with quote

I have tried out three methods to copy part of window to a dc.
The first method is Gdip_CreateBitmapFromFile, It is ok.

The second is via Gdip_BitmapFromHWND and PrintWindows. If the param is hwnd of a window, it works. While it's a control, only a solid rect filled with color black is showed. what's the reason?

The third is via BitBlt. In this case, it seems that all the black pixels become transparent and only white pixels remain. how to solve this problem?

Code:

#Include Gdip.ahk

GUI_INDEX_DC := 2
OnMessage(0x200, "WM_MOUSEMOVE")

Gui, +LastFound +AlwaysOnTop
$hGUI1 := WinExist()
Gui, Add, Pic, x20 y20 hwnd$hBTN, test.jpg
Gui, Show, AutoSize
Return

GuiClose:
ExitApp

F1::
   CreateGUI_DC1("test.jpg")
Return

F2::
   CreateGUI_DC2()
Return

F3::
   CreateGUI_DC3()
Return

Esc::
   Gui, %GUI_INDEX_DC%: Destroy
Return

WM_MOUSEMOVE(wParam, lParam, uMsg, hWnd)
{
   local X, Y
   
   CoordMode, Mouse, Screen
   MouseGetPos, X, Y
   X -= nWidth / 2, Y -= nHeight / 2
   DllCall("UpdateLayeredWindow"
      , "Uint", $hGui_DC, "Uint", 0
      , "int64P", X|Y<<32
      , "Uint", 0, "Uint", 0, "Uint", 0, "Uint", 0, "Uint", 0, "Uint", 0)
}


CreateGUI_DC1(File)
{
   global
   
   local hDC_Scr := CreateCompatibleDC(0)
   local pToken := Gdip_Startup()
   local pBitmap := Gdip_CreateBitmapFromFile(File)
   nWidth := Gdip_GetImageWidth(pBitmap)
   nHeight := Gdip_GetImageHeight(pBitmap)
   local hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
   Gdip_DisposeImage(pBitmap)
   Gdip_Shutdown(pToken)
   local oBitmap :=SelectObject(hDC_Scr, hBitmap)
   

   Gui, %GUI_INDEX_DC%: +LastFound -Caption +E0x80000 +ToolWindow +AlwaysOnTop
   $hGui_DC := WinExist()
   UpdateLayeredWindow($hGui_DC, hDC_Scr, 0, 0, nWidth, nHeight, 255)
   Gui, %GUI_INDEX_DC%: Show, W%nWidth% H%nHeight%
   
   SelectObject(hDC_Scr, oBitmap)
   DeleteObject(hBitmap)
   DeleteDC(hDC_Scr)
}

CreateGUI_DC2()
{
   global

   ControlGetPos, , , nWidth, nHeight, , ahk_id %$hBTN%
   
   local hDC_Scr :=CreateCompatibleDC(0)
   local pToken := Gdip_Startup()
   local pBitmap :=    Gdip_BitmapFromHWND($hBTN)
   local hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
   Gdip_DisposeImage(pBitmap)
   Gdip_Shutdown(pToken)
   local oBitmap :=SelectObject(hDC_Scr, hBitmap)
   

   Gui, %GUI_INDEX_DC%: +LastFound -Caption +E0x80000 +ToolWindow +AlwaysOnTop
   $hGui_DC := WinExist()
   UpdateLayeredWindow($hGui_DC, hDC_Scr, 0, 0, nWidth, nHeight, 255)
   Gui, %GUI_INDEX_DC%: Show, W%nWidth% H%nHeight%
   
   SelectObject(hDC_Scr, oBitmap)
   DeleteObject(hBitmap)
   DeleteDC(hDC_Scr)
}

CreateGUI_DC3()
{
   global
   
   ControlGetPos, , , nWidth, nHeight, , ahk_id %$hBTN%
   
   Gui, %GUI_INDEX_DC%: +LastFound -Caption +E0x80000 +ToolWindow +AlwaysOnTop
   $hGui_DC := WinExist()
   
   local pToken := Gdip_Startup()
   local hDC_Scr :=CreateCompatibleDC(0)
   local hBitmap :=CreateDIBSection(nWidth, nHeight)
   local oBitmap :=SelectObject(hDC_Scr, hBitmap)

   local sDC :=GetDC($hBTN)
   BitBlt(hDC_Scr, 0, 0, nWidth, nHeight, sDC, 0, 0, 0x00CC0020)   ; SRCCOPY
   
   UpdateLayeredWindow($hGui_DC, hDC_Scr, 0, 0, nWidth, nHeight, 255)
   Gui, %GUI_INDEX_DC%: Show, W%nWidth% H%nHeight%
   
   Gdip_Shutdown(pToken)
   SelectObject(hDC_Scr, oBitmap)
   DeleteObject(hBitmap)
   DeleteDC(hDC_Scr)
   DeleteDC(sDC)
}
Back to top
View user's profile Send private message
hughman



Joined: 11 Feb 2007
Posts: 166

PostPosted: Tue Sep 08, 2009 2:14 am    Post subject: Reply with quote

who can help me?
Back to top
View user's profile Send private message
holomind



Joined: 11 Mar 2006
Posts: 341
Location: Munich, Germany

PostPosted: Sun Sep 13, 2009 1:31 pm    Post subject: Reply with quote

i guess bitblt and printwindow only work with "windows" and not with controls. (maybe also with controls of type window, you can look with the inspector which type your element is). when you try to read from a control where nothing exists i guess its fall back to pointer 0 which defaults to the desktop. or you just get random values which look black. just guessing but perhaps it brings a new idea to you.

H WND stands for Handle-of-Window. so i guess it will not work with controls which are elements of windows and not of type window itself.

when you work with Bitblt then you always grab only the topmost layer, which you can see on you desktop/screen. if the control you try to grab is behind something else it will either give you the window in front.
When you use Printwindow then the redraw routine is called and the window is acutally repainted with all its subelements, and therefore can be "grabbed" even when behind other windows. (if its minimized its then not exitisting and will be rendered black even with printwindow).

maybe you can get the position of your control and bitblit only this part of your "window" ?
Back to top
View user's profile Send private message Visit poster's website
hughman



Joined: 11 Feb 2007
Posts: 166

PostPosted: Mon Sep 14, 2009 3:19 pm    Post subject: Reply with quote

Yes, your guessing is right. I had a expriment and found that ListView, TreeView can be grabbed via PrintWindow, while Edit, Button, Static can't.

Now I am puzzled that why the bitmap getting from BitBlt becomes transparent and can't be displayed over the white background. So is PrintWindow.
Back to top
View user's profile Send private message
holomind



Joined: 11 Mar 2006
Posts: 341
Location: Munich, Germany

PostPosted: Mon Sep 14, 2009 5:04 pm    Post subject: Reply with quote

If you only get transparent pixels by copying it can be the effect of using layered-window or translucent windows. (even when opacity is 100% or 255) then the whole window will be transparent while copying with bitblt.
it seems UpdateLayeredWindow is causing the window to be translucent even with alpha=255 and is somehow rended in hardware /overlay (as in "layered").
This way it will not be "part" of the desktop and cannot be grabbed with bitblt. (and maybe even printwindow). then the region will be shown black. as this is the defaultvalue for transparency in this case. (maybe if you change the transparency color of the window you will get a different color instead of black. ). the other effekt may be that the transparency is in some sort of mask/filter and all white pixels are rendered transparent. there are different modes/masks/filters which can be applied in bitblit/printwindow.

See also:
http://msdn.microsoft.com/en-us/library/ms633540%28VS.85%29.aspx
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group