PDA

View Full Version : How change textbox(edit-control) background color



zlatkoAB
30-03-2009, 16:09
Hi guys... :)
Sorry if i'm post on wrong place.
My question is:
How change back color of textbox.
I search trough help and don't find any command which can do that.
or I miss something?
here is my example:


DECLARE FUNCTION AnimateWindow LIB "USER32.DLL" ALIAS "AnimateWindow" (BYVAL hWnd AS DWORD, BYVAL dwTime AS DWORD, BYVAL dwFlags AS DWORD) AS LONG

uses "UI"

'---Global variables declaration-------------------------------------------------------------------
DIM Msg AS LONG
DIM wParam AS LONG
DIM lParam AS LONG
DIM hwnd AS LONG
DIM text$ AS STRING
'----------------------------------------------------------------------------------------------------
DIALOG NEW 0, "Color Edit", -1, -1, 195, 180, _
%WS_DLGFRAME OR %DS_CENTER OR %WS_CAPTION OR %WS_SYSMENU,0 TO hwnd

%ID_SLIDE = 10
%ID_ACTIVATE = 20
%ID_BUTTON1 = 30
%ID_HIDE = 40
%ID_CENTER = 50
%ID_HOR_POSITIVE = 60
%ID_HOR_NEGATIVE = 70
%ID_VER_POSITIVE = 80
%ID_VER_NEGATIVE = 90
%ID_EDIT = 100
' add controls-------------------------------------------------------------------------------------
CONTROL ADD BUTTON , hwnd, %ID_BUTTON1,"SetEditBackColor", 10, 10, 80,18
CONTROL ADD TEXTBOX , hwnd, %ID_EDIT,"BackColor Green", 100, 10, 80, 13,%ES_LEFT

'show dialog
DIALOG SHOW MODELESS hwnd

'---Start the main message loop/////////////////////////
WHILE IsWindow(hwnd)
'///////////////////////////////////////////////////////

'---Get the message and fill wParam and lParam-----
Msg = GetMessage(hwnd, wParam, lParam)
'--------------------------------------------------

SELECT CASE Msg

CASE %WM_KEYUP
CASE %WM_COMMAND
'========================================
SELECT CASE wParam

CASE %ID_BUTTON1
text$ = CONTROL_GetText(hwnd,%ID_EDIT)
MSGBOX 0, text$,32, "Edit.Text"
'What command or block of code or API
'I must use here to change edit control
'background color?

END SELECT
'===========================================
CASE %WM_SYSCOMMAND

SELECT CASE wParam
CASE %SC_CLOSE
EXIT WHILE
END SELECT

END SELECT ' Endselect Msg

WEND
'..............................................
DIALOG END hwnd


Thanks advance
Zlatko

Petr Schreiber
30-03-2009, 16:37
Hi Zlatko,

the answer is simple : CONTROL SET COLOR

One thing - your script is perfectly valid, but you probably feel that WHILE / WEND to get messages takes lot of power from the interpreter - it reads the lines again and again, checking for message...

Callbacks are the future - I modified your example to use them.
You will see it is very intuitive.

To explain the differences between older WHILE / GetMessage / WEND coding and callbacks is explained in ThinBasic Journal #2 (http://community.thinbasic.com/index.php?topic=2310.msg17554#msg17554), article "Callbacks – new approach for UI programming".

Here is callbackified version of your code:



uses "UI"

BEGIN CONST
%ID_SLIDE = %WM_USER + 500 ' -- WM_USER is safe offset for control IDs ... adding 500 makes it super safe :)
%ID_ACTIVATE ' -- The other controls will have assigned ID +1 automatically
%ID_BUTTON1
%ID_HIDE
%ID_CENTER
%ID_HOR_POSITIVE
%ID_HOR_NEGATIVE
%ID_VER_POSITIVE
%ID_VER_NEGATIVE
%ID_EDIT
END CONST
DECLARE FUNCTION AnimateWindow LIB "USER32.DLL" ALIAS "AnimateWindow" (BYVAL hWnd AS DWORD, BYVAL dwTime AS DWORD, BYVAL dwFlags AS DWORD) AS LONG

function tbMain()
local hWnd as dword ' -- Callbacks allow keeping hWnd "private" or "local" to creation function

DIALOG NEW 0, "Color Edit", -1, -1, 195, 180, _
%WS_DLGFRAME OR %DS_CENTER OR %WS_CAPTION OR %WS_SYSMENU TO hwnd

CONTROL ADD BUTTON , hwnd, %ID_BUTTON1,"SetEditBackColor", 10, 10, 80,18
CONTROL ADD TEXTBOX , hwnd, %ID_EDIT,"BackColor Green", 100, 10, 80, 13

DIALOG SHOW MODAL hwnd call dlgProc

end function

callback function dlgProc()
Local ControlText as string
' -- Callback function preallocates and prefills handle to CbHndl
' -- mesage to CbMsg, ...

SELECT CASE CbMsg

CASE %WM_COMMAND
'========================================
SELECT CASE CbCtl ' -- Which control

CASE %ID_BUTTON1
' -- React only in case of click
if CbCtlMsg = %BN_Clicked THEN
ControlText = CONTROL_GetText(CbHndl,%ID_EDIT)
control set color CbHndl, %ID_EDIT, %BLACK, %GREEN
control redraw CbHndl, %ID_EDIT
end if

END SELECT

CASE %WM_CLOSE
DIALOG END CbHndl

END SELECT
end function



Petr