Page 1 of 1

Cant get Win32 API

Posted: Tue Mar 27, 2012 6:55 am
by 649psoft2
Hi. I just got a copy of Pure Basic. The demo does not include the API but it was supposed to work "just like a regular basic command" after purchase. I am new to Pure Basic but have a lot of API experience. Can someone tell me why this code won't run and I have tried the usual suspect variations.

Import "User32.lib"
If OpenWindow(0, 0, 0, 300, 300, "Resize me !", PB_Window_SystemMenu | PB_Window_ScreenCentered | PB_Window_SizeGadget)

hMainMenu.i = CreateMenu()
hFileMenu.i = CreatePopupMenu()
ret1.b = AppendMenu(hMainMenu, MF_POPUP, hFileMenu, "File")
ret2.b = AppendMenu(hFileMenu. MF_STRING, 10, "New")


Repeat
Event = WaitWindowEvent()
Until Event = PB_Event_CloseWindow
EndIf
EndImport

There should be a menu heading with "File" and a one item dropdown "New"

Re: Cant get Win32 API

Posted: Tue Mar 27, 2012 7:51 am
by Fangbeast
You are missing the '#' in front of your constants to actually make them constants and API names need the '_' after them.

Some of the constants may not already be defined in the compiler as well.

I.e: #PB_Window_SystemMenu

Re: Cant get Win32 API

Posted: Tue Mar 27, 2012 12:05 pm
by gnozal
649psoft2 wrote:Hi. I just got a copy of Pure Basic. The demo does not include the API but it was supposed to work "just like a regular basic command" after purchase. I am new to Pure Basic but have a lot of API experience. Can someone tell me why this code won't run and I have tried the usual suspect variations.
Hi 649psoft2,

like Fangbeast wrote :
- constants start with #
- you may use API functions directly, simply add an underscore to the function name (no need to import / declare)
And you forgot SetMenu_()

This should work :

Code: Select all

hWnd = OpenWindow(0, 0, 0, 300, 300, "Resize me !", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
If hWnd
  hMainMenu = CreateMenu_()
  hFileMenu = CreatePopupMenu_()
  AppendMenu_(hMainMenu, #MF_POPUP, hFileMenu, "File")
  AppendMenu_(hFileMenu, #MF_STRING, 10, "New")
  SetMenu_(hWnd, hMainMenu)
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf

Re: Cant get Win32 API

Posted: Tue Mar 27, 2012 12:07 pm
by luis
The problems:

1) Import is not used that way (automagically). You need to actually import using the appropriate declarations. See the HELP.

2) Your constants are variables. Use the # prefix.

3) I believe you need to associate the API-style menu to the PB window, so you need to call the SetMenu API.


I don't have the demo version installed, so I cannot check with it, but this should work.

Code: Select all


#MF_POPUP = 16
#MF_STRING = 0

Import "User32.lib"
    CreateMenu_() As "_CreateMenu"
    AppendMenu_(hMenu, uFlags, uIDNewItem, *lpNewItem) As "_AppendMenuA@16"
    SetMenu_(hWnd, hMenu) As "_SetMenu@8"
EndImport

If OpenWindow(0, 0, 0, 300, 300, "Resize me !", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)

    hMainMenu = CreateMenu_()
    hFileMenu = CreateMenu_()
    ret1 = AppendMenu_(hMainMenu, #MF_POPUP, hFileMenu, @"File")
    ret2 = AppendMenu_(hFileMenu, #MF_STRING, 10, @"New")

    SetMenu_(WindowID(0), hMainMenu)    

    Repeat
        Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
EndIf
4) You should use EnableExplicit. Doing so you would have at least trapped the problem with the constants not being constants.

5) No need to specify .i or use .b unless really required. Go with the .i by default.
See also: http://www.purebasic.fr/blog/?p=42

Re: Cant get Win32 API

Posted: Tue Mar 27, 2012 12:22 pm
by Fangbeast
This is good stuff, I learned a few things today as well:):)

Re: Cant get Win32 API

Posted: Tue Mar 27, 2012 12:50 pm
by luis
Oh, what the hell :D

You BOUGHT PB right ? You talked about the demo version and I misunderstood you still had that one.

So you can discard my example where I painfully tried to make all work for the demo version like an idiot. :cry:

BTW probably it's not enough, you need to define the Win32 constants also. I just added them to that post (maybe can be useful to someone else still trying the demo).

*SIGH*

Re: Cant get Win32 API

Posted: Wed Mar 28, 2012 9:24 am
by 649psoft2
Thanks a lot for your responses they have been a great help.

Yes the demo won't work for the API I have purchased the full version.

I didn't expect such a good reply so there is a little syntax variation to learn.

I will have to get use to the explicit declarations.

:D

Re: Cant get Win32 API

Posted: Wed Mar 28, 2012 3:21 pm
by Zach
It will help you to learn the language better if you use the "Enable Explicit" command at the top of your source file.
This will help correct you on various syntax issues, as well as refuse to compile on warnings that may interest you.

Re: Cant get Win32 API

Posted: Fri Mar 30, 2012 4:12 pm
by blueznl