Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Transparent ListBox: Help Porting C++ to AHK


  • Please log in to reply
16 replies to this topic
Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Hi! I am working on translating some cool C++ code to AHK code, but I am having difficulty.

 

I am following the tutorial posted here. I do not ask anyone to read through it completely, but simply take a look at the c++ message handler for WM_OnEraseBkgnd, compare it to my ahk OnEraseBkgnd, and see if they can identify where I am going wrong.

 

Actually, I am almost certain the problem is having to do with the ListBox's bitmap, but I am not sure what to do. I think this code/concept is pretty easy to anyone who is well versed in gdip.

 

Thanks in advance

 

Spoiler

Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

I think you have confused GDI for GDI+.  It's an easy mistake to make since the author of the GDI+ lib for AutoHotkey included GDI functions.

 

SelectObject is a GDI function, and requires that you pass a GDI object, such as a GDI bitmap.  Gdip_BitmapFromHWND() returns a GDI+ Bitmap, which is different to a GDI bitmap.  CreateDIBSection() returns a GDI bitmap.  You've created hbm but never used it; did you intend to pass it instead of calling Gdip_BitmapFromHWND()?

 

You should call ReleaseDC() on the return value of GetDC(g_hLB).  Note that Gdip.ahk (my copy, at least) contains a wrapper for this function, but the parameters are swapped.

 

Window and device context handles are pointer-sized, so you should use ptr rather than uint.  However, it doesn't matter in practice.  It would matter for structs, or if you were passing a pointer to a HWND.

 

If you want to keep the bitmap (referred by hbm), you should store it in a static or global variable.  Otherwise, you should delete it before the function returns.  The same goes for memdc.



guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
your SelectObject call is wrong like Lex said, you need to use hbm as the 2nd param

the tutorial is using the ERASEBKGND message of the listbox, while your OnMessage captures all messages for the entire script, including your main gui and all. i'm not so sure thats what you want

anyway
from the site:
 

First thing we have to do is copy the parent window's image before the first time the ListBox is drawn, this will give us the background for the listbox. We can do this in the WM_ERASEBKGND message handler. The first time this message is received the ListBox has not yet been drawn, so it is a safe place to take a snapshot of the parent window.


if all he wants you to do is bitblt the image into a memory DC, you can simply do this before you even Gui,Add the listbox, you dont need to capture the ERASEBKGND message to do it

hrm actually nevermind, i guess hes doing all those client to screen coordinates to just copy the cropped area of the image that fits the rectangle of the listbox

Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Thank you both very much! I had no idea about the differences between GDI and GDI+.

 

Here's a revised version. In this version, BitBit fails and returns 1. GetLastError is returning 0 though...

 

Spoiler

Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011

Well, partially working. You have to subclass the control, and there is some work remaining:

 

Spoiler

Prefer ahkscript.org for the time being.


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
In this version, BitBit fails and returns 1.

 

Really?

 

If the function succeeds, the return value is nonzero.


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Really?

 

Oh, dear. That is exactly the same place I read about BitBlt, but somehow I read that as, "zero" instead of "nonzero."


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Oh, that is cool! Thank you! I am guessing (hoping) this is enough to help me get the rest figured out.

 

Well, partially working. You have to subclass the control, and there is some work remaining:

 

Spoiler

Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

I'm getting hung up on CPaintDC. How can I handle class objects?


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Progress.

 

I have taken my shot porting most of the important code from the tutorial. There are a lot of errors in my script below, but I'm not sure what the right way to do it is. Specifically, passing around old bmps and membmps before BitBlt. I did, at one point, have a script that was successfully drawing colored text onto the LV. Also, for OnPaint, the tutorial uses a CPaintDC class object. I have no idea how to use class objects in AHK or find a struct-equivalent.

 

Any help is much appreciated!

 

Spoiler

Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Ah! I believe I found a way around using CPaintDC. See this MSND CWnd::Begin/EndPaint.

 

Edit: Updated link from CWnd::BeginPaint to BeginPaint function


Edited by Verdlin, 20 August 2013 - 02:30 PM.

Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Another update: in this version, I believe I have properly worked around CPaintDC; however, I cannot figure out why hOldBmp is blank (from a SelectObject call); for all this script I've added, there is actually no visible change to the ListBox. I am hoping this is related to my problem with hOldBmp

 

Spoiler

Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Another update. hOldBmp was failing because I was trying to select the bitmap into a non-memory DC. Now that appears to be fixed, and I am successfully drawing text onto the ListBox.

Still, I cannot seem to copy the picture's bmp over when I draw the text, though.

 

I realize I'm doing some pretty messy, and in some cases, unnecessary stuff. I'm just trying to get a working example and then clean up the script from there.

 

Spoiler

 

@

 

Only if you don't mind, would you mind taking a look at this revised version?


Edited by Verdlin, 21 August 2013 - 06:04 PM.

Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Bumping for visibility. I admit, I haven't spent much time on this since the last revision; however, I am hoping this will catch someone's interest who is willing to take a stab at it ;)


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011

Well, I've spent some time again and it seems that I made some progress. The following seems to work pretty stable, but there are some restrictions:

  1. The ListBox is very static. You must not change the position, the size, or the contents after creating the instance.
     
  2. I had to suppress some messages, so the gLabel is rather useless.
     
  3. You cannot move using Down or Up keys so far.

There might be other issues I didn't notice as yet. I will try to make things more comfortable and publish the class in 'Scripts' as soon as it seems to be reliable.

 

Spoiler

 

 


Prefer ahkscript.org for the time being.