PowerBASIC Peer Support Forums
 

Go Back   PowerBASIC Peer Support Forums > User to user Discussions > Source Code

Notices

Source Code PowerBASIC and related source code. Please do not post questions or discussions, just source code.

Reply
 
Thread Tools Display Modes
  #1  
Old Dec 16th, 2006, 02:12 AM
Kev Peel Kev Peel is offline
Member
 
Join Date: Sep 2000
Location: South West, United Kingdom
Posts: 2,858
Creating UNICODE controls dynamically in a DDT dialog

Code:
'______________________________________________________________________________
'
'  Unicode for DDT
'  ---------------
'
'  Some easy to use functions that allow the creation and manipulation of
'  UNICODE format controls in a DDT dialog box. The unicode support is
'  dynamically called in case of an older, non-unicode OS.
'
' --------------------------------------------
' Created by Kev Peel, KGP Software. Dec 2006. version 1.00.
'______________________________________________________________________________
 
#Compile Exe
#Dim All
#Register All
 
$APPTITLE = "UncDDT"
%USEMACROS = 1
#Include "WIN32API.INC"
 

'------------------------------------------------------------------------------
' Create the specified class as a unicode control.
'
'   suClass is the class to create (ie. "EDIT"). Must be a unicode string.
'   hParent is the handle of the parent dialog (DDT). Required.
'   nID is the control identifier. Required.
'   suTitle is the title text to use. Must be a unicode string.
'   x, y are the horizontal and vertical position (dialog units).
'   w, h are the width and height (dialog units).
'   dwStyle is the standard style bits to use. Optional.
'   dwStyleEx is the extended style bits to use. Optional.
'
' Returns a nonzero unicode window handle on success, else NULL.
'
' Warning: Only works on UNICODE-supported versions of Windows (NT/2000/XP+).
'          The unicode functions are dynamically called.
'------------------------------------------------------------------------------
Function ControlAddUnicode(ByVal suClass As String, _
                           ByVal hParent As Dword, _
                           ByVal nID As Long, _
                           ByVal suTitle As String, _
                           ByVal x As Long, _
                           ByVal y As Long, _
                           ByVal w As Long, _
                           ByVal h As Long, _
                           Optional ByVal dwStyle As Dword, _
                           Optional ByVal dwStyleEx As Dword) As Dword
 
  Local hLib As Dword, hFunc As Dword, hCtl As Dword
  Local plpClassName As Dword, plpWindowName As Dword
  Local hInstance As Dword
 
  ' Parameter check...
  If Len(suClass) = 0 Then Exit Function
  If IsWindow(hParent) = %FALSE Then Exit Function
  If (nID = 0) Then Exit Function
 
  ' Translate from DUs to Pixels...
  Dialog Units hParent, x, y To Pixels x, y
  Dialog Units hParent, w, h To Pixels w, h
 
  ' Always use these styles...
  If (dwStyle And %WS_CHILD) = 0 Then dwStyle = dwStyle Or %WS_CHILD
  If (dwStyle And %WS_VISIBLE) = 0 Then dwStyle = dwStyle Or %WS_VISIBLE
 
  ' Call the unicode function dynamically...
  hLib = LoadLibrary("USER32.DLL")
  If hLib Then
     hFunc = GetProcAddress(hLib, "CreateWindowExW")
     If hFunc Then
        hInstance = GetModuleHandle(ByVal %NULL)
        ! pushad
        ! push 0
        ! push hInstance
        ! push nID
        ! push hParent
        ! push h
        ! push w
        ! push y
        ! push x
        ! push dwStyle
        plpWindowName = StrPtr(suTitle)
        ! push plpWindowName
        plpClassName = StrPtr(suClass)
        ! push plpClassName
        ! push dwStyleEx
        ! call hFunc
        ! mov hCtl, eax
        ! popad
     End If
     FreeLibrary hLib
  End If
 
  If hCtl Then
     ' Either use parent font or Windows' GUI font...
     Local hFont As Dword
     hFont = SendMessage(hParent, %WM_GETFONT, 0, 0)
     If (hFont = %NULL) Then hFont = GetStockObject(%DEFAULT_GUI_FONT)
     SendMessage hCtl, %WM_SETFONT, hFont, %TRUE
     Function = hCtl
  End If
 
End Function
 

'------------------------------------------------------------------------------
' Changes the text of the specified unicode control.
'
'   hParent is the handle of the parent dialog (DDT). Required.
'   nID is the control identifier. Required.
'   suTitle is the title text to use. Must be a unicode string.
'
' Returns nonzero on success, else zero.
'
' Warning: Only works on UNICODE-supported versions of Windows (NT/2000/XP+).
'          The unicode functions are dynamically called.
'------------------------------------------------------------------------------
Function ControlSetTextUnicode(ByVal hParent As Dword, ByVal nID As Long, ByVal suTitle As String) As Long
 
  Local hLib As Dword, hFunc As Dword, rvLong As Long
  Local plpWindowName As Dword
 
  ' Parameter check...
  If IsWindow(hParent) = %FALSE Then Exit Function
  If (nID = 0) Then Exit Function
 
  ' Call the unicode function dynamically...
  hLib = LoadLibrary("USER32.DLL")
  If hLib Then
     hFunc = GetProcAddress(hLib, "SetDlgItemTextW")
     If hFunc Then
        ! pushad
        plpWindowName = StrPtr(suTitle)
        ! push plpWindowName
        ! push nID
        ! push hParent
        ! call hFunc
        ! mov rvLong, eax
        ! popad
        Function = rvLong
     End If
     FreeLibrary hLib
  End If
 
End Function
 

 
'------------------------------------------------------------------------------
' Returns the text of the specified unicode control.
'
'   hParent is the handle of the parent dialog (DDT). Required.
'   nID is the control identifier. Required.
'
' Returns the actual string on success, else an empty string.
'
' Warning: Only works on UNICODE-supported versions of Windows (NT/2000/XP+).
'          The unicode functions are dynamically called.
'------------------------------------------------------------------------------
Function ControlGetTextUnicode(ByVal hParent As Dword, ByVal nID As Long) As String
 
  Local hLib As Dword, hFunc As Dword, rvLong As Long
  Local plpWindowName As Dword, dwLen As Dword, suTitle As String
 
  ' Parameter check...
  If IsWindow(hParent) = %FALSE Then Exit Function
  If (nID = 0) Then Exit Function
  dwLen = SendDlgItemMessage(hParent, nID, %WM_GETTEXTLENGTH, 0, 0)
  If (dwLen = 0) Then Exit Function
 
  ' Call the unicode function dynamically...
  hLib = LoadLibrary("USER32.DLL")
  If hLib Then
     hFunc = GetProcAddress(hLib, "GetDlgItemTextW")
     If hFunc Then
        suTitle = String$((dwLen*2), $Nul)
        Incr dwLen
        ! pushad
        ! push dwLen
        plpWindowName = StrPtr(suTitle)
        ! push plpWindowName
        ! push nID
        ! push hParent
        ! call hFunc
        ! mov rvLong, eax
        ! popad
        If (rvLong) Then Function = suTitle
     End If
     FreeLibrary hLib
  End If
 
End Function
 

'------------------------------------------------------------------------------
' Program Start Point
'------------------------------------------------------------------------------
Function PBMain
 
  Local hDlg As Dword
 
  ' Set the DDT dialog font. This is optional,
  ' depending on which characters you need...
  Dialog Font "Arial", 18
 
  ' Create the main program interface with DDT...
  Dialog New %HWND_DESKTOP, $APPTITLE, , , 150, 100, %WS_OVERLAPPEDWINDOW To hDlg
 
  ' Create a unicode child window (uses smiley face for text)...
  ControlAddUnicode UCode$("STATIC"), hDlg, 100, Chr$(&H3B, &H26), 10, 10, 100, 50
 
  ' Change the text (uses DELTA symbol here)...
  ControlSetTextUnicode hDlg, 100, Chr$(&H06, &H22) + UCode$(" Hello Unicode DDT!")
 
  ' Get the existing text and reset it...
  ControlSetTextUnicode hDlg, 100, ControlGetTextUnicode(hDlg, 100)
 
  ' Display the main dialog...
  Dialog Show Modal hDlg
 
End Function
------------------
kgpsoftware.com - Downloads
kgpsoftware.com - Development and Consulting
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:41 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright © 1999-2010 PowerBASIC, Inc. All Rights Reserved.