Cant get Win32 API

Everything else that doesn't fall into one of the other PB categories.
649psoft2
New User
New User
Posts: 2
Joined: Tue Mar 27, 2012 6:19 am

Cant get Win32 API

Post 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"
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Cant get Win32 API

Post 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
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: Cant get Win32 API

Post 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
Last edited by gnozal on Tue Mar 27, 2012 12:07 pm, edited 1 time in total.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Cant get Win32 API

Post 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
Last edited by luis on Tue Mar 27, 2012 12:55 pm, edited 1 time in total.
"Have you tried turning it off and on again ?"
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Cant get Win32 API

Post by Fangbeast »

This is good stuff, I learned a few things today as well:):)
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Cant get Win32 API

Post 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*
"Have you tried turning it off and on again ?"
649psoft2
New User
New User
Posts: 2
Joined: Tue Mar 27, 2012 6:19 am

Re: Cant get Win32 API

Post 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
Zach
Addict
Addict
Posts: 1676
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Cant get Win32 API

Post 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.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Cant get Win32 API

Post by blueznl »

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply