ThaiPBL.com

PowerBuilder Library Thailand

อีเมล พิมพ์ PDF

Marquee ทำตัวหนังสือวิ่ง

Marquee
สวัสดีครับ วันนี้จะขอเสนอเทคนิคในการทำตัวหนังสือวิ่ง หรือที่เรียกกันว่า marquee น่ะครับ เป็นการเพิ่มลูกเล่นให้กับ application ของเราน่ะครับ อันดับแรกเลยเราต้องทำการสร้าง user object ขึ้นมาใช้งานก่อนโดยตั้งชื่อว่า  u_message_board ซึ่งข้างใน object ตัวนี้ก็จะมีฟังก์ชั่นต่างๆอยู่ 6 ฟังก์ชั่นด้วยกัน แต่อันดับแรกเราต้องทำการกำหนด Instance variable ก่อนดังนี้ครับ 

PRIVATE:
statictext ist_leader
statictext ist_follower
long il_BackColor = 0
long il_TextColor = 255
string isz_Font = "Arial"
int in_Fontsize = 10
int in_speed = 16
จากนั้นก็กำหนด local external function ดังนี้
FUNCTION UINT SetTimer( uint hwin, uint idtimer, uint timeout, long tmprc ) library "user32.dll"
FUNCTION BOOLEAN KillTimer( uint hwin, uint idtimer ) library "user32.dll"
คราวนี้ก็มาลงมือกันเลยนะครับ เริ่มจากให้ทำการคลิกที่ user object แล้วเลือกที่ visual และคลิกที่ custom แล้วก็ทำการย่อขนาดให้เล็กลงให้มีเส้นกริดสัก 2 แถวก็พอ แล้วก็วาง static text ลงไปเขียนข้อความที่เราต้องการลงไป จากนั้นที่ constructor event ของตัว user object ให้วางสคริปต์นี้ลงไป
this.SetTimer( Handle( this ), 0, 50, 0 )
st_message1.Height = this.Height
st_message2.Height = this.Height
ist_leader = st_message1
ist_follower = st_message2
mle_1.y = this.Height + 20
และที่ destructor event
this.KillTimer( Handle( this ),0 )
จากนั้นก็กำหนด user event ขึ้นมาใหม่อีก 2 ตัวทำการตั้งชื่อและ map ดังนี้ครับ
ue_time pbm_timer
ue_resize pbm_size
เสร็จแล้วก็ไปเขียนสคริปต์ที่ ue_resize
long l_Size
mle_1.width = this.width
l_Size = uf_GetSize( )
และที่ ue_time ก็วางสคริปต์นี้ลงไป
statictext st_hold
If ist_leader.width < this.width Then
If ist_leader.x = 0 Then
Return
End If
ist_leader.x = 0
ist_follower.x = ist_leader.x + ist_leader.width
Return
End If
ist_leader.Move( ist_leader.x - in_speed , 0 )
ist_follower.Move( ist_leader.x + ist_leader.width, 0 )
If ABS( ist_leader.x ) > ist_leader.width AND ist_leader.x < 0 Then
st_hold = ist_leader
ist_leader = ist_follower
ist_follower = st_hold
ist_follower.Move( ist_leader.x + ist_leader.width, 0 )
End If

เอาละ คราวนี้เราลงมือเขียน user object function กันได้เลย ซึ่งมีอยู่ด้วยกัน 6 ฟังก์ชั่นดังนี้เลยครับ

uf_getsize
int n
string sz_mod
DO WHILE mle_1.LineCount() > 1
mle_1.width += 20
LOOP
If mle_1.Width < this.width Then
st_message1.width = this.width
st_message2.width = this.width
Return this.width
Else
st_message1.width = mle_1.width + 4
st_message2.width = mle_1.width + 4
Return mle_1.width + 4
End If
Return -1
uf_setcolor กำหนดค่าต่างๆดังนี้ครับ access=public return = none
arg_textcolor long value
arg_backcolor long value
il_textcolor = arg_textcolor
il_backcolor = arg_backcolor
st_message1.textcolor = il_textcolor
st_message1.backcolor = il_backcolor
st_message2.textcolor = il_textcolor
st_message2.backcolor = il_backcolor
this.backcolor = il_backcolor

uf_setfont กำหนดค่าต่างๆดังนี้ครับaccess = public return = none
arg_font string value

long l_sizeisz_font = arg_font
st_message1.facename = isz_font
st_message2.facename = isz_font
mle_1.Facename = isz_font
l_Size = uf_GetSize( )

uf_setfontsize กำหนดค่าต่างๆดังนี้ครับaccess = public return = none
arg_textsize integer value

long l_size
in_fontsize = arg_textsize
st_message1.textsize = in_fontsize
st_message2.textsize = in_fontsize
mle_1.textsize = in_fontsize
l_Size = uf_GetSize( )
uf_setmessage กำหนดค่าต่างๆดังนี้ครับaccess = public return = none
arg_message string value

long l_Poslong l_Size
mle_1.width = this.width
l_Pos = Pos( arg_message, "~r~n" )
DO WHILE l_Pos > 0arg_message = Replace( arg_message, l_Pos, 2, " " )
l_Pos = Pos( arg_message, "~r~n" , l_Pos - 1 )
LOOP
mle_1.text = arg_message
st_message1.text = arg_message
st_message2.text = arg_message

l_Size = uf_GetSize( )
uf_setspeed กำหนดค่าต่างๆดังนี้ครับaccess = public return = none
arg_speed integer value

in_speed = arg_speed
เมื่อทำตามขั้นตอนด้านบนเสร็จแล้วคราวนี้เราก็มาลองใช้งานกันนะครับ ให้เปิด วินโดวส์ เพนเตอร์ขึ้นมา แล้ววาง object ต่างๆลงไปดังรูปเลยครับ

สีตัวหนังสือ ตั้งชื่อว่า sle_textcolor
สีพื้น ตั้งชื่อว่า sle_backcolor
แบบอักษร ตั้งชื่อว่า sle_font
ขนาด ตั้งชื่อว่า sle_fontsize
ข้อความ ตั้งชื่อว่า mle_message
ความเร็ว ตั้งชื่อว่า em_speed
เมื่อทำเสร็จแล้วเราก็จะมาเขียนสคริปต์กันในแต่ละส่วนๆกันนะครับ
Sle_Textcolor modified event
uo_1.uf_SetColor( Long( this.text) , Long( sle_backcolor.text ) )
Sle_Backcolor modified event
uo_1.uf_SetColor( Long( sle_textcolor.text) , Long( this.text ) )
Sle_Font modified event
uo_1.uf_SetFont( this.text )
Sle_Fontsize modified event
uo_1.uf_SetFontSize( long(this.text) )
Mle_Message modified event
uo_1.uf_SetFontSize( long(this.text) )
Em_Speed. modified event
uo_1.uf_SetSpeed( long( this.text ) )
และที่ขาดไม่ได้ก็คือ open event
uo_1.uf_SetMessage( mle_message.text )
uo_1.uf_SetFont( sle_font.text )
uo_1.uf_SetFontSize( Long(sle_fontsize.text) )
uo_1.uf_SetColor( long(sle_textcolor.text), long(sle_backcolor.text) )
และที่ resize event
uo_1.width = this.workspacewidth()