The MASM Forum Archive 2004 to 2012
Welcome, Guest. Please login or register.
March 23, 2023, 07:46:26 AM

Login with username, password and session length
Search:     Advanced search
128553 Posts in 15254 Topics by 684 Members
Latest Member: mottt
* Home Help Search Login Register
+  The MASM Forum Archive 2004 to 2012
|-+  Project Support Forums
| |-+  MASM32
| | |-+  Memory dialogs revisited.
« previous next »
Pages: [1] 2 3 Print
Author Topic: Memory dialogs revisited.  (Read 23936 times)
hutch--
Administrator
Member
*****
Posts: 12013


Mnemonic Driven API Grinder


Memory dialogs revisited.
« on: March 15, 2011, 01:09:53 PM »

I have always liked the technique but have not done much with it for some years. I did the original set of macros back in the Win88 days when it was genuine PHUN to debug, they are much easier to work with in development from Win2000 upwards. I am tempted to upgrade this set of techniques as it provides a fully self contained dialog capacity that does not use resource dialogs at all and has all the advantages of a scalable yet precision interface where you don't go blind and mad trying to line up controls in a dialog editor.

I have attached a test piece that takes ANSI strings. There are a number of ways of handling UNICODE strings, resource strings being the older technique but they can be stored as byte sequences and passed by address to UNICODE API functions for languages like Chinese and Japanese.

I think there is a viable method to do much the same with image data, store it as byte data, load it into a temporary file, read that file with LoadImage() then delete the file. The tests so far have been good and easily fast enough so if it looks like it viable, it would be easy enough to make a tool that loads the image file as DB data so the function can be called and return either a valid ICO or BMP handle from that data.

* dlgtest.zip (12.1 KB - downloaded 387 times.)
Logged

Regards,



Download site for MASM32
http://www.masm32.com
donkey
Member
*****
Posts: 3789


ASS-embler


Re: Memory dialogs revisited.
« Reply #1 on: March 15, 2011, 01:46:25 PM »

There is no need to create a temporary file with image data, you can convert an area of memory directly to a DIB. You simply have to create an empty DIB then use SetDIBits to read the transfer into the image. Much faster using the disk drive. If you have stored the full image the header can give you all the information you need to rebuild the DIB (color table etc...) even conversion to/from different palettes is fairly simple. Pretty sure I wrote a function to do it (in MASM it was that long ago) I'll have a peek at the old archives.
Logged

"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable
hutch--
Administrator
Member
*****
Posts: 12013


Mnemonic Driven API Grinder


Re: Memory dialogs revisited.
« Reply #2 on: March 15, 2011, 02:07:00 PM »

Thanks Edgar, I would certainly be interested to see it. I wasted the time playing with CreateBitmapIndirect() but could not get it to work in a static control where the handle returned by CreateImage() works perfectly. If you create a temporary file using that flag the OS retains it in memory unless it is very large so the read back time with CreateImage is reasonably fast, the same technique can be used with an icon as well.
Logged

Regards,



Download site for MASM32
http://www.masm32.com
donkey
Member
*****
Posts: 3789


ASS-embler


Re: Memory dialogs revisited.
« Reply #3 on: March 15, 2011, 02:10:08 PM »

Hi Steve,

The archives are on DVDs and contain thousands of compressed projects/tools etc.. it will take a day or two. It should do icons as well, at any rate bitmap to icon conversion is pretty simple, I have many examples of how to do that.
Logged

"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable
donkey
Member
*****
Posts: 3789


ASS-embler


Re: Memory dialogs revisited.
« Reply #4 on: March 15, 2011, 02:47:57 PM »

I found one for icons held in data, its in GoAsm syntax though, translated on the fly (sorry about the square brackets I know you hate them  Tongue) :

Code:
CreateIconFromData PROC pIconData:DWORD, iIcon:DWORD
LOCAL sz[2] :DWORD

/*
CreateIconFromData
Creates an icon from icon data stored in the DATA or CONST SECTION
(The icon data is an ICO file stored directly in the executable)

Parameters
pIconData = Pointer to the ico file data
iIcon = zero based index of the icon to load

If successful will return an icon handle, this handle must be freed
using DestroyIcon when it is no longer needed. The size of the icon
is returned in EDX, the high order word contains the width and the
low order word the height.

Returns 0 if there is an error.
If the index is greater than the number of icons in the file EDX will
be set to the number of icons available otherwise EDX is 0. To find
the number of available icons set the index to -1
*/

xor eax, eax
mov edx, [pIconData]
or edx, edx
jz ERRORCATCH

movzx eax, WORD PTR [edx+4]
cmp eax, [iIcon]
ja @F
ERRORCATCH:
push eax
invoke SetLastError, ERROR_RESOURCE_NAME_NOT_FOUND
pop edx
xor eax, eax
ret
@@:

mov eax, [iIcon]
shl eax, 4
add edx, eax
add edx, 6

movzx eax, BYTE PTR [edx]
mov [sz], eax
movzx eax, BYTE PTR [edx+1]
mov [sz+4], eax

mov eax, [edx+12]
add eax, [pIconData]
mov edx, [edx+8]

invoke CreateIconFromResourceEx, eax, edx, 1, 030000h, [sz], [sz+4], 0

mov edx,[sz]
shl edx,16
mov dx,[sz+4]

RET
CreateIconFromData endp
Logged

"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable
Vortex
Raider of the lost code
Member
*****
Gender: Male
Posts: 3460



Re: Memory dialogs revisited.
« Reply #5 on: March 15, 2011, 08:32:30 PM »

Hi Hutch,

Here is a quick example to convert an image in memory to bitmap :

Code:
; On success, the function returns the handle to the bitmap otherwise it returns 0

.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\user32.inc
include     \masm32\include\gdi32.inc

.code

CreateBmpFromMem PROC hWnd:DWORD,pBmp:DWORD

LOCAL hDC:DWORD
LOCAL hBmp:DWORD

    invoke  GetDC,hWnd
    test    eax,eax
    jz      @f
    mov     hDC,eax
    mov     edx,pBmp
    lea     ecx,[edx + SIZEOF BITMAPFILEHEADER]  ; start of the BITMAPINFOHEADER header
    mov     eax,BITMAPFILEHEADER.bfOffBits[edx]
    add     edx,eax
    invoke  CreateDIBitmap,hDC,ecx,CBM_INIT,edx,ecx,DIB_RGB_COLORS
    mov     hBmp,eax
    invoke  ReleaseDC,hWnd,hDC
    mov     eax,hBmp
@@:
    ret

CreateBmpFromMem ENDP

END

* CreateBmpFromMem.zip (12.73 KB - downloaded 383 times.)
Logged

hutch--
Administrator
Member
*****
Posts: 12013


Mnemonic Driven API Grinder


Re: Memory dialogs revisited.
« Reply #6 on: March 15, 2011, 10:56:39 PM »

Edgar, Erol,

Thank you both, its been years since I have done this stuff and i am a long way out of practice.

LATER : I have tested Erol's algo and it works fine, problem is it displays the alpha channel in an RGB/a bitmap as a black background. Rough guess here is that the later structure BITMAPV5HEADER appears to have the alpha channel data but I am just in the process of decyphering it.  Tongue

LATER AGAIN : That problem solved, must have a manifest file to get the alpha channel to be transparent.
« Last Edit: March 16, 2011, 01:57:28 AM by hutch-- » Logged

Regards,



Download site for MASM32
http://www.masm32.com
hutch--
Administrator
Member
*****
Posts: 12013


Mnemonic Driven API Grinder


Re: Memory dialogs revisited.
« Reply #7 on: March 16, 2011, 05:34:44 AM »

This is the working example of what I was after with both Erol's algo and the disk based on the uses LoadImage(). The direct memory load should be a lot faster but the LoadImage() version is a lot more flexible with scaling so I see a use for both.

The next trick I need is a similar algo to load an Icon from memory. LoadImage() does this fine but it may be a speed advantage i some contexts to have a direct memory load version.

* dialog_image.zip (30.28 KB - downloaded 377 times.)
Logged

Regards,



Download site for MASM32
http://www.masm32.com
Vortex
Raider of the lost code
Member
*****
Gender: Male
Posts: 3460



Re: Memory dialogs revisited.
« Reply #8 on: March 16, 2011, 09:19:07 PM »

Hi Hutch,

With thanks to xandaz's code, I coded an application displaying an icon from memory :

Code:
    .IF uMsg==WM_INITDIALOG

        invoke  CreateBmpFromMem,hWnd,ADDR pIcon
        mov     hBitmap,eax

; Code portion from xandaz
                     
        mov     ii.fIcon,TRUE
        push    hBitmap
        pop     ii.hbmMask
        push    hBitmap
        pop     ii.hbmColor
        invoke  CreateIconIndirect,ADDR ii
        mov     hIcon,eax

* IconFromBin.zip (3.85 KB - downloaded 378 times.)
Logged

hutch--
Administrator
Member
*****
Posts: 12013


Mnemonic Driven API Grinder


Re: Memory dialogs revisited.
« Reply #9 on: March 17, 2011, 12:06:43 AM »

Gratsie,

I will digest this a bit later. I have added a couple of extra controls to play with, basically bitmap and icon versions that use the center image flag, just have to check that they behave in predictable ways. Getting both bitmaps and icons stored directly in code is very useful in that it allows complete object design for components that are fully self contained so they can just be linked and called without having to tweak the details for each use.
Logged

Regards,



Download site for MASM32
http://www.masm32.com
hutch--
Administrator
Member
*****
Posts: 12013


Mnemonic Driven API Grinder


Re: Memory dialogs revisited.
« Reply #10 on: March 17, 2011, 04:27:34 AM »

Here is a test piece with 2 new macros to handle centered images. The original macros for both icons and bitmaps set the image location to the top left X Y corordinates. This extra pair allow a static control to be placed in an area of a dialog interface and will center any image loaded into them. They will also clip the image if its bigger than the control.

IMPORTANT : The attached ZIP file has the later version of DIALOGS.INC that has the extra two macros in it, replace the old one with this one, everything else is the same.

* newmacrotest.zip (55.46 KB - downloaded 388 times.)
Logged

Regards,



Download site for MASM32
http://www.masm32.com
dedndave
Member
*****
Posts: 12523


Re: Memory dialogs revisited.
« Reply #11 on: March 17, 2011, 10:57:15 AM »

works fine here, Hutch

XP MCE2005 SP2

Logged
Vortex
Raider of the lost code
Member
*****
Gender: Male
Posts: 3460



Re: Memory dialogs revisited.
« Reply #12 on: March 17, 2011, 07:31:19 PM »

Hi Hutch,

I downloaded your example but it did not work on my XP Pro SP3. Could it be a manifest file issue?
Logged

dedndave
Member
*****
Posts: 12523


Re: Memory dialogs revisited.
« Reply #13 on: March 17, 2011, 08:17:47 PM »

hiyas Erol
did you replace the dialogs.inc file ?
i used the one from the other thread (in the masm32 subforum)
perhaps they are different   Tongue

did it assemble properly ?
Logged
Vortex
Raider of the lost code
Member
*****
Gender: Male
Posts: 3460



Re: Memory dialogs revisited.
« Reply #14 on: March 17, 2011, 09:59:28 PM »

Hi Dave,

I replaced the include file. No problem with building the project but running the executable does not display the GUI.
Logged

Pages: [1] 2 3 Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP The MASM Forum Archive 2004 to 2012 | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.
Valid XHTML 1.0! Valid CSS!