Hullo all.
I decided to quickly create a cool window effect for the community. Basically what this does is allow you to alter the window and give it a custom caption - as well as be able to control your window like a widget.
People may find this useful for the following reasons:
* It shows how to use flags
* It shows a "better" way to organize code which utilizes DLLs so that others may use it.
* It shows a cool way to trick windows
* You can port this code directly into your own project with minimal work
* You can use a custom caption / custom minimize, maximize, restore, and close buttons
* You can use a custom window border
* You can even provide the ability to be able to drag the window by clicking anywhere on it
This code is, for the most part complete. What's missing that I probably won't add in is the ability to resize the window by clicking on any border. The only way to resize the window is with the statusbar. Also I didn't make this code use the GDI for 2 reasons, 1: Too much work, 2: DBP isn't natively suited well for the GDI commands I was planning (Plus would probably run into a few bugs).
This code was made quickly, it only took about an hour to make, debug, and test. All the bugs I ran into were mainly visual ones. The code can easily be extended to meat your needs specifically.
ALSO, this code fixes a DBP "problem" where when you drag your window (with a normal caption) you no longer see your sprites or basic 2d renderings. This fixes that. (I assume because when you drag the DBP window, sync is being called rather than presenting the frame again. While there may be a possibility of the entire back-buffer being replaced with garbage when presenting the frame again, this could be avoided by taking a quick "snapshot" of the window and rendering that while moving/resizing the window as to keep the same effect but without the mentioned problem. This is too much work to do in general, but if one wishes to do that, that is how it should be done.)
REM Project: Cool Window Effect
REM Created: 4/13/2008 11:45:06 AM
REM
REM ***** Main Source File *****
REM
`For window control
#constant WM_NCLBUTTONDOWN 161 `Message
#constant HTCAPTION 2 `WParam
#constant HTLEFT 10 `WParam
#constant HTRIGHT 11 `WParam
#constant HTTOP 12 `WParam
#constant HTTOPLEFT 13 `WParam
#constant HTTOPRIGHT 14 `WParam
#constant HTBOTTOM 15 `WParam
#constant HTBOTTOMLEFT 16 `WParam
#constant HTBOTTOMRIGHT 17 `WParam
#constant HTBORDER 18 `WParam
`For UpdateWindow
#constant WINDOW_DRAWNOTHING 0
#constant WINDOW_DRAWCAPTION 1
#constant WINDOW_DRAWBORDER 2
#constant WINDOW_DRAWSTATUS 4
#constant WINDOW_DRAWCLOSE 8
#constant WINDOW_DRAWMAXBOX 16
#constant WINDOW_DRAWMINBOX 32
#constant WINDOW_HIGHLIGHTS 64
#constant WINDOW_HIGHLIGHTS_EX 128
`For drawing
#constant CAPCOL_TOPLEFT 0xe0c0c0c0
#constant CAPCOL_TOPRIGHT 0xe0c0c0c0
#constant CAPCOL_BTMLEFT 0xe0808080
#constant CAPCOL_BTMRIGHT 0xe0808080
#constant CAPCOL_HTOPLEFT 0xe0dfdfdf
#constant CAPCOL_HTOPRIGHT 0xe0dfdfdf
#constant CAPCOL_HBTMLEFT 0xe09f9f9f
#constant CAPCOL_HBTMRIGHT 0xe09f9f9f
#constant CAPCOL_TEXT 0xe0000000
#constant CAPCOL_HTEXT 0xe01f1f1f
#constant CLXCOL_TOPLEFT 0xe0dd0000
#constant CLXCOL_TOPRIGHT 0xe0dd0000
#constant CLXCOL_BTMLEFT 0xe0990000
#constant CLXCOL_BTMRIGHT 0xe0990000
#constant CLXCOL_HTOPLEFT 0xe0ff0000
#constant CLXCOL_HTOPRIGHT 0xe0ff0000
#constant CLXCOL_HBTMLEFT 0xe0aaaa00
#constant CLXCOL_HBTMRIGHT 0xe0aaaa00
#constant CLXCOL_TEXT 0xe0ffffff
#constant CLXCOL_HTEXT 0xe0ffffd0
#constant BTNCOL_TOPLEFT 0xe00000dd
#constant BTNCOL_TOPRIGHT 0xe00000dd
#constant BTNCOL_BTMLEFT 0xe0000099
#constant BTNCOL_BTMRIGHT 0xe0000099
#constant BTNCOL_HTOPLEFT 0xe000ffff
#constant BTNCOL_HTOPRIGHT 0xe000ffff
#constant BTNCOL_HBTMLEFT 0xe000aaaa
#constant BTNCOL_HBTMRIGHT 0xe000aaaa
#constant BTNCOL_TEXT 0xe0ffffff
#constant BTNCOL_HTEXT 0xe0ffffd0
#constant STACOL_TOPLEFT 0xe0ffffff
#constant STACOL_TOPRIGHT 0xe0eeeeee
#constant STACOL_BTMLEFT 0xe0bbbbbb
#constant STACOL_BTMRIGHT 0xe0aaaaaa
#constant STACOL_TEXT 0xe0000000
`For DLL call
#constant USER32_DLL_NAME "user32.dll"
#constant USER32_DLL_ID 1
`DEMO SPECIFIC
`EFFECT 0 = CUSTOM CAPTION
`EFFECT 1 = CLICK ANYWHERE TO DRAG
#constant DO_EFFECT 0
#constant CAPTION_HEIGHT 25
`Call before using library
InitCoolWindow()
`Figure out which demo to use
select DO_EFFECT
`Custom Caption
case 0
CoolWindowEffect1()
endcase
`Click Anywhere to Drag
case 1
CoolWindowEffect2()
endcase
endselect
`Call when done with library
ReleaseCoolWindow()
end
function CoolWindowEffect1()
`Preliminary
sync on
sync
make object cube 1, 100
`Timing
prevTime = timer()
newTime = timer()
estTime = 0
`Style
style = WINDOW_DRAWCAPTION + WINDOW_DRAWBORDER + WINDOW_DRAWSTATUS + WINDOW_DRAWCLOSE + WINDOW_DRAWMAXBOX + WINDOW_DRAWMINBOX + WINDOW_HIGHLIGHTS + WINDOW_HIGHLIGHTS_EX
`Main loop
do
`Timing
prevTime = newTime
newTime = timer()
estTime = newTime - prevTime
turn object left 1, wrapvalue(estTime * (60.0/1000.0))
`Update our window
UpdateWindow(0, 0, screen width(), screen height(), CAPTION_HEIGHT, style, "Cool Window Effect 1", "Custom Caption Demo")
`Sync
sync
loop
endfunction
function CoolWindowEffect2()
`Preliminary
sync on
sync
make object cube 1, 100
`Timing
prevTime = timer()
newTime = timer()
estTime = 0
`Style
style = WINDOW_DRAWBORDER + WINDOW_DRAWSTATUS + WINDOW_DRAWCLOSE + WINDOW_HIGHLIGHTS
`Main loop
do
`Timing
prevTime = newTime
newTime = timer()
estTime = newTime - prevTime
turn object left 1, wrapvalue(estTime * (60.0/1000.0))
`Update our window
UpdateWindow(0, 0, screen width(), screen height(), 0, style, "Cool Window Effect 2", "Custom Window - Click anywhere to drag!")
`Sync
sync
loop
endfunction
````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
````` COOL WINDOW EFFECT LIBRARY ```````````````````````````````````````````````````````````````````````````````````````````
````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
global g_windowState as dword
`0 = normal
`1 = maximized
`Initialize the library
function InitCoolWindow()
load dll USER32_DLL_NAME, USER32_DLL_ID
set window layout 0, 0, 0
endfunction
`Deinitialize the library
function ReleaseCoolWindow()
delete dll USER32_DLL_ID
endfunction
`Send a message to a window
function SendMessage(hWnd, uiMsg, wParam, lParam)
x = call dll(USER32_DLL_ID, "SendMessageA", hWnd, uiMsg, wParam, lParam)
endfunction x
`Get the current focus window
function GetFocus()
x = call dll(USER32_DLL_ID, "GetFocus")
endfunction x
`Button
function ButtonWindow(x1, y1, x2, y2, text$, c0, c1, c2, c3, hc0, hc1, hc2, hc3, ct, hct)
mx = mousex()
my = mousey()
mc = mouseclick()
retVal = 0
state = 0
if mx > x1-1 and my > y1-y1 and mx < x2+1 and my < y2+1
state = 1
if mc = 1
retVal = 1
endif
endif
if state = 0
box x1, y1, x2, y2, c2, c0, c3, c1
ink ct, 0
else
box x1, y1, x2, y2, hc2, hc0, hc3, hc1
ink hct, 0
endif
center text x1 + ((x2-x1)/2), y1 + ((y2-y1)/2)-(text height(text$)/2), text$
endfunction retVal
`Update the window
function UpdateWindow(x, y, x2, y2, clickHeight, style, capText$, staText$)
`Preliminary
mx = mousex()
my = mousey()
mc = mouseclick()
if clickHeight = 0
clickHeight = y2
endif
inCaption = 0
if mx > x - 1 and my > y - 1 and mx < x2 + 1 and my < y + clickHeight + 1
inCaption = 1
endif
width = x2 - x
`Draw the window's caption
if style && WINDOW_DRAWCAPTION
`Render based on rather or not there are highlights
if style && WINDOW_HIGHLIGHTS_EX
`Render based on rather or not a highlight is justified
if inCaption = 1
box x, y, x2, y+clickHeight, CAPCOL_HBTMLEFT, CAPCOL_HTOPLEFT, CAPCOL_HBTMRIGHT, CAPCOL_HTOPRIGHT
ink CAPCOL_HTEXT, 0
center text (width / 2), (clickHeight / 2) - (text height(capText$) / 2), capText$
else
box x, y, x2, y+clickHeight, CAPCOL_BTMLEFT, CAPCOL_TOPLEFT, CAPCOL_BTMRIGHT, CAPCOL_TOPRIGHT
ink CAPCOL_TEXT, 0
center text (width / 2), (clickHeight / 2) - (text height(capText$) / 2), capText$
endif
else
box x, y, x2, y+clickHeight, CAPCOL_BTMLEFT, CAPCOL_TOPLEFT, CAPCOL_BTMRIGHT, CAPCOL_TOPRIGHT
ink CAPCOL_TEXT, 0
center text (width / 2), (clickHeight / 2) - (text height(capText$) / 2), capText$
endif
endif
`Further setup
captionHeight = clickHeight
clickHeight = 25
btnX = x2 - 35
btnY = y+2
btnW = 33
btnH = clickHeight - 4
btnS = 2
`Draw a status bar
if style && WINDOW_DRAWSTATUS
box x, y2-clickHeight, x2, y2, STACOL_BTMLEFT, STACOL_TOPLEFT, STACOL_BTMRIGHT, STACOL_TOPRIGHT
text x + 5, (y2-clickHeight) + ((clickHeight / 2) - (text height(capText$) / 2)), staText$
endif
`Draw the window's border
if style && WINDOW_DRAWBORDER
ink 0xe0000000, 0
`Top-Left to Top-Right
line x, y, x2, y
`Top-Left to Bottom-Left
line x, y, x, y2
`Top-Right to Bottom-Right
line x2-1, y, x2-1, y2
`Bottom-Left to Bottom-Right
line x, y2-1, x2, y2-1
`Caption border
if style && WINDOW_DRAWCAPTION
line x, y+captionHeight, x2, y+captionHeight
endif
`Status border
if style && WINDOW_DRAWSTATUS
line x, y2-clickHeight, x2, y2-clickHeight
bx1 = x2 - (clickHeight - 4)
by1 = y2 - (clickHeight - 2)
bx2 = x2 - 2
by2 = y2 - 2
line bx1, by2-2, bx2-2, by1
line bx1, by2-7, bx2-7, by1
line bx1, by2-12, bx2-12, by1
if mx > bx1 and my > by1 and mx < bx2 and my < by2 and mc = 1
SendMessage(GetFocus(), WM_NCLBUTTONDOWN, HTBOTTOMRIGHT, 0)
endif
endif
endif
`Draw the close button
if style && WINDOW_DRAWCLOSE
result = ButtonWindow(btnX, btnY, btnX + btnW, btnY + btnH, "X", CLXCOL_TOPLEFT, CLXCOL_TOPRIGHT, CLXCOL_BTMLEFT, CLXCOL_BTMRIGHT, CLXCOL_HTOPLEFT, CLXCOL_HTOPRIGHT, CLXCOL_HBTMLEFT, CLXCOL_HBTMRIGHT, CLXCOL_TEXT, CLXCOL_HTEXT)
if result = 1
end
endif
endif
`Draw the maximize button
if style && WINDOW_DRAWMAXBOX
btnW = 25
btnX = btnX - (btnW + btnS)
result = ButtonWindow(btnX, btnY, btnX + btnW, btnY + btnH, "+", BTNCOL_TOPLEFT, BTNCOL_TOPRIGHT, BTNCOL_BTMLEFT, BTNCOL_BTMRIGHT, BTNCOL_HTOPLEFT, BTNCOL_HTOPRIGHT, BTNCOL_HBTMLEFT, BTNCOL_HBTMRIGHT, BTNCOL_TEXT, BTNCOL_HTEXT)
if result = 1
if g_windowState = 0
g_windowState = 1
maximize window
else
g_windowState = 0
restore window
endif
endif
endif
`Draw the minimize button
if style && WINDOW_DRAWMINBOX
btnW = 25
btnX = btnX - (btnW + btnS)
result = ButtonWindow(btnX, btnY, btnX + btnW, btnY + btnH, "-", BTNCOL_TOPLEFT, BTNCOL_TOPRIGHT, BTNCOL_BTMLEFT, BTNCOL_BTMRIGHT, BTNCOL_HTOPLEFT, BTNCOL_HTOPRIGHT, BTNCOL_HBTMLEFT, BTNCOL_HBTMRIGHT, BTNCOL_TEXT, BTNCOL_HTEXT)
if result = 1
minimize window
`To Do: Add support to restore the window
endif
btnX = btnX - (btnW + btnS)
endif
`Control the window
if inCaption = 1 and mc = 1
SendMessage(GetFocus(), WM_NCLBUTTONDOWN, HTCAPTION, 0)
endif
endfunction
Cheers,
-naota
My email actually IS "nocannedmeat@gmail.com". Why? I don't know.
Aex.Uni forums