flat assembler
Message board for the users of flat assembler.

Index > Windows > [SOLVED Worked] Unworked example with RT_TOOLBAR resource

Author
Thread Post new topic Reply to topic
ProMiNick



Joined: 24 Mar 2012
Posts: 798
Location: Russian Federation, Sochi
ProMiNick 22 Jan 2018, 10:02
Toolbars created, but buttons not.

and patch kernel32.inc of course
Code:
; ...
Resource types

RT_CURSOR       = 1
RT_BITMAP       = 2
RT_ICON         = 3
RT_MENU         = 4
RT_DIALOG       = 5
RT_STRING       = 6
RT_FONTDIR      = 7
RT_FONT         = 8
RT_ACCELERATOR  = 9
RT_RCDATA       = 10
RT_MESSAGETABLE = 11
RT_GROUP_CURSOR = 12
RT_GROUP_ICON   = 14
RT_VERSION      = 16
RT_DLGINCLUDE   = 17
RT_PLUGPLAY     = 19
RT_VXD          = 20
RT_ANICURSOR    = 21
RT_ANIICON      = 22
RT_HTML         = 23
RT_MANIFEST     = 24
RT_DLGINIT      = 240 ; added optionaly
RT_TOOLBAR      = 241 ; added & needed
...    


program itself (RAW). I think I make mistake in loop operating on Toolbar resource:
Code:
format PE GUI 4.0
entry start

include 'win32a.inc'

IDR_ICON = 17
IDR_MENU = 37
IDR_TBAR = 800

IDM_NEW   = 101
IDM_EXIT  = 102
IDM_ABOUT = 901
IDB_TBAR  = 835

FLOODFILLBORDER  = 0
FLOODFILLSURFACE = 1

section '.text' code readable executable

  start:

        invoke  GetModuleHandle,0
        mov     [wc.hInstance],eax

        invoke  LoadIcon,eax,IDR_ICON
        mov     [wc.hIcon],eax
        invoke  LoadCursor,0,IDC_ARROW
        mov     [wc.hCursor],eax
        invoke  RegisterClass,wc
        test    eax,eax
        jz      error

        invoke  LoadMenu,[wc.hInstance],IDR_MENU
        invoke  CreateWindowEx,0,_class,_title,WS_VISIBLE+WS_OVERLAPPEDWINDOW,144,128,256,256,NULL,eax,[wc.hInstance],NULL
        test    eax,eax
        jz      error
  msg_loop:
        invoke  GetMessage,msg,NULL,0,0
        cmp     eax,1
        jb      end_loop
        jne     msg_loop
        invoke  TranslateMessage,msg
        invoke  DispatchMessage,msg
        jmp     msg_loop

  error:
        invoke  GetLastError
        push    eax
        push    eax ; skipped: HLL places here varg count, FormatMessage ignores this dword and start indexing with next param
        lea     eax,[lpBuffer]
        invoke  FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_ARGUMENT_ARRAY,NULL,1,LANG_NEUTRAL,eax,0,esp
        pop     eax
        pop     eax
        invoke  MessageBox,NULL,[lpBuffer],NULL,MB_ICONERROR+MB_OK
        invoke  LocalFree,[lpBuffer]
  end_loop:
        invoke  ExitProcess,[msg.wParam]

proc WindowProc hwnd,wmsg,wparam,lparam
        push    ebx esi edi
        mov     eax,[wmsg]
        cmp     eax,WM_CREATE
        je      .wmcreate
        cmp     eax,WM_SIZE
        je      .wmsize
        cmp     eax,WM_COMMAND
        je      .wmcommand
        cmp     eax,WM_DESTROY
        je      .wmdestroy
  .defwndproc:
        invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
        jmp     .finish
  .wmcreate:
        invoke  GetClientRect,[hwnd],client
        invoke  CreateWindowEx,WS_EX_CLIENTEDGE,_tbar,0,WS_VISIBLE or WS_CHILD or CCS_NODIVIDER or TBSTYLE_FLAT,0,0,[client.right],40,[hwnd],0,[wc.hInstance],NULL
        or      eax,eax
        jz      .failed
        mov     [tbarhwnd],eax
        invoke  LoadBitmap,[wc.hInstance],IDB_TBAR
        stdcall TransparentFromTopLeft,eax
        mov     [hTBbmp],eax
        stdcall LoadToolbar,[wc.hInstance],IDR_TBAR
        mov     [hTBres],eax
        invoke  SendMessage,eax,TB_BUTTONSTRUCTSIZE,sizeof.TBBUTTON,0
        mov     eax,[hTBres]
        invoke  SendMessage,[tbarhwnd],TB_SETBITMAPSIZE,0, [eax+2]
        mov     [tbab.hInst], 0
        mov     eax,[hTBbmp]
        mov     [tbab.nID],eax
        invoke  SendMessage,[tbarhwnd],TB_ADDBITMAP,12,tbab
        mov     eax,[hTBres]
        invoke  SendMessage,[tbarhwnd],TB_SETBUTTONSIZE,0,[eax+2]
        mov     [tbb.fsState],   TBSTATE_ENABLED
        mov     [tbb.dwData],    0
        mov     [tbb.iString],   0
        mov     esi,[hTBres]
        mov     edx,[esi+6]
        movzx   edx,dx ; count
        xor     ecx,ecx
  .loopTB:
        xor     eax,eax
        mov     ax,[esi+8+2*ecx]
        mov     [tbb.idCommand],   eax
        test    eax,eax
        jz      .blankTB
        mov     [tbb.iBitmap], ecx
        mov     [tbb.fsStyle], TBSTYLE_BUTTON
        inc     ecx
        jmp     .commonTB
  .blankTB:
        mov     [tbb.iBitmap], eax
        mov     [tbb.fsStyle], TBSTYLE_SEP
        inc     esi
        inc     esi
        dec     edx
  .commonTB:
        pushad
        invoke  SendMessage,[tbarhwnd],TB_ADDBUTTONS,1,tbb
        popad
        cmp     ecx,edx
        jb      .loopTB
        xor     eax,eax
        jmp     .finish
      .failed:
        or      eax,-1
        jmp     .finish
  .wmsize:
        invoke SendMessage,[tbarhwnd],TB_AUTOSIZE,0,0
        xor     eax,eax
        jmp     .finish
  .wmcommand:
        mov     eax,[wparam]
        and     eax,0FFFFh
        cmp     eax,IDM_NEW
        je      .new
        cmp     eax,IDM_ABOUT
        je      .about
        cmp     eax,IDM_EXIT
        je      .wmdestroy
        jmp     .defwndproc
      .new:
        jmp     .finish
      .about:
        invoke  MessageBox,[hwnd],_about_text,_about_title,MB_OK
        jmp     .finish
  .wmdestroy:
        invoke  PostQuitMessage,0
        xor     eax,eax
  .finish:
        pop     edi esi ebx
        ret
endp

proc LoadToolbar hInstance,tbID
        invoke  FindResource,[hInstance],[tbID],RT_TOOLBAR
        or      eax,eax
        jz      error
        invoke  LoadResource,[hInstance],eax
        or      eax,eax
        jz      error
        invoke  LockResource,eax
        or      eax,eax
        jz      error
        ret
endp

proc TransparentFromTopLeft,hBitmap
        locals
                mDC dd ?
                hBrush dd ?
                hOldBrush dd ?

        endl
        invoke  CreateCompatibleDC,NULL
        mov     [mDC],eax
        invoke  SelectObject,eax,[hBitmap]
        invoke  GetSysColor,COLOR_BTNFACE
        invoke  CreateSolidBrush,eax
        mov     [hBrush],eax
        invoke  SelectObject,[mDC],eax
        mov     [hOldBrush],eax
        invoke  GetPixel,[mDC],1,1
        invoke  ExtFloodFill,[mDC],1,1,eax,FLOODFILLSURFACE
        invoke  SelectObject,[mDC],[hOldBrush]
        invoke  DeleteObject,[hBrush]
        invoke  SelectObject,[mDC],[hBitmap]
        mov     [hBitmap],eax
        invoke  DeleteDC,[mDC]
        mov     eax,[hBitmap]
        ret
endp

section '.data' data readable writeable
  Fmt TCHAR 'HEX: %08X',0
  _title TCHAR 'MiniPad',0
  _about_title TCHAR 'About MiniPad',0
  _about_text TCHAR 'This is Win32 example program created with flat assembler.',0
  _error TCHAR 'Startup failed.',0

  _class TCHAR 'MINIPAD32',0
  _edit TCHAR 'EDIT',0
  _tbar TCHAR 'ToolbarWindow32',0

  wc WNDCLASS 0,WindowProc,0,0,NULL,NULL,NULL,COLOR_BTNFACE+1,NULL,_class
  tbab     TBADDBITMAP
  tbb      TBBUTTON
  tbarhwnd dd ?
  hTBres   dd ?
  hTBbmp   dd ?
  lpBuffer dd ?
  hBrush   dd ?
  msg MSG
  client RECT
  Buffer   db 255 dup (?)

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL',\
          gdi,'GDI32.DLL'

  import kernel,\
         GetLastError,'GetLastError',\
         FormatMessage,'FormatMessageA',\
         GetModuleHandle,'GetModuleHandleA',\
         FindResource,'FindResourceA',\
         LockResource,'LockResource',\
         LoadResource,'LoadResource',\
         ExitProcess,'ExitProcess',\
         LocalFree,'LocalFree'

  import user,\
         RegisterClass,'RegisterClassA',\
         CreateWindowEx,'CreateWindowExA',\
         DefWindowProc,'DefWindowProcA',\
         SetWindowLong,'SetWindowLongA',\
         RedrawWindow,'RedrawWindow',\
         GetMessage,'GetMessageA',\
         TranslateMessage,'TranslateMessage',\
         DispatchMessage,'DispatchMessageA',\
         SendMessage,'SendMessageA',\
         LoadCursor,'LoadCursorA',\
         LoadIcon,'LoadIconA',\
         LoadMenu,'LoadMenuA',\
         LoadBitmap,'LoadBitmapA',\
         GetClientRect,'GetClientRect',\
         MoveWindow,'MoveWindow',\
         SetFocus,'SetFocus',\
         MessageBox,'MessageBoxA',\
         wsprintf,'wsprintfA',\
         GetSysColor,'GetSysColor',\
         PostQuitMessage,'PostQuitMessage'

  import gdi,\
         SetBmpColor,'SetBmpColor',\
         CreateCompatibleDC,'CreateCompatibleDC',\
         DeleteDC,'DeleteDC',\
         SelectObject,'SelectObject',\
         DeleteObject,'DeleteObject',\
         GetPixel,'GetPixel',\
         ExtFloodFill,'ExtFloodFill',\
         CreateSolidBrush,'CreateSolidBrush'

section '.rsrc' resource data readable

  ; resource directory

  directory RT_MENU,menus,\
            RT_ICON,icons,\
            RT_BITMAP,bitmaps,\
            RT_GROUP_ICON,group_icons,\
            RT_VERSION,versions,\
            RT_TOOLBAR,toolbars,\
            RT_MESSAGETABLE,errors

  ; resource subdirectories

  resource menus,\
           IDR_MENU,LANG_ENGLISH+SUBLANG_DEFAULT,main_menu

  resource icons,\
           1,LANG_NEUTRAL,icon_data

  resource group_icons,\
           IDR_ICON,LANG_NEUTRAL,main_icon

  resource versions,\
           1,LANG_NEUTRAL,version

  resource bitmaps,\
           IDB_TBAR,LANG_NEUTRAL,tb_bitmap

  resource toolbars,\
           IDR_TBAR,LANG_ENGLISH+SUBLANG_DEFAULT,main_tbar

  resource errors,\
           1,LANG_ENGLISH+SUBLANG_DEFAULT,error_table

  menu ex,main_menu ; to run in oficial version remove "ex,"
       menuitem '&File',0,MFR_POPUP
                menuitem '&New',IDM_NEW
                menuseparator
                menuitem 'E&xit',IDM_EXIT,MFR_END
       menuitem '&Help',0,MFR_POPUP + MFR_END
                menuitem '&About...',IDM_ABOUT,MFR_END
  endmenu                  ; to run in oficial version remove all this line

  icon main_icon,icon_data,'minipad.ico'

  bitmap tb_bitmap,'toolbar.bmp'

  versioninfo version,VOS__WINDOWS32,VFT_APP,VFT2_UNKNOWN,LANG_ENGLISH+SUBLANG_DEFAULT,0,\
              'FileDescription','MiniPad - example program',\
              'LegalCopyright','No rights reserved.',\
              'FileVersion','1.0',\
              'ProductVersion','1.0',\
              'OriginalFilename','MINIPAD.EXE'

macro messagetable label,[strings*]
{ 
 common 
   local data,size 
   label dd RVA data,size,0,0 
   data = $ 
   strt = $ 

   dd 1 ; Only use 1 block 

   tbl_size equ size = $ - data 

   lowid = 1
   highid = 1

 ; Find the highest id 
 ;forward
   ;highid = highid + 1

 ; MESSAGE_RESOURCE_BLOCK 
 common 
   dd lowid
   dd highid
   dd $ - strt + 4         ; Offset to block 

 ; Each MESSAGE_RESOURCE_ENTRY 
 forward 
   local stringLen 
   virtual at 0  
      db strings 
      stringLen=$ 
   end virtual 

   dw stringLen+7   ; 7 because dw + dw + CR,LF,0 
   dw 0      ; ANSI 
   db strings,13,10,0

 common 
   tbl_size 
   align 4 
}

  messagetable error_table,<'%1!*i!'>
  ;error_table dd RVA erdata,ersize,0,0
  ;erdata dd 1

  ;dd 1,1
  ;dd $+4

  ;dw 6+7,0
  ;db '%1!*u!',13,10,0
  ;ersize = $ - erdata
  ;align 4


macro toolbar label,bmpID,width,height,[item]
 { common
     local data,size,items
     label dd RVA data,size,0,0
     data dw bmpID+0,width,height,items
     tb_counter = 0
   forward
     dw item+0
     tb_counter = tb_counter + 1
   common
     items = tb_counter
   size = $ - data
 }

  toolbar main_tbar,IDB_TBAR,16,16,IDM_NEW,,IDM_ABOUT,,IDM_EXIT    

_________________
I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.


Last edited by ProMiNick on 23 Jan 2018, 14:27; edited 1 time in total
Post 22 Jan 2018, 10:02
View user's profile Send private message Send e-mail Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 798
Location: Russian Federation, Sochi
ProMiNick 23 Jan 2018, 14:21
I make it work.

my mistake was
Code:
.wmcreate: 
        invoke  GetClientRect,[hwnd],client 
;here:  invoke  CreateWindowEx,WS_EX_CLIENTEDGE,_tbar,0,WS_VISIBLE or WS_CHILD or CCS_NODIVIDER or TBSTYLE_FLAT,0,0,[client.right],40,[hwnd],0,[wc.hInstance],NULL     


correction is
Code:
invoke  CreateToolbarEx,[hwnd],WS_VISIBLE or WS_CHILD or CCS_NODIVIDER or TBSTYLE_FLAT,0,0,0,0,0,0,16,16,16,16    


And, yes because of that I have to add to libraries 'COMCTL32.DLL' and it`s import 'CreateToolbarEx'

as my prey I used minipad example...

sources with resources attached


Description:
Filesize: 8.74 KB
Viewed: 2526 Time(s)

Безымянный.jpg


Description:
Download
Filename: TOOLBAR.zip
Filesize: 3.99 KB
Downloaded: 246 Time(s)


_________________
I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.
Post 23 Jan 2018, 14:21
View user's profile Send private message Send e-mail Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 798
Location: Russian Federation, Sochi
ProMiNick 23 Jan 2018, 14:27
Tomasz, still toolbar resource is supported with macroses of fasm (easily ported to fasmg) can you fix official pacckage includes? Add to kernel32.inc & in kernel64.inc a row "RT_TOOLBAR = 241" and add to resource macro mine macro? (or your own macro if you can improve it and it needed to improve)
Post 23 Jan 2018, 14:27
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.