Buttons colored by Gradientfill

Windows specific questions.
Post Reply
Kwabbernoot
Posts: 80
Joined: Apr 19, 2010 18:23
Location: NL

Buttons colored by Gradientfill

Post by Kwabbernoot »

Feel free to experiment with your favourite colors.

Code: Select all

'  ******************************
'  *                            *
'  *  TEST WINDOWS API BUTTONS  *
'  *                            *
'  ******************************
'
#INCLUDE ONCE "WINDOWS.BI"

DECLARE SUB WinInit
DECLARE FUNCTION WinProc (BYVAL Hwin AS HWND, _
                  BYVAL message AS UINT, _
                  BYVAL wParam AS WPARAM, _
                  BYVAL lParam AS LPARAM) AS LRESULT
DECLARE SUB ColorButton (BYREF PntDrawIS AS DRAWITEMSTRUCT PTR, _
                  BYVAL ButColor AS INTEGER, _
                  BYVAL ButWidth AS INTEGER, BYVAL ButHeight AS INTEGER)
DECLARE FUNCTION GradientFill LIB "msimg32" ALIAS "GradientFill"( _
                  BYVAL hdc AS HDC, _
                  BYVAL pVertex AS PTRIVERTEX, _
                  BYVAL dwNumVertex AS DWORD, _
                  BYVAL pMesh AS CONST PVOID, _
                  BYVAL dwNumMesh AS DWORD, _
                  BYVAL dwMode AS DWORD) AS BOOL

#DEFINE GRADIENT_FILL_RECT_H  0
#DEFINE CodeRed 1
#DEFINE CodeGreen 2
#DEFINE CodeBlue 3
#DEFINE CodeYellow 4
#DEFINE CodePurple 5
ENUM
   IdButRed = 101
   IdButGreen
   IdButBlue
   IdButYellow
   IdButPurple
END ENUM

   DIM SHARED AS HINSTANCE ProgInst
   DIM SHARED AS HWND Hwin
   DIM SHARED AS HWND HwinRed, HwinGreen, HwinBlue
   DIM SHARED AS HWND HwinYellow, HwinPurple
   DIM AS MSG WinMessage

'*** Start

   WinInit   '*** Initialize windows session

'*** Process windows messages

   DO WHILE GetMessage(@WinMessage, Null, 0, 0)
      TranslateMessage @WinMessage
      DispatchMessage @WinMessage
   LOOP

'*** End

   END
'
'  ********************************
'  *  INITIALIZE WINDOWS SESSION  *
'  ********************************
'
SUB WinInit

   DIM AS WNDCLASS WinClass
   DIM AS STRING AppName

'*** Setup window class

   ProgInst = GetModuleHandle(Null)
   AppName = "Colored Buttons"

   WITH WinClass
      .style         = CS_HREDRAW or CS_VREDRAW
      .lpfnWndProc   = @WinProc
      .cbClsExtra    = 0
      .cbWndExtra    = 0
      .hInstance     = ProgInst
      .hIcon         = LoadIcon(Null, IDI_APPLICATION)
      .hCursor       = LoadCursor(Null, IDC_ARROW)
'      .hbrBackground = GetStockObject(LTGRAY_BRUSH)
'      .hbrBackground = GetSysColorBrush(COLOR_3DFACE)
      .hbrBackground = CAST(HBRUSH, COLOR_BACKGROUND)
      .lpszMenuName  = Null
      .lpszClassName = STRPTR(AppName)
   END WITH

'*** Register the window class

   IF RegisterClass(@WinClass) = False THEN
      MessageBox(Null, "Failed to register the window class", AppName, _
		  MB_ICONERROR)
      END
   END IF

'*** Create the window and show it

   Hwin = CreateWindowEx(0, AppName, AppName, WS_OVERLAPPEDWINDOW, _
     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, _
     Null, Null, ProgInst, Null)   '*** Create window (windows)
   ShowWindow Hwin, SW_NORMAL  '*** Show window (windows)
   UpdateWindow Hwin           '*** Update (client area) window (windows)

END SUB
'
'  ******************************
'  *  PROCESS WINDOWS MESSAGES  *
'  *  (called by Windows)       *
'  ******************************
'
FUNCTION WinProc(BYVAL Hwin AS HWND, _
                 BYVAL Message AS UINT, _
                 BYVAL wParam AS WPARAM, _
                 BYVAL lParam AS LPARAM) AS LRESULT

   DIM AS DRAWITEMSTRUCT PTR PntDrawIS

   FUNCTION = 0

'*** Process windows messages

   SELECT CASE Message
   CASE WM_CREATE   '*** Create menus
      HwinRed = CreateWindowEx(0, "BUTTON", "RED", _
        WS_VISIBLE OR WS_CHILD OR BS_OWNERDRAW, 10, 10, 100, 50, Hwin, _
        CAST(HMENU, IdButRed), ProgInst, NULL)
      HwinBlue = CreateWindowEx(0, "BUTTON", "BLUE", _
        WS_VISIBLE OR WS_CHILD OR BS_OWNERDRAW, 10, 70, 100, 50, Hwin, _
        CAST(HMENU, IdButBlue), ProgInst, NULL)
      HwinGreen = CreateWindowEx(0, "BUTTON", "GREEN", _
        WS_VISIBLE OR WS_CHILD OR BS_OWNERDRAW, 10, 130, 100, 50, Hwin, _
        CAST(HMENU, IdButGreen), ProgInst, NULL)
      HwinYellow = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "YELLOW", _
        WS_VISIBLE OR WS_CHILD OR BS_OWNERDRAW, 10, 190, 100, 50, Hwin, _
        CAST(HMENU, IdButYellow), ProgInst, NULL)
      HwinPurple = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "PURPLE", _
        WS_VISIBLE OR WS_CHILD OR BS_OWNERDRAW, 10, 250, 100, 50, Hwin, _
        CAST(HMENU, IdButPurple), ProgInst, NULL)

   CASE WM_COMMAND   '*** User selected a command

'*** Process commands

      SELECT CASE LOWORD(wParam)
      CASE IdButRed
         MessageBox(Hwin, "Red Button Pressed", "Red", MB_OK)
      CASE IdButGreen
         MessageBox(Hwin, "Green Button Pressed", "Green", MB_OK)
      CASE IdButBlue
         MessageBox(Hwin, "Blue Button Pressed", "Blue", MB_OK)
      CASE IdButYellow
         MessageBox(Hwin, "Yellow Button Pressed", "Yellow", MB_OK)
      CASE IdButPurple
         MessageBox(Hwin, "Purple Button Pressed", "Purple", MB_OK)
      END SELECT

   CASE WM_DRAWITEM

'*** Owner-drawn button

      PntDrawIS = CAST(DRAWITEMSTRUCT PTR, lParam)
      SELECT CASE PntDrawIS->CtlID
      CASE IdButRed
         ColorButton (PntDrawIS, CodeRed, 100, 50)   '*** Color button
      CASE IdButGreen
         ColorButton (PntDrawIS, CodeGreen, 100, 50)  '*** Color button
      CASE IdButBlue
         ColorButton (PntDrawIS, CodeBlue, 100, 50)  '*** Color button
      CASE IdButYellow
         ColorButton (PntDrawIS, CodeYellow, 95, 45)  '*** Color button
      CASE IdButPurple
         ColorButton (PntDrawIS, CodePurple, 100, 50)  '*** Color button
      END SELECT

   CASE WM_KEYDOWN   '*** A (nonsystem) key is pressed
      IF LOBYTE(wParam) = 27 THEN
         PostMessage Hwin, WM_CLOSE, 0, 0
      END IF

'*** Window was closed

   CASE WM_DESTROY
      PostQuitMessage(0)
      EXIT FUNCTION

'*** Give message back to windows

   CASE ELSE
      RETURN DefWindowProc(Hwin, Message, wParam, lParam)
   END SELECT
   RETURN 0

END FUNCTION
'
'  ******************
'  *  COLOR BUTTON  *
'  ******************
'
SUB ColorButton (BYREF PntDrawIS AS DRAWITEMSTRUCT PTR, _
                 BYVAL ButColor AS INTEGER, _
                 BYVAL ButWidth AS INTEGER, BYVAL ButHeight AS INTEGER)

   DIM AS HWND HwinBut
   DIM AS TRIVERTEX Vertex(0 TO 1)
   DIM AS GRADIENT_RECT GraRect
   DIM AS RECT RectPnt
   DIM AS UINTEGER RGB0, RGB1
	DIM AS ZSTRING*30 ButBuffer

   HwinBut = PntDrawIS->hwndItem   '** Get handle of button
   Vertex(0).x = 0
   Vertex(0).y = 0
   Vertex(1).x = ButWidth
   Vertex(1).y = ButHeight

   IF PntDrawIS->itemState AND ODS_SELECTED THEN

'*** Set colors for button pressed

      SELECT CASE Butcolor
      CASE CodeRed
         RGB0 = &HFF0000
         RGB1 = &HCC0000
      CASE CodeGreen
         RGB0 = &H00FF00
         RGB1 = &H00CC00
      CASE CodeBlue
'         RGB0 = &H0000FF
         RGB0 = &H87CEEB
         RGB1 = &H0000CC
      CASE CodeYellow
         RGB0 = &HFFFF00
         RGB1 = &HFFDF00
      CASE ELSE
         RGB0 = &HE3BBFF
         RGB1 = &HC8A2C8
      END SELECT

   ELSE

'*** Set colors for button (not pressed)

      SELECT CASE Butcolor
      CASE CodeRed
         RGB0 = &H990000
         RGB1 = &HDD0000
      CASE CodeGreen
         RGB0 = &H009900
         RGB1 = &H00DD00
      CASE CodeBlue
         RGB0 = &H000099
         RGB1 = &H0000DD
      CASE CodeYellow
         RGB0 = &HFFBF00
         RGB1 = &HFFDB58
      CASE ELSE
         RGB0 = &HB57ECF
         RGB1 = &H800080
      END SELECT
   END IF

'*** Show colors and text

   Vertex(0).Red   = (RGB0 SHR 8) AND &H00FF00
   Vertex(0).Green =  RGB0 AND &H00FF00
   Vertex(0).Blue  = (RGB0 SHL 8) AND &H00FF00
   Vertex(1).Red   = (RGB1 SHR 8) AND &H00FF00
   Vertex(1).Green =  RGB1 AND &H00FF00
   Vertex(1).Blue  = (RGB1 SHL 8) AND &H00FF00

   GraRect.UpperLeft = 0
   GraRect.LowerRight = 1
   GradientFill(PntDrawIS->hDC, @Vertex(0), 2, @GraRect, 1, _
     GRADIENT_FILL_RECT_H)          '*** Fill rectangle (windows)
   GetClientRect(HwinBut, @RectPnt) '*** Get client coordinates (windows)
   SetBkMode(PntDrawIS->hDC, TRANSPARENT)
                                    '*** Set background mode (windows)
   GetWindowText(HwinBut, ButBuffer, 30)
                                    '*** Get text of a control (windows)
   DrawText(PntDrawIS->hDC, ButBuffer, -1, @RectPnt, DT_CENTER OR _
     DT_VCENTER OR DT_SINGLELINE)  '*** Draw text in rectangle (windows)

END SUB
Kwabbernoot
Posts: 80
Joined: Apr 19, 2010 18:23
Location: NL

Re: Buttons colored by Gradientfill

Post by Kwabbernoot »

I've changed the program and the colors.

Code: Select all

'  ******************************
'  *                            *
'  *  SHOW WINDOWS API BUTTONS  *
'  *                            *
'  ******************************
'
#DEFINE Win_Height 600
#DEFINE Win_Width 900

#INCLUDE ONCE "WINDOWS.BI"

DECLARE SUB StartWin
DECLARE FUNCTION WinProc (BYVAL Hwin AS HWND, _
                  BYVAL message AS UINT, _
                  BYVAL wParam AS WPARAM, _
                  BYVAL lParam AS LPARAM) AS LRESULT
DECLARE SUB ColorBox (BYREF PntDrawIS AS DRAWITEMSTRUCT PTR, _
                      ColorInd AS INTEGER)
DECLARE FUNCTION GradientFill LIB "msimg32" ALIAS "GradientFill"( _
                  BYVAL hdc AS HDC, _
                  BYVAL pVertex AS PTRIVERTEX, _
                  BYVAL dwNumVertex AS DWORD, _
                  BYVAL pMesh AS CONST PVOID, _
                  BYVAL dwNumMesh AS DWORD, _
                  BYVAL dwMode AS DWORD) AS BOOL

ENUM
   IdButRed = 101, IdButGreen, IdButBlue
   IdButYellow, IdButPurple, IdButAubergine
   IdStat1 = 201
END ENUM

   DIM SHARED AS HINSTANCE ProgInst
   DIM SHARED AS HWND HwinStat1
   DIM SHARED AS CONST UINTEGER BoxColors(1 TO 28) = { _
     &HE11E38, &HE17688, &HE59D9D, &HE75D7A, _    '* Red
     &H6B8E23, &H98FB98, &HADFF2F, &H90EE90, _    '* Green
     &H5CB7EB, &HA2E1DC, &HDFEFF9, &H9ED0ED, _    '* Blue
     &HFFBF00, &HFFDB58, &HFFFF00, &HFFDF00, _    '* Yellow
     &HBE1FEB, &HF288D7, &HE9DDEF, &HFCACDA, _    '* Purple
     &H61045F, &HAA076B, &HF7BB97, &HDD5E89, _    '* Aubergine
     &H98B4B7, &HE9EEC6, 0, 0}                    '* Blue-yellow

'*** Start

   StartWin   '*** Start windows session

'*** End

   END
'
'  ***************************
'  *  START WINDOWS SESSION  *
'  ***************************
'
SUB StartWin

   DIM AS INTEGER StartX
   DIM AS WNDCLASS WinClass
   DIM AS MSG WinMsg
   DIM AS HWND Hwin
   DIM AS STRING AppName

'*** Setup window class

   ProgInst = GetModuleHandle(Null)
   AppName = "Colored Buttons"

   WITH WinClass
      .style         = CS_HREDRAW or CS_VREDRAW
      .lpfnWndProc   = PROCPTR(WinProc)
      .cbClsExtra    = 0
      .cbWndExtra    = 0
      .hInstance     = ProgInst
      .hIcon         = LoadIcon(Null, IDI_APPLICATION)
      .hCursor       = LoadCursor(Null, IDC_ARROW)
      .hbrBackground = CreateSolidBrush(&HD0D0D0)
      .lpszMenuName  = 0
      .lpszClassName = STRPTR(AppName)
   END WITH

'*** Register the window class

   IF RegisterClass(@WinClass) = False THEN
      MessageBox(Null, "Failed to register the window class", AppName, _
		  MB_ICONERROR)
      END
   END IF

'*** Create the window and show it

   StartX = (GetSystemMetrics(SM_CXSCREEN ) \ 2) - Win_Width \ 2

   Hwin = CreateWindowEx(0, AppName, AppName, WS_OVERLAPPEDWINDOW, _
     StartX, 0, Win_Width, Win_Height, 0, 0, ProgInst, 0)
   ShowWindow Hwin, SW_NORMAL  '*** Show window (windows)
   UpdateWindow Hwin           '*** Update (client area) window (windows)

'*** Process Windows-messages

   DO WHILE GetMessage(@WinMsg, 0, 0, 0) > 0
      IF IsDialogMessage(Hwin, @WinMsg) = 0 THEN
         TranslateMessage @WinMsg
         DispatchMessage @WinMsg
      END IF
   LOOP

END SUB

'
'  ******************************
'  *  PROCESS WINDOWS MESSAGES  *
'  *  (called by Windows)       *
'  ******************************
'
FUNCTION WinProc(BYVAL Hwin AS HWND, _
                 BYVAL Message AS UINT, _
                 BYVAL wParam AS WPARAM, _
                 BYVAL lParam AS LPARAM) AS LRESULT

   STATIC AS INTEGER ColorInd, ColorStat
   DIM AS INTEGER WinId
   DIM AS HWND HwinBut
   DIM AS DRAWITEMSTRUCT PTR PntDrawIS

'*** Process windows messages

   SELECT CASE Message
   CASE WM_CREATE   '*** Create controls
      HwinBut = CreateWindowEx(0, "BUTTON", "Red button", _
        WS_VISIBLE OR WS_CHILD OR BS_OWNERDRAW, 10, 10, 140, 50, Hwin, _
        CAST(HMENU, IdButRed), ProgInst, NULL)
      HwinBut = CreateWindowEx(0, "BUTTON", "Green button", _
        WS_VISIBLE OR WS_CHILD OR BS_OWNERDRAW, 10, 70, 140, 50, Hwin, _
        CAST(HMENU, IdButGreen), ProgInst, NULL)
      HwinBut = CreateWindowEx(0, "BUTTON", "Blue button", _
        WS_VISIBLE OR WS_CHILD OR BS_OWNERDRAW, 10, 130, 140, 50, Hwin, _
        CAST(HMENU, IdButBlue), ProgInst, NULL)
      HwinBut = CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "Yellow button", _
        WS_VISIBLE OR WS_CHILD OR BS_OWNERDRAW, 10, 190, 140, 50, Hwin, _
        CAST(HMENU, IdButYellow), ProgInst, NULL)
      HwinBut = CreateWindowEx(0, "BUTTON", "Purple button", _
        WS_VISIBLE OR WS_CHILD OR BS_OWNERDRAW, 10, 250, 140, 50, Hwin, _
        CAST(HMENU, IdButPurple), ProgInst, NULL)
      HwinBut = CreateWindowEx(0, "BUTTON", "Aubergine button", _
        WS_VISIBLE OR WS_CHILD OR BS_OWNERDRAW, 10, 310, 140, 50, Hwin, _
        CAST(HMENU, IdButAubergine), ProgInst, NULL)
      ColorStat = 25
      HwinStat1 = CreateWindowEx(0, "STATIC", "No button pressed", _
        WS_CHILD OR WS_VISIBLE OR SS_OWNERDRAW OR SS_NOPREFIX, _
        210, 310, 200, 100, Hwin, CAST(HMENU, IdStat1), ProgInst, 0)

   CASE WM_COMMAND   '*** Process commands
      WinId = LOWORD(wParam)
      IF WinId = IDCANCEL THEN   '*** Escape key
         PostMessage (Hwin, WM_CLOSE, 0, 0)
      ELSEIF WinId < IdStat1 THEN   '*** Button pressed
         ColorStat = (WinId - 100) * 4 - 3
         SetWindowText (HwinStat1, "Button pressed")
      END IF

   CASE WM_DRAWITEM   '*** Owner-drawn control
      PntDrawIS = CAST(DRAWITEMSTRUCT PTR, lParam)
      WinId = PntDrawIS->CtlID
      ColorInd = IIF(WinId = IdStat1, ColorStat, (WinId - 100) * 4 - 3)
      ColorBox (PntDrawIS, ColorInd)   '*** Color box

   CASE WM_DESTROY   '*** Window was closed
      PostQuitMessage(0)

   CASE ELSE   '*** Return message to windows
      RETURN DefWindowProc(Hwin, Message, wParam, lParam)
   END SELECT
   RETURN 0

END FUNCTION
'
'  ***************
'  *  COLOR BOX  *
'  ***************
'
SUB ColorBox (BYREF PntDrawIS AS DRAWITEMSTRUCT PTR, _
              ColorInd AS INTEGER)

   DIM AS HWND HwinBut
   DIM AS TRIVERTEX Vertex(1)
   DIM AS GRADIENT_RECT GraRect
   DIM AS RECT RectPnt
   DIM AS INTEGER WinId, Ind
   DIM AS UINTEGER RGB0, RGB1
   DIM AS ZSTRING*100 ButBuffer

   IF ColorInd < 1 OR ColorInd > 25 THEN EXIT SUB

   HwinBut = PntDrawIS->hwndItem    '*** Get handle of button
   GetClientRect(HwinBut, @RectPnt) '*** Get client coordinates (windows)
   Vertex(0).x = 0
   Vertex(0).y = 0
   Vertex(1).x = RectPnt.right - RectPnt.left
   Vertex(1).y = RectPnt.bottom - RectPnt.top

   IF PntDrawIS->itemState AND ODS_SELECTED THEN

'*** Set colors for button pressed

      RGB0 = BoxColors(ColorInd + 2)
      RGB1 = BoxColors(ColorInd + 3)
   ELSE

'*** Set colors for button (not pressed) or static control

      RGB0 = BoxColors(ColorInd)
      RGB1 = BoxColors(ColorInd + 1)
   END IF

'*** Show colors and text

   Vertex(0).Red   = (RGB0 SHR 8) AND &H00FF00
   Vertex(0).Green =  RGB0 AND &H00FF00
   Vertex(0).Blue  = (RGB0 SHL 8) AND &H00FF00
   Vertex(1).Red   = (RGB1 SHR 8) AND &H00FF00
   Vertex(1).Green =  RGB1 AND &H00FF00
   Vertex(1).Blue  = (RGB1 SHL 8) AND &H00FF00

   GraRect.UpperLeft = 0
   GraRect.LowerRight = 1
   GradientFill(PntDrawIS->hDC, @Vertex(0), 2, @GraRect, 1, 0)
                                    '*** Fill rectangle (windows)
   SetBkMode(PntDrawIS->hDC, TRANSPARENT)
                                    '*** Set background mode (windows)
   GetWindowText(HwinBut, ButBuffer, 100)
                                    '*** Get text of a control (windows)
   DrawText(PntDrawIS->hDC, ButBuffer, -1, @RectPnt, DT_CENTER OR _
     DT_VCENTER OR DT_SINGLELINE)  '*** Draw text in rectangle (windows)

END SUB
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: Buttons colored by Gradientfill

Post by kcvinu »

Hi, i have some questions about this gradient button. Are you there ?
Post Reply