ความรู้เกี่ยวกับคำสั่ง: Get Message และ Send Message รูปแบบต่างๆเรามาดูคำสั่งที่เกี่ยวข้องกับการรับ - ส่ง Message ว่ามีอะไรกันบ้าง
Message แบ่งออกเป็น 3 ประเภท
1. GUI Message
2.Windows Message
3.Macro Message
----------------------------------------------------------------------------
Message ในที่นี้หมายถึง การรับ - ส่งสัญญานควบควบ ..ไม่ใช่ Popup MessageBox นะครับ
Message ต่างๆที่ใช้ในการควบคุมการทำงานของโปรแกรม จะใช้วิธีรับหรือส่งสัญญานออกไปก่อน
จากนั้น จะมีการเลือกการควบคุม ว่าจะควบคุมอะไร? ให้ทำงาน
คล้ายกับการส่ง SMS ในโทรศัพท์มือถีอ ..เมื่อพิมพ์ข้อความเสร็จ
จากนั้นก็จะเลือกว่า.. จะส่งให้กับผู้ใด?
------------------------------------------------------------------------
เดี๋ยวลงโค๊ดตัวอย่างมาให้ทดสอบ รับ - ส่ง Message ในรูปแบบต่างๆ
ท่านจะได้รับการอธิบายให้เข้าใจในโค๊ดคำสั่งอย่างถึงกึ๋น อย่างที่ไม่มีใครอธิบายได้ละเอียดกว่านี้
ขอให้ติดตามผลงานกันไปเรื่อยๆ ท่านจะได้ ..มากกว่าคำว่า ..ได้
- Code:
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 154, 162, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 40, 128, 75, 25, $WS_GROUP)
$Input1 = GUICtrlCreateInput("Input1", 16, 24, 121, 21)
$Graphic1 = GUICtrlCreateGraphic(48, 64, 52, 44)
GUISetState(@SW_SHOW)
Sleep(30000) ; หน่วงเวลาให้โชว์หน้าต่างโปแรกม 30 วินาที
----------------------------------------------------------------------------------------
จากโค๊ดจะเห็นได้ว่า มี Object อยู่บน Form 3 อย่าง คือ Button, Input และ Graphic
การที่โปรแกรมที่สร้าง จะโชว์หรือแสดงหน้าต่างให้เห็น ต้องมีการกระตุ้นให้เกิดการทำงานของ Form ก่อน
ด้วยคำสั่ง GUISetState(@SW_SHOW) เมื่อกระตุ้นเรียบร้อยแล้ว ก็ต้องมีการตั้งเวลาให้โปรแกรมทำงาน
ถ้าไม่มีการกำหนดเวลาให้โปรแกรมทำงาน โปรแกรมจะโชว์ให้เห็นแว๊บเดียวเท่าันั้น
ที่เป็นเช่นนี้เพราะ From และ Object เป็น Library design ทีถูกผูกติดอยู่กับเงื่อนไขเวลา
-------------------------------------------------------------------------------------------------------------
ทีนี้จะทำอย่างไรล่ะ่?!? ที่จะทำให้หน้าต่างโปรแกรมนั้นโชว์อยู่ได้ตลอดเวลา
มีอยู่ทางเดียวที่จะทำได้ คือ ใช้คำสั่ง Loop เพื่อที่จะให้เวลานั้นไม่หยุดเดิน
- Code:
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 154, 162, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 40, 128, 75, 25, $WS_GROUP)
$Input1 = GUICtrlCreateInput("Input1", 16, 24, 121, 21)
$Graphic1 = GUICtrlCreateGraphic(48, 64, 52, 44)
GUISetState(@SW_SHOW)
Do
Sleep(100)
Until False
---------------------------------------------------------------------------------------
ดูจากโค๊ดจะเห็นได้ว่า.. ไม่ว่าจะเป็น Form หรือ Object ต่างก็มีตัวแปรด้วยกันทั้งนั้น
เพราะ Message จะได้จากการรีเทิร์นค่า ออกมาทางตัวแปร
แต่เนื่องจาก Object อาศัยอยู่บน Form ..Objectจะทำอะไร ต้องได้รับอนุญาตจาก Form ก่อนถึงจะทำได้
ในชั้นการควบคุมนั้น ต้องใช้คำสั่ง GUIGetMsg() เพื่อรับสัญญานควบคุมมาจาก Form ก่อน
เมื่อได้รับสัญญานควบคุมจาก Form แล้ว จากนั้นแล้วค่อยมาเลือกว่า จะให้ Form หรือ Object ทำอะไร?
ด้วยคำสั่งที่ใช้เป็นตัวเลือก เช่น Switch Case , Select Case หรือ If Else เป็นต้น.
-------------------------------------------------------------------------------------
เมื่อท่านรันทดสอบแล้วปรากฏว่า ปิดโปรแกรมไม่ได้ ก็เพราัะว่า ท่านยังไม่ได้เรียกใช้งาน
Event ID message ของ Form ที่มีให้เลือกใช้หลายคำสั่ง ดังนี้
Event ID the ID of the control sending the message
0 หมายถึง No event
$GUI_EVENT_CLOSE หมายถึง dialog box being closed (either by defined button or system menu).
$GUI_EVENT_MINIMIZE หมายถึง dialog box minimized with Windows title bar button.
$GUI_EVENT_RESTORE หมายถึง dialog box restored by click on task bar icon.
$GUI_EVENT_MAXIMIZE หมายถึง dialog box maximized with Windows title bar button.
$GUI_EVENT_MOUSEMOVE หมายถึง the mouse cursor has moved.
$GUI_EVENT_PRIMARYDOWN หมายถึง the primary mouse button was pressed.
$GUI_EVENT_PRIMARYUP หมายถึง the primary mouse button was released.
$GUI_EVENT_SECONDARYDOWN หมายถึง the secondary mouse button was pressed.
$GUI_EVENT_SECONDARYUP หมายถึง the secondary mouse button was released.
$GUI_EVENT_RESIZED หมายถึง dialog box has been resized.
$GUI_EVENT_DROPPED หมายถึง End of a Drag&Drop action @GUI_DRAGID, @GUI_DRAGFILE and @GUI_DROPID หมายถึง will be used to retrieve the ID's/file corresponding to the involve control.
-------------------------------------------------------------------------------------------------------------
- Code:
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 154, 162, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 40, 128, 75, 25, $WS_GROUP)
$Input1 = GUICtrlCreateInput("Input1", 16, 24, 121, 21)
$Graphic1 = GUICtrlCreateGraphic(48, 64, 52, 44)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
-------------------------------------------------------------
คำสั่ง GUIGetMsg() ..นอกจากจะใช้งานแบบปกติธรรมดาแล้ว
เรายังสามารถกำหนดการใช้งานแบบ Advance ได้อีก ดังนี้
When using the "advanced" parameter the information is returned in an array with extended information:
$array[0] = 0 or Event ID or Control ID
$array[1] = The window handle the event is from
$array[2] = The control handle the event is from (if applicable)
$array[3] = The current X position of the mouse cursor (relative to the GUI window)
$array[4] = The current Y position of the mouse cursor (relative to the GUI window)
If the GUIOnEventMode option is set to 1 then the return from GUIGetMsg is always 0 and the @error is set to 1.
If the option GUIEventOptions is set to 1 the minimize, restore and maximize button will not do any action on the window just a simple notification.
-----------------------------------------------------------------------------------------------
- Code:
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 154, 162, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 40, 128, 75, 25, $WS_GROUP)
$Input1 = GUICtrlCreateInput("Input1", 16, 24, 121, 21)
$Graphic1 = GUICtrlCreateGraphic(48, 64, 52, 44)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg(1)
Switch $nMsg[0]
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
ถ้้ากำหนด $nMsg = GUIGetMsg(1) ..ใส่หมายเลข1 ไว้ในวงเล็บแบบนี้
ตัวแปรของ GUIGetMsg() คือ $nMsg ต้องกำหนดไว้เป็น Array เช่น $nMsg[0] เป็นต้น.
------------------------------------------------------------------------------------------
เรามาดูการเขียนคำสั่งที่ใช้เป็นตัวเลือกในแบบอื่นๆดูบ้าง
- Code:
While 1
$nMsg = GUIGetMsg()
Select
Case $nMsg = $GUI_EVENT_CLOSE
Exit
EndSelect
WEnd
----------------------------------------------------------------
- Code:
While 1
$nMsg = GUIGetMsg()
If $nMsg = -3 Then Exit
WEnd
-----------------------------------------------
- Code:
Do
Sleep(10)
Until GUIGetMsg() = $GUI_EVENT_CLOSE
--------------------------------------------------------
- Code:
Do
Sleep(10)
Until GUIGetMsg() = -3
------------------------------------------------------
- Code:
While 1
$nMsg = GUIGetMsg(1)
If $nMsg[0] = $GUI_EVENT_CLOSE Then ExitLoop
WEnd