It is currently Thu Apr 02, 2020 10:28 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: #Gadget or GadgetID - convert automatically to GadgetID
PostPosted: Fri Feb 21, 2014 12:05 pm 
Offline
User
User
User avatar

Joined: Fri Feb 21, 2014 11:46 am
Posts: 17
I found the following Trick to handle automatically conversion to GadgetID, but this doesn't work in all situations reliable
Code:
If IsGadget(GadgetID) And Not IsWindow_(GadgetID)     ; If you've passed a #Gadget instead of a GadgetID(), it will be convert automatically.
      GadgetID = GadgetID(GadgetID)
   EndIf

Therefore i tried to find an alternative conversion
Code:
    If Not GetDlgCtrlID_( GadgetID )    ; If the reverse value of GadgetID is no GadgetNr the GadgetID-Value is no GadgetID
      GadgetID = GadgetID( GadgetID )    ; Convert it
    EndIf


I think this works reliable, please give me a feedback

_________________
I may not know much, but I try a lot


Top
 Profile  
Reply with quote  
 Post subject: Re: #Gadget or GadgetID - convert automatically to GadgetID
PostPosted: Fri Feb 21, 2014 1:48 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Apr 20, 2005 12:48 pm
Posts: 1104
Do a check with IsGadget()

_________________
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,


Top
 Profile  
Reply with quote  
 Post subject: Re: #Gadget or GadgetID - convert automatically to GadgetID
PostPosted: Fri Feb 21, 2014 2:33 pm 
Offline
User
User
User avatar

Joined: Fri Feb 21, 2014 11:46 am
Posts: 17
va!n wrote:
Do a check with IsGadget()

Is not the same, because: I created a gadget with Nr 422 and called test -->
Code:
Procedure test ( GadgetID  )
  If  Not IsGadget( GadgetID )             ; produces a value 3211200 which is true
    GadgetID = GadgetID( GadgetID )    ; therefor the conversion will not be done
  EndIf
  SendMessage_( GadgetID, #EM_SHOWBALLOONTIP, 0, @Balloon )  ; gives me a failure

  but:
  If Not GetDlgCtrlID_( GadgetID )       ; realizes that this is not really a GadgetID
    GadgetID = GadgetID( GadgetID )   ; will be executed
  EndIf
  SendMessage_( GadgetID, #EM_SHOWBALLOONTIP, 0, @Balloon ) ; proceeds without failure

_________________
I may not know much, but I try a lot


Top
 Profile  
Reply with quote  
 Post subject: Re: #Gadget or GadgetID - convert automatically to GadgetID
PostPosted: Fri Feb 21, 2014 2:54 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10583
Location: Beyond the pale...
I must be missing something here because it all works fine for me. Nothing is debugged with the following which is correct.

Code:
  If OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(422, 10, 10, 200, 20, "Standard Button")

    If Not IsGadget(422)
      Debug "Not return #True!"
    EndIf

    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf


You seem to be making additional work for yourself here.

Can you give me an example of when the following fails? Not that I'd ever use it because it simply isn't needed inmo. If some clot passes a hWnd instead of a gadget# to one of your library procedures then they deserve the outcome, whatever that might be! :) If you use #PB_Any in particular then this really isn't needed. IsGadget() will suffice.

Code:
If IsGadget(GadgetID) And Not IsWindow_(GadgetID)     ; If you've passed a #Gadget instead of a GadgetID(), it will be convert automatically.
      GadgetID = GadgetID(GadgetID)
   EndIf

_________________
I may look like a mule, but I'm not a complete ass.


Top
 Profile  
Reply with quote  
 Post subject: Re: #Gadget or GadgetID - convert automatically to GadgetID
PostPosted: Fri Feb 21, 2014 4:35 pm 
Offline
User
User
User avatar

Joined: Fri Feb 21, 2014 11:46 am
Posts: 17
[quote="srod"]I must be missing something here because it all works fine for me. Nothing is debugged with the following which is correct.
...
Thank you for your attention, here is my code. It seems that my other code uses by calling 'Show_StrGdgBalloon' GadgetNr or the GadgetID,
and i couldnt find the problem, so I will solve this here and be sure that i use GadgetID and
if i dont use 'If Not GetDlgCtrlID_( GadgetID )' my balloon on LostFocus will not appear
Code:
; This code is copy and paste, so i have to thank to all guys who find a fragment of their own work here

#StrGdg_User = 422 : #wMain = 400

Structure __Struct_StrGdgBalloon
  StructSize.l
  CompilerIf #PB_Compiler_Processor=#PB_Processor_x64 : _pad1.l : CompilerEndIf
  pTitle.i
  pText.i
  Icon.l
  CompilerIf #PB_Compiler_Processor=#PB_Processor_x64 : _pad2.l : CompilerEndIf
EndStructure
 
Procedure CreateGadget( GadgetTyp, GadgetNr, x, y, w, h, Text.s="", Flags=0, gTitle.s="" )
  Define.l gTitleID, gTitleID_Width = 30, gTitleID_Height = 20
  Define.i gadPosX
 
  ; First, create the Gadget
  Select GadgetTyp                                                               
    Case #PB_GadgetType_Button    :  ButtonGadget(   GadgetNr, x, y, w, h, Text )
    Case #PB_GadgetType_ComboBox  :  ComboBoxGadget( GadgetNr, x, y, w, h )
    Case #PB_GadgetType_String    :  StringGadget(   GadgetNr, x, y, w, h, Text, Flags )
  EndSelect
 
  ;  Produce the StringGadget and the related TextGadget in one process     
  If gTitle <> ""
    gTitleID =  1000 + GadgetNr
    TextGadget(     gTitleID, gadPosX, y + ( h - 14 ) / 2, 130, 14, gTitle, #PB_Text_Right ) 
    ResizeGadget(   gTitleID, x - gTitleID_Width - 10, y+(h-14)/2, gTitleID_Width, 14)
  EndIf
 
EndProcedure

Procedure Hide_StrGdgBalloon( GadgetID )
  SendMessage_( GadgetID, #EM_HIDEBALLOONTIP, 0, 0 )
EndProcedure

Procedure Show_StrGdgBalloon( hWnd, GadgetID, Title.s, Text.s, IconFlag = #TTI_INFO, MilliSecs = 3000 )
               Title         = "   " + Title
               Text          = #CRLF$ + Text + #CRLF$ + " "
  Protected   TitleBuffer.l = StringByteLength( Title, #PB_Unicode ) + 2
  Protected   TextBuffer.l  = StringByteLength( Text , #PB_Unicode ) + 2
  Protected   *Buffer       = AllocateMemory(   TitleBuffer + TextBuffer )
  Protected.i timerNr       = 123
  Protected   Balloon.__Struct_StrGdgBalloon
  ; I use this Show_StrGdgBalloon for different Gadgets and sometimes is the GadgetNr or the GadgetID delivered so will be sure that:
  If Not GetDlgCtrlID_( GadgetID )    ; If the reverse value of GadgetID is no GadgetNr the GadgetID-Value is no GadgetID
    ; Debug IsGadget( GadgetID )         in this situation IsGadget doesnt help 
    GadgetID = GadgetID( GadgetID )
  EndIf
 
  If OSVersion() < #PB_OS_Windows_Vista And IconFlag > #TTI_ERROR : IconFlag - 3 : EndIf
   
 If *Buffer
    Balloon\StructSize = SizeOf( __Struct_StrGdgBalloon )
    PokeS( *Buffer, Title, Len( Title ), #PB_Unicode)
    PokeS( *Buffer + TitleBuffer, Text, Len( Text ), #PB_Unicode )
    Balloon\pTitle     = *Buffer
    Balloon\pText      = *Buffer + TitleBuffer
    Balloon\Icon       = IconFlag
    SendMessage_( GadgetID, #EM_SHOWBALLOONTIP, 0, @Balloon )
    FreeMemory(   *Buffer )
  EndIf
 
  AddWindowTimer( hWnd, timerNr, MilliSecs )
  Repeat
    If WaitWindowEvent() = #PB_Event_Timer And EventTimer() = timerNr
      Hide_StrGdgBalloon( GadgetID )
      RemoveWindowTimer(  hWnd, timerNr )
      Break
    EndIf
  ForEver
     
  Hide_StrGdgBalloon( GadgetID )
   
EndProcedure
 
If OpenWindow(   #wMain, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CreateGadget(  #PB_GadgetType_String, #StrGdg_User, 70, 10, 100, 20, "", 0, "User" )
  GadgetToolTip( #StrGdg_User,   "Enter user (at least 6 characters)"     )
  ButtonGadget(  1, 70, 35, 100, 20,    "Standard Button")

  If Not IsGadget(422)
    Debug "Not return #True!"
  EndIf

  Repeat
    wwEvent = WaitWindowEvent()
    If wwEvent = #PB_Event_Gadget And EventType() = #PB_EventType_LostFocus And EventGadget() = #StrGdg_User
      If Len( GetGadgetText( #StrGdg_User ) ) < 6
        Show_StrGdgBalloon( #wMain, #StrGdg_User, "Error...", "Must be 6 chars at minimum", #TTI_ERROR, 2000 )
      EndIf
     
    EndIf
   
  Until wwEvent = #PB_Event_CloseWindow
EndIf

_________________
I may not know much, but I try a lot


Top
 Profile  
Reply with quote  
 Post subject: Re: #Gadget or GadgetID - convert automatically to GadgetID
PostPosted: Fri Feb 21, 2014 4:57 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10583
Location: Beyond the pale...
Well firstly, these are functions being used in your own program, so why would you need to check if a gadget# or a gadgetID has been passed? Just code for one and stick to it. There really is no need to put these checks in... unless these routines are to be made available to others via a library for example.

Secondly, if I run the following code :

Code:
a$=Space(100)
GetClassName_(422, @a$, 100)
Debug a$


I get a classname of "PrintCacheLocalConnectionListenerHiddenWindow"

There are loads of hidden windows scattered about which explains why your original Not IsWindow_() fails. You just happened to stumble upon what looks like a system generated window here. I have no idea what it is or quite why it has such a bizarre hWnd? Something I have not encountered before.

Thirdly... just stick to IsGadget(). Use #PB_Any when you create your gadgets and this method will be as safe as houses.

_________________
I may look like a mule, but I'm not a complete ass.


Top
 Profile  
Reply with quote  
 Post subject: Re: #Gadget or GadgetID - convert automatically to GadgetID
PostPosted: Fri Feb 21, 2014 5:17 pm 
Offline
Always Here
Always Here
User avatar

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 5755
Location: Berlin - Germany
srod wrote:
... and this method will be as safe as houses.

<offtopic>
Image
</offtopic>

_________________
PureBasic 5.71 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 19.3 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: #Gadget or GadgetID - convert automatically to GadgetID
PostPosted: Fri Feb 21, 2014 5:21 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10583
Location: Beyond the pale...
:lol:

_________________
I may look like a mule, but I'm not a complete ass.


Top
 Profile  
Reply with quote  
 Post subject: Re: #Gadget or GadgetID - convert automatically to GadgetID
PostPosted: Fri Feb 21, 2014 6:30 pm 
Offline
User
User
User avatar

Joined: Fri Feb 21, 2014 11:46 am
Posts: 17
srod wrote:
:lol:

I found and bought purebasic a few days ago and I am very excited, although I have to rewrite many routines that I had in other programming languages ​​already. and I am even more excited by your feedback. Thank you for your help.

_________________
I may not know much, but I try a lot


Top
 Profile  
Reply with quote  
 Post subject: Re: #Gadget or GadgetID - convert automatically to GadgetID
PostPosted: Fri Feb 21, 2014 6:32 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10583
Location: Beyond the pale...
EfDschoma wrote:
srod wrote:
:lol:

I found and bought purebasic a few days ago and I am very excited, although I have to rewrite many routines that I had in other programming languages ​​already. and I am even more excited by your feedback. Thank you for your help.


You made a good choice. ;)

Welcome aboard.

_________________
I may look like a mule, but I'm not a complete ass.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 9 guests


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

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye