Foundations of F#.Net phần 7

Tham khảo tài liệu 'foundations of f#.net phần 7', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | CHAPTER 8 USER INTERFACES 183 this case it can be useful to use the function to create a new event that responds only to the left mouse button click. The next example demonstrates how to do this light open let form let temp new Form fun e - fun _ - Left button ignore temp form Here the filter function is used with a function that checks whether the left mouse button is pressed the resulting event is then piped forward to the listen function that adds an event handler to the event exactly as if you had called the event s .Add method. You could have implemented this using an if expression within the event handler but this technique has the advantage of separating the logic that controls the event firing and what happens during the event itself. If you want several event handlers can reuse the new event. Listing 8-3 demonstrates using more of IEvent s functions to create a simple drawing application shown in Figure 8-5 . Here you want to use the MouseDown event in different ways first to monitor whether the mouse is pressed at all and then to split the event into left or right button presses using the function. This is used to control the drawing color either red or black. Listing 8-3. Using Events to Implement a Simple Drawing Application light open System open open let form let temp new Form Text Scribble let pointsMasterList ref let pointsTempList ref let mouseDown ref false let pen ref new Pen fun _ - mouseDown true 184 CHAPTER 8 USER INTERFACES let leftMouse rightMouse fun e - fun _ - pen new Pen fun _ - pen new Pen fun _ - mouseDown false if pointsTempList 1 then let points .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.