AutoHotkey Community

It is currently May 26th, 2012, 10:32 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: September 5th, 2009, 7:33 am 
Offline

Joined: February 11th, 2007, 4:10 pm
Posts: 185
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)
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2009, 3:14 am 
Offline

Joined: February 11th, 2007, 4:10 pm
Posts: 185
who can help me?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 13th, 2009, 2:31 pm 
Offline

Joined: March 11th, 2006, 12:44 pm
Posts: 341
Location: Munich, Germany
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" ?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 14th, 2009, 4:19 pm 
Offline

Joined: February 11th, 2007, 4:10 pm
Posts: 185
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 14th, 2009, 6:04 pm 
Offline

Joined: March 11th, 2006, 12:44 pm
Posts: 341
Location: Munich, Germany
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 ... 85%29.aspx


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Apollo, JSLover, Leef_me, Miguel, rbrtryn and 66 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group