程式語言 - High Level Assembly (HLA) - Win32 API (HLA v1.x) - Dialog - Handle Event



參考網站:
http://winapi.freetechsecrets.com/win32/
http://masm32.com/board/index.php?topic=3584.0
https://www.plantation-productions.com/Webster/
https://www.plantation-productions.com/Webster/Win32Asm/win32API.html

一般在建立Windows視窗時,會註冊自定義的Class並且設定事件處理的副程式(callback),不過,目前範例使用的Class是系統預設的WC_DIALOG,因此,可以透過SetWindowLong()設定callback,這種替換callback的行為就稱為Subclass,意指為原本Class的Subclass,所以Subclass後,需要將原先callback的位址(SetWindowLong()的回傳值)保存下來,然後透過呼叫CallWindowProc()(傳入原先callback的位址),用來將處理控制權移交回原先視窗元件,所以當有多層Subclass的情形發生時,每次在處理事件時,記得判斷對應的Handle,如:在處理關閉事件時(WM_CLOSE),會先判斷目前的hWnd是否為當前的視窗hWnd,如果是當前視窗hWnd,才進行關閉的動作,避免關閉到其它視窗元件

main.hla

program main;
 
#include("w.hhf")
#include("args.hhf")
#include("memory.hhf")
#include("strings.hhf")
 
static
    hWin:        dword;
    hInstance:   dword;
    CommandLine: string;
    defWndProc:  dword;
 
readonly
    szCaption: string:= "main";

procedure WndProc(hWnd:dword; uMsg:uns32; wParam:dword; lParam:dword); @stdcall;
begin WndProc;
    if (uMsg == w.WM_CLOSE) then
        w.DestroyWindow(hWnd);
        xor(eax, eax);
        exit WndProc;
    elseif (uMsg == w.WM_DESTROY) then
        w.PostQuitMessage(0);
        xor(eax, eax);
        exit WndProc;
    endif;

    w.CallWindowProc(defWndProc, hWnd, uMsg, wParam, lParam);
end WndProc;

procedure WinMain(hInst:dword; hPrevInst:dword; CmdLine:string; CmdShow:dword);
var
    msg: w.MSG;
 
begin WinMain;
    w.CreateWindowEx(w.WS_EX_LEFT, w.WC_DIALOG, szCaption,
        w.WS_OVERLAPPEDWINDOW | w.WS_VISIBLE, 0, 0, 300, 300, 0, 0, NULL, NULL);
    mov(eax, hWin);

    w.SetWindowLong(hWin, w.GWL_WNDPROC, &WndProc);
    mov(eax, defWndProc);
     
    forever
        w.GetMessage(msg, NULL, 0, 0);
        breakif(!eax);
 
        w.DispatchMessage(msg);
    endfor;
    mov(msg.wParam, eax);
end WinMain;
 
begin main;
    w.GetModuleHandle(NULL);
    mov(eax, hInstance);
    mov(arg.cmdLn(), CommandLine);

    WinMain(hInstance, NULL, CommandLine, w.SW_SHOWNORMAL);

    w.ExitProcess(eax);
end main;

Line 17:Pascal預設的Calling Convention並不是stdcall,因此,如果不使用@stdcall關鍵字,參數記得反相擺放
Line 19~27:關閉視窗的順序為:主視窗收到WM_CLOSE事件時,呼叫DestroyWindow(),DestroyWindow()會自動將子視窗的相關資源也一併釋放掉(包含Resource描述的資源),系統接著會發送WM_DESTROY事件給主視窗,待主視窗收到WM_DESTROY事件時,呼叫PostQuitMessage()結束視窗,值得注意的地方是,SendMessage()是屬於Block方式呼叫(必須等待動作執行完畢才返回),而PostQuitMessage()則是Non-block方式呼叫
Line 22:在離開副程式時,Pacal語法是使用exit而非Assembly的ret(),如果使用ret()離開,記得要手動釋放Stack資源
Line 29:將處理控制權移交給下一個視窗元件

完成