May 06, 2024, 01:42:30 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


static control with colored border

Started by stefanodel, February 07, 2010, 12:30:58 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

stefanodel

I need to have a static control with a colored border. Using the style flag @BORDER I can get a black border, I have no idea how to change the color and if it is possible to do it.

Thanks
Stefano

sapero

Hello stefanode, this example shows how to maniputale the size of nonclient area (border) and how you can paint it:
// You don't need to specify @BORDER style.

$include "windowssdk.inc"

CONST STATIC_1 = 1000
DIALOG d1
CREATEDIALOG d1,0,0,300,202,0x80CA0080,0,"Caption",&d1_handler
CONTROL d1,@STATIC,"Static",67,53,70,20,0x5000010B,STATIC_1
DOMODAL d1
end

SUB d1_handler
SELECT @MESSAGE
CASE @IDINITDIALOG
CENTERWINDOW d1
SetBorderColor(d1, STATIC_1, 0x0000FF)
' start animation timer
StartTimer d1, 1000

CASE @IDTIMER
SetBorderColor(d1, STATIC_1, rnd(0,1) * 0xFFFFFF)

CASE @IDCLOSEWINDOW
CLOSEDIALOG d1,@IDOK

ENDSELECT
RETURN
ENDSUB

sub SetBorderColor(WINDOW w,int id, int co1or)
HWND hwndControl = GetControlHandle(w, id)
' delete old color
HPEN pen = GetProp(hwndControl, "NCBK")
if (pen) then DeleteObject(pen)
' save new color
SetProp(hwndControl, "NCBK", CreatePen(PS_SOLID, 1, co1or))
' check if subclassed
if (!GetProp(hwndControl, "NCBKFN"))
SetProp(hwndControl, "NCBKFN", SetWindowLong(hwndControl, GWL_WNDPROC, &BorderColorWndProc))
' force WM_NCCALCSIZE
SetWindowPos(hwndControl,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_FRAMECHANGED)
endif
' redraw it
WINRECT rc
GetClientRect(hwndControl, &rc) ' dummy
RedrawWindow(hwndControl, &rc, 0, RDW_FRAME|RDW_INVALIDATE|RDW_UPDATENOW)
endsub


sub BorderColorWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam),LRESULT
WNDPROC OriginalHandler = GetProp(hwnd, "NCBKFN")
WINRECT rc

select (uMsg)
case WM_DESTROY
DeleteObject(GetProp(hwnd, "NCBK"))
RemoveProp(hwnd, "NCBK")
RemoveProp(hwnd, "NCBKFN")
SetWindowLong(hwnd, GWL_WNDPROC, OriginalHandler)

case WM_NCCALCSIZE
if (wParam)

pointer NewWindowRect = *<NCCALCSIZE_PARAMS>lParam.rgrc : settype NewWindowRect,WINRECT
pointer NewClientRect = NewWindowRect     : settype NewClientRect,WINRECT

*NewClientRect.left   = *NewWindowRect.left+1 ' border sizes
*NewClientRect.top    = *NewWindowRect.top+1
*NewClientRect.right  = *NewWindowRect.right-1
*NewClientRect.bottom = *NewWindowRect.bottom-1
return 1
endif

case WM_NCPAINT
'CallWindowProc(OriginalHandler, hwnd, WM_NCPAINT, wParam, 0)
HDC hdc = GetWindowDC(hwnd)
GetWindowRect(hwnd, &rc)
HPEN   pen = SelectObject(hdc, GetProp(hwnd, "NCBK"))
HBRUSH hbr = SelectObject(hdc, GetStockObject(NULL_BRUSH))
Rectangle(hdc, 0, 0, rc.right-rc.left, rc.bottom-rc.top)
SelectObject(hdc, hbr)
SelectObject(hdc, pen)
ReleaseDC(hwnd, hdc)
return 0
endselect
return CallWindowProc(OriginalHandler, hwnd, uMsg, wParam, lParam)
endsub

stefanodel

thank you Sapero,
I download your windows include files and tried the code you have posted in my code, all works fine.

Thank you again very much!

Stefano