Lập trình GUI trong môi trường window

138 826 4
Lập trình GUI trong môi trường window

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Lập trình GUI trong môi trường window

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN KHOA CÔNG NGHỆ PHẦN MỀM LẬP TRÌNH MƠI TRƯỜNG WINDOW Chương 3:LẬP TRÌNH GUI ThS NGUYỄN THỊ THANH TRÚC UIT-VNUHCM Lập trình mơi trường Windows Nội dung Mơ tả đặc tính Window Form Tạo window Form & Cách tạo Menu Các Dialog Các control window form UIT-VNUHCM Lập trình mơi trường Windows 1.FORM UIT-VNUHCM Lập trình môi trường Windows Giao diện người dùng Giao diện người dùng cung cấp phương tiện để người dùng tương tác với chương trình ứng dụng Có hai loại giao diện người dùng: (Character User Giao diện người dùng dạng ký tựtác với chương trình Interface CUI): người dùng tương theo câu lệnh nhập từ bàn phím: ví dụ Ms Dos Giao diện người dùng dạng đồ họa (Graphical User Interface GUI): người dùng tương tác với chương trình thơng qua hệ thống window Giao diện người dùng đồ họa thực thông qua Window Form control chứa bên UIT-VNUHCM Lập trình mơi trường Windows Window Form Các window form dùng để hiển thị tất cửa sổ chương trình ứng dụng Nhận liệu vào từ người dùng hiển thị thông tin kết Khi tạo project kiểu Window Application, có window form tự động thêm vào project Tất Window form thừa kế từ lớp Form chứa namespace: System.Windows.Forms Cũng kế thừa từ window form có sẵn, thêm vào tính UIT-VNUHCM Lập trình mơi trường Windows Window form  Hình sau ví dụ giao diện cửa sổ đăng nhập: UIT-VNUHCM Lập trình mơi trường Windows Visual Studio Net designer controls Properties, events UIT-VNUHCM Lập trình mơi trường Windows UIT-VNUHCM Lập trình môi trường Windows Project  Add Reference UIT-VNUHCM Lập trình mơi trường Windows Form Một « form » cửa sổ hình - đơn vị giao diện người dùng Microsoft đưa kể từ Windows 1.0 Một ứng dụng Windows Forms (WinForms) phải có cửa sổ « main form » (cửa sổ Form chứa component Form có file resource UIT-VNUHCM Lập trình mơi trường Windows 10 159 160 // update menu state and set font to Comic Sans MS 161 private void comicMenuItem_Click( 162 object sender, System.EventArgs e ) Times New Roman 163 { event handler 164 // reset checkmarks for font menu items 165 ClearFont(); 166 167 // set Comic Sans font 168 comicMenuItem.Checked = true; 169 displayLabel.Font = new Font( 170 "Comic Sans MS", 14, displayLabel.Font.Style ); Comic Sans 171 } event handler 172 173 // toggle checkmark and toggle bold style 174 private void boldMenuItem_Click( 175 object sender, System.EventArgs e ) 176 { 177 // toggle checkmark 178 boldMenuItem.Checked = !boldMenuItem.Checked; Courier New event handler 179 180 // use Xor to toggle bold, keep all other styles 181 displayLabel.Font = new Font( 182 displayLabel.Font.FontFamily, 14, 183 displayLabel.Font.Style ^ FontStyle.Bold ); 184 } Bold event handler UIT-VNUHCM Lập trình mơi trường Windows 124 185 186 // toggle checkmark and toggle italic style 187 private void italicMenuItem_Click( 188 object sender, System.EventArgs e) 189 { 190 // toggle checkmark 191 italicMenuItem.Checked = !italicMenuItem.Checked; 192 193 // use Xor to toggle bold, keep all other styles 194 displayLabel.Font = new Font( 195 displayLabel.Font.FontFamily, 14, 196 displayLabel.Font.Style ^ FontStyle.Italic ); 197 } 198 199 } // end class MenuTest UIT-VNUHCM Lập trình mơi trường Windows 125 UIT-VNUHCM Lập trình mơi trường Windows 126 Ví dụ Mã chương trình C# public class Listing42 : Form { private MainMenu mnuFile = new MainMenu(); private Label lblMessage = new Label(); public Listing42() { this.Text = "Listing 4.2"; this.Menu = mnuFile; MenuItem miFile = mnuFile.MenuItems.Add("&Windows"); miFile.MenuItems.Add(new MenuItem("&Forms")); miFile.MenuItems[0].MenuItems.Add(new MenuItem("&Menus")); miFile.MenuItems[0].MenuItems[0].MenuItems.Add(new MenuItem("&Are")); miFile.MenuItems[0].MenuItems[0].MenuItems.Add(new MenuItem("As")); miFile.MenuItems[0].MenuItems[0].MenuItems[1] MenuItems.Add(new MenuItem("&Easy")); miFile.MenuItems[0].MenuItems[0].MenuItems[1] MenuItems.Add(new MenuItem("As pie")); } UIT-VNUHCM Lập trình mơi trường Windows 127 4.DIALOG UIT-VNUHCM Lập trình mơi trường Windows 128 GIỚI THIỆU Dialog Windows Form đặc biệt dùng để tương tác với người sử dụng cung cấp thông báo Dialog Windows Form đa Dialog Form với thuộc tính FormBorderStyle có giá trị FixedDialog UIT-VNUHCM Lập trình mơi trường Windows 129 GIỚI THIỆU  Mục đích sử dụng Dialog trao đổi thơng tin với người sử dụng  Sau lấy thơng tin, trình xử lý Dialog lấy thơng tin thực cơng việc khác UIT-VNUHCM Lập trình mơi trường Windows 130 PHÂN LOẠI DIALOG MODAL  Phải cung cấp thông tin trước tiếp tục thực chương trình  Dùng cần thu thập thơng tin UIT-VNUHCM MODELESS  Có thể tiếp tục sử dụng chương trình mà khơng cần phản hồi thơng tin Dialog  Dùng đơn thông báo thông tin Lập trình mơi trường Windows 131 SỬ DỤNG DIALOG  MessageBox.Show() TT Kiểu Miêu tả IWin32Window Tham chiếu cửa sổ phát sinh Dialog String Nội dung hiển thị Dialog (bắt buộc) String Nội dung hiển thị đầu đề Dialog MessageBoxButtons Số lượng loại nút có Dialog MessageBoxIcon Biểu tượng hiển thị Dialog MessageBoxDefaultButton Xác định nút mặc định Dialog MessageBoxOption Lựa chọn khác (DefaultDesktopOnly, ServiceNotification, RightAlign, RtlReading) UIT-VNUHCM Lập trình mơi trường Windows 132 SỬ DỤNG DIALOG Thể dialog VS.Net private void button1_Click(object sender, private void button1_Click(object sender, System.EventArgs e) System.EventArgs e) { { OpenFileDialog1.ShowDialog(); OpenFileDialog1.ShowDialog(); } } Thể với thông tin riêng public void PerformCalculations() public void PerformCalculations() { { MessageBox.Show ("The search is now complete", MessageBox.Show ("The search is now complete", "My Application", "My Application", MessageBoxButtons.OKCancel, MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk); MessageBoxIcon.Asterisk); } } UIT-VNUHCM Lập trình mơi trường Windows 133 TẠO MỚI DIALOG Lớp MessageBox đáp ứng hết nhu cầu người sử dụng Tạo Dialog tương tự tạo form Không chứa phương thức Main() UIT-VNUHCM Lập trình mơi trường Windows 134 LẤY THƠNG TIN PHẢN HỒI Sử dụng thuộc tính DialogResult o o o o o o o o UIT-VNUHCM Abort Cancel Ignore No None OK Retry Yes Lập trình mơi trường Windows 135 LẤY THÔNG TIN PHẢN HỒI public void DisplayValue() public void DisplayValue() {{ DialogResult userResponse = openFileDialog1.ShowDialog(); DialogResult userResponse = openFileDialog1.ShowDialog(); {{ if (userResponse == DialogResult.OK) if (userResponse == DialogResult.OK) filePath = openFileDialog1.FileName.ToString(); filePath = openFileDialog1.FileName.ToString(); MessageBox.Show("You successfully opened: '" + filePath + "'", MessageBox.Show("You successfully opened: '" + filePath + "'", "Success", MessageBoxButtons.OK, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information,MessageBoxDefaultButton.Button1); MessageBoxIcon.Information,MessageBoxDefaultButton.Button1); }} … … }} UIT-VNUHCM Lập trình mơi trường Windows 136 CÁC DIALOG PHỔ BiẾN ColorDialog FontDialog OpenFileDialog PageSetupDialog PrintDialog PrintPreviewDialog SaveFileDialog UIT-VNUHCM Lập trình mơi trường Windows 137 Tìm hiểu thêm      ComboBoxes TreeViews ListViews Tab Control Các control khác UIT-VNUHCM Lập trình mơi trường Windows 138 ... nhập: UIT-VNUHCM Lập trình mơi trường Windows Visual Studio Net designer controls Properties, events UIT-VNUHCM Lập trình mơi trường Windows UIT-VNUHCM Lập trình mơi trường Windows Project ... dung Mơ tả đặc tính Window Form Tạo window Form & Cách tạo Menu Các Dialog Các control window form UIT-VNUHCM Lập trình mơi trường Windows 1.FORM UIT-VNUHCM Lập trình mơi trường Windows Giao diện... UIT-VNUHCM Lập trình mơi trường Windows Button “OK” 14 GUI Tree Structure GUI Internal structure Form Button Form containers Panel Panel Label UIT-VNUHCM Button Label Lập trình mơi trường Windows

Ngày đăng: 17/08/2012, 09:52

Hình ảnh liên quan

Hình sau đây là ví dụ về giao diện một cửa sổ đăng nhập: - Lập trình GUI trong môi trường window

Hình sau.

đây là ví dụ về giao diện một cửa sổ đăng nhập: Xem tại trang 6 của tài liệu.
Hình sau đây mô tả một số form control - Lập trình GUI trong môi trường window

Hình sau.

đây mô tả một số form control Xem tại trang 61 của tài liệu.

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan