ThaiPBL.com

PowerBuilder Library Thailand

อีเมล พิมพ์ PDF

Resize window amd set center (ปรับขนาด window อัตโนมัติ )

อันดับแรก ให้กำหนดตัวแปรแบบ Global ดังนี้


CONSTANT Integer DevelopWidth = 800
environment env
Long scrHeight
Long scrWidth


สำหรับตัวแปร DevelopWidth นันให้เราทำการกำหนดค่าที่เราใช้งานอยู่ในที่นี้ที่กำหนดใว้ว่า 800 หมายถึงขณะนี้เรากำหนดหน้าจอเป็น 800 x 600

ตัวแปร  env, scrHeight, scrWidth ใช้สำหรับ เก็บค่าความละเอียดปัจจุบันของหน้าจอ


อันดับต่อมาไปที่ Application เขียนสคริปต์ที่ Open event ดังนี้

integer rtn
rtn = GetEnvironment(env)
scrHeight = env.screenheight
scrWidth = env.screenwidth

ต่อมาก็ให้ทำการสร้างฟังก์ชั่น ขึ้นมาหนึ่งตัว  ตั้งชื่อว่า winsize กำหนด return เป็น integer และกำหนด ตัวแปร 2 ตัวw_ref กำหนด type เป็น window
b_sizewindow กำหนด type เป็น boolean
กำหนดค่า passed เป็น value
w_ref จะเป็นตัวบอกให้ทราบว่าจะทำงานกับ window ตัวใด
b_sizewindow เป็นตัวอ้างถึงขนาดความกว้าง,สูง ของตัว window
คราวนี้ก็มาดูรายละเอียดของสคริปต์ในตัวฟังก์ชั่นกันต่อ

dragobject temp
line temp2
integer cnt, i
double ratio

// Calculate the ratio
ratio = scrWidth / DevelopWidth

// ถ้าค่า ratio ที่ได้เท่ากับ 1 แสดงว่าความละเอียดเท่ากับที่เรากำหนดใว้ตอนแรก

if ratio = 1 then
return 0
end if

// Resize the window ถ้าเราต้องการ
If bsizewindow then
w_ref.width *= ratio
w_ref.height *= ratio
end if

// Now resize the controls

cnt = upperbound(w_ref.control)

for i = cnt to 1 step -1
if typeof(w_ref.control[i]) = line! then
temp2 = w_ref.control[i]
temp2.beginx *= ratio
temp2.beginy *= ratio
temp2.endx *= ratio
temp2.endy *= ratio
else
temp = w_ref.control[i]
temp.x *= ratio
temp.width *= ratio
temp.y *= ratio
temp.height *= ratio

// Change the fontsize

choose case typeof(w_ref.control[i])
case commandbutton!
commandbutton cb
cb = temp
cb.textsize *= ratio

case singlelineedit!
singlelineedit sle
sle = temp
sle.textsize *= ratio

case editmask!
editmask em
em = temp
em.textsize *= ratio

case statictext!
statictext st
st = temp
st.textsize *= ratio

case datawindow! // datawindows get
zoomed
datawindow dw
dw = temp
dw.Object.DataWindow.zoom =
string(int(ratio*100))

case picturebutton!
picturebutton pb
pb = temp
pb.textsize *= ratio

case checkbox!
checkbox cbx
cbx = temp
cbx.textsize *= ratio

case dropdownlistbox!
dropdownlistbox ddlb
ddlb = temp
ddlb.textsize *= ratio

case groupbox!
groupbox gb
gb = temp
gb.textsize *= ratio

case listbox!
listbox lb
lb = temp
lb.textsize *= ratio

case multilineedit!
multilineedit mle
mle = temp
mle.textsize *= ratio

case radiobutton!
radiobutton rb
rb = temp
rb.textsize *= ratio

end choose
end if
next

return 1
============================

คราวนี้ก็ถึงการนำไปใช้งานก็ไม่ยากวางสคริปต์ด้านล่างนี้เอาใว้ที่ open event โดยที่เรากำหนด resizewin เป็น true ก็แสดงว่ามีการเปลี่ยนแปลงขนาดตัว วินโดวส์ตามความละเอียดบนหน้าจอ

boolean resizeWin
resizeWin = true
winsize(this, resizeWin)

และเรายังสามารถกำหนดตัววินโดวส์ให้อยู่กึ่งกลางจอได้อีกด้วย โดยการสร้างฟังก์ชั่นอีกหนึ่งตัวตั้งชื่อว่า wincenter กำหนดตัวแปรหนึ่งตัว ตั้งชื่อว่า w_ref กำหนด type เป็น window และค่า passed by เป็น value

เขียนสคริปต์ด้านล่างลงไป

Integer thisHeight
Integer thisWidth
Integer thisX
Integer thisY

thisHeight = UnitsToPixels(w_ref.height,YUnitsToPixels!)
thisWidth = UnitsToPixels(w_ref.width, XUnitsToPixels!)
thisX = (scrWidth - thisWidth) / 2
thisY = (scrHeight - thisHeight) / 2
w_ref.x = PixelsToUnits(thisX, XPixelsToUnits!)
w_ref.y = PixelsToUnits(thisY, YPixelsToUnits!)

return 0
=====================
วิธีการใช้งานก็เรียกฟังก์ชั่นขึ้นมาดังนี้

wincenter(this)

เราควรจะเรียกใช้ตัว winsize ก่อนเรียกใช้ตัว wincenter