Changing Colours of a dialog

10 340 0
Changing Colours of a dialog

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

Thông tin tài liệu

Changing Colours of a dialog

Benina’s TutASM # 2: Changing Colours of a Dialog – Ver 1.0Author: BeninaBenina’ s Tut ASM # 2 Changing Colours of a DialogAuthor: BeninaTut này tôi sẽ cùng bạn khám phá cách thay đổi màu sắc giao diện của một chương trình dùng Dialog làm cửa sổ chính.Đây là Demo hòan khi hòan tất tut này:Các bạn có thể download Source theo link:http://h1.ripway.com/benina/files/Keygen 2.rar A. Lý thuyết: Windows gởi các thông điệp rất đa dạng liên quan đến màu sắc đến thủ tục dialog của bạn, và bằng cách xử lý các thông điệp này bạn có thể thay đổi màu sắc nào mà bạn muốn hiển thị.Cho ví dụ, để thay đổi màu sắc của chính dialog box, bạn có thể xử lý thông điệp WM_CTLCOLORDLG, để thay đổi màu sắc cho điều khiển static control , thì bạn xử lý thông điệp WM_CTLCOLORSTATIC và vân vân.Sau đây tôi xin liệt kê một số thông điệp liên quan, bạn tra cứu Win32 API Reference để biết thêm chi tiết :WM_CTLCOLORDLG, WM_CTLCOLOREDIT, WM_CTLCOLORLISTBOX, WM_CTLCOLORMSGBOX, WM_CTLCOLORSCROLLBAR, WM_CTLCOLORSTATIC, WM_CTLCOLORBTNTrước tiên bạn cài đặt một Brush dùng để paint nền background và chứa nó trong 1 biến để dùng sau này. Thông điệp WM_CTLCOLORDLG và các thông điệp liên quan thường được gọi trong suốt chương trình của bạn , và nếu bạn cài đặt một brush mới mỗi lần thông điệp được send thì Trang 1/10 Benina’s TutASM # 2: Changing Colours of a Dialog – Ver 1.0Author: Beninacuối cùng bạn sẽ truy cập RAM rất nhiều với các brush die cũng nhiều vô số kể. Vì vậy với cách cài đặt 1 brush lưu nó trong 1 biến, bạn sẽ dễ dàng điều khiển hơn, và chúng ta có thể xóa nó khi dialog bị hủy và khi chúng ta đã biết chúng ta ko dùng nó nữa.Hàm cài đặt 1 brush là hàm sau và lưu nó vào 1 biến:(Các bạn tập tham khảo các hàm API bằng tiếng Anh cho quen, vì chắc chắn rất tiện lợi cho bạn sau này)The CreateSolidBrush function creates a logical brush that has the specified solid color. HBRUSH CreateSolidBrush( COLORREF crColor // brush color value ); ParameterscrColorSpecifies the color of the brush. Return ValuesIf the function succeeds, the return value identifies a logical brush.If the function fails, the return value is NULL. RemarksA solid brush is a bitmap that Windows uses to paint the interiors of filled shapes. After an application creates a brush by calling CreateSolidBrush, it can select that brush into any device context by calling the SelectObject function.Trong phần xử lý thông điệp WM_CTLCOLORDLG ta cho giá trị trả về là brush mà ta đã cài đặt.Trong phần xử lý các thông điệp liên quan như WM_CTLCOLORSTATIC vân vân, ta co thể dùng SetBkMode, SetBkColor, SetTextColor để thay đổi màu sắc của control mà thông điệp chịu trách nhiệm.Các hàm này có ý nghĩa sau:Chức năng của hàm SetBkMode là set một kiểu pha trộn bkground của DC ta chỉ định. Kiểu pha trộn Bkground (background mix mode) được sử dụng với texts, hatched brushes và các lọai pen ko phải để vẽ line “đặc” (solid). Nó tác dụng đến các lọai line vẽ- sử dụng một pen bởi hàm cài đặt là CreatePen . SetBkMode ko gây ảnh hưởng đến lines sử dụng hàm cài đặt ExtCreatePen.Chức năng của hàm SetBkColor là set một bkground color hiện hành hay là màu tự nhiên gần nhất nếu device ko miêu tả giá trị màu được chỉ định. Hàm này lắp các kẻ hở giữa các lines vẽ được định mẫu sử dụng hàm CreatePen để cài đặt.Hàm SetTextColor có chức năng set màu text .Trang 2/10 Benina’s TutASM # 2: Changing Colours of a Dialog – Ver 1.0Author: BeninaCác bạn có thể tham khảo các hàm này trong phần phụ lục của tut.-Và cuối cùng khi thóat chương trình chúng ta đừng quên free brush của chúng ta đã cài đặt ra khỏi memory bằng hàm DeleteObjectB. Thực hành: Trong MASM ta thực hiện theo các bước sau:Các bạn hãy thực hiện tiếp tục project mà ta đã làm ở tutorial “Benina’ Tut # 1”.Hảy Open project Keygen1 trong Tut#1.1. Bước 1: Trước tiên ta include thư viện để sử dụng các hàm API đồ họa trong file Keygen1.Inc: include gdi32.incincludelib gdi32.libKế đến khai báo các biến và hằng color trong file Keygen1.Inc:Trong section .CONST khai báo hằng số màu color ; ------------------------------------------------------------------------------; Colors; ------------------------------------------------------------------------------CR_BACKGROUND equ 00333300hCR_TEXT equ 00CCCC99hTrong section .DATA ?Khai báo biến lưu Brush cài đặthBgColor HBRUSH ?2. Bước 2 : Trong file Keygen1.Asm , phần section .CODE trước lời gọi hàm DialogBoxParam ta add lệnh sau để cài đặt Brush:INVOKE CreateSolidBrush, CR_BACKGROUNDmov hBgColor, eaxSau đó trong vòng lặp xử lý thông điệp ta add các lệnh:Trang 3/10 Benina’s TutASM # 2: Changing Colours of a Dialog – Ver 1.0Author: Benina .ELSEIF uMsg == WM_CTLCOLORDLG mov eax, hBgColor ret .ELSEIF uMsg == WM_CTLCOLORSTATIC INVOKE SetBkMode, wParam, TRANSPARENT INVOKE SetTextColor, wParam, CR_TEXT Invoke SetBkColor,wParam,CR_BACKGROUND mov eax, hBgColorret .ELSEIF uMsg == WM_CTLCOLOREDIT INVOKE SetBkMode, wParam, TRANSPARENT INVOKE SetTextColor, wParam, CR_TEXT Invoke SetBkColor,wParam,CR_BACKGROUND mov eax, hBgColor retGiải thích đôi chút: -Ta biết thông điệp trả về giá trị trong eax, nên ta có các lệnh mov eax, hBgColorret-Chú ý dòng lệnh Set Background Mode (hàm SetBkMode) tham số thứ 2 là TRANSPARENT . Nếu ko có dòng này thì Bkground sẽ được fill với brush mà bạn chỉ định , nhưng khi control vẽ text nó sẽ vẽ đè lên với default background color. Vì vậy nếu ta setting với TRANSPARENT thì sẽ fix được vấn đề này.-Thay đổi màu của các control khác cũng tương tự. Bạn tìm các thông điệp có dạng sau : WM_CTLCOLOR* message trong Win32 reference để tham khảo thêm. Chú ý rằng, một edit control sẽ gởi thông điệp WM_CTLCOLORSTATIC nếu nó chỉ đọc (read only) và nếu khác thì nó sẽ send thông điệp WM_CTLCOLOREDIT.-Nếu bạn có nhiều hơn một static control hay nhiều các control khác mà bạn muốn thay đổi màu sắc của chúng khác nhau cho từng control, bạn cần check ID của control từ thông điệp ta đang xử lý và thay đổi màu dựa trên ID đó. Bạn đã chuyển Hwnd của controls trong lParam, và bạn có thể lấy ID của control bằng cách dùng hàm GetDlgCtlrID(). Chú ý rằng static control được cho bởi ID mặc định của IDC_STATIC (-1) bởi resource editor, vì vậy nếu bạn muốn có thể nói với chúng phần bạn sẽ cần chỉ định chúng Ids mới. Phần này các bạn tự tìm hiểu và viết code nhé.3. Bước 3: Cuối cùng chúng ta cần free Object Brush ra khỏi bộ nhớ. Trước lệnh ExitProcess và sau lệnh DialogBoxParam trong file Keygen1.Asm ta add thêm lệnh:Trang 4/10 Benina’s TutASM # 2: Changing Colours of a Dialog – Ver 1.0Author: BeninaINVOKE DeleteObject, hBgColorĐây là hình ảnh sau khi thực hiện trong file Keygen1.IncĐây là hình ảnh sau khi thực hiện trong file Keygen.AsmTrang 5/10 Benina’s TutASM # 2: Changing Colours of a Dialog – Ver 1.0Author: BeninaTrang 6/10 Benina’s TutASM # 2: Changing Colours of a Dialog – Ver 1.0Author: BeninaSau khi thực hiện xong ta Hit nút “GO” xem sau nhé. OK rồi chứ!C. Phụ lục: Tham khảo các hàm API:1. SetBkMode : The SetBkMode function sets the background mix mode of the specified device context. The background mix mode is used with text, hatched brushes, and pen styles that are not solid lines. int SetBkMode( HDC hdc, // handle of device context int iBkMode // flag specifying background mode ); ParametersTrang 7/10 Benina’s TutASM # 2: Changing Colours of a Dialog – Ver 1.0Author: BeninahdcIdentifies the device context. iBkModeSpecifies the background mode. This parameter can be either of the following values: Value DescriptionOPAQUE Background is filled with the current background color before the text, hatched brush, or pen is drawn.TRANSPARENT Background remains untouched. Return ValuesIf the function succeeds, the return value specifies the previous background mode.If the function fails, the return value is zero. RemarksThe SetBkMode function affects the line styles for lines drawn using a pen created by the CreatePen function. SetBkMode does not affect lines drawn using a pen created by the ExtCreatePen function. The iBkMode parameter can also be set to driver-specific values. GDI passes such values to the device driver and otherwise ignores them.2. SetBkColor The SetBkColor function sets the current background color to the specified color value, or to the nearest physical color if the device cannot represent the specified color value. COLORREF SetBkColor( HDC hdc, // handle of device context COLORREF crColor // background color value ); ParametershdcTrang 8/10 Benina’s TutASM # 2: Changing Colours of a Dialog – Ver 1.0Author: BeninaIdentifies the device context. crColorSpecifies the new background color. Return ValuesIf the function succeeds, the return value specifies the previous background color as a COLORREF value. If the function fails, the return value is CLR_INVALID. RemarksThis function fills the gaps between styled lines drawn using a pen created by the CreatePen function; it does not fill the gaps between styled lines drawn using a pen created by the ExtCreatePen function. If the background mode is OPAQUE, the background color is used to fill gaps between styled lines, gaps between hatched lines in brushes, and character cells. The background color is also used when converting bitmaps from color to monochrome and vice versa.3. SetTextColor: The SetTextColor function sets the text color for the specified device context to the specified color. COLORREF SetTextColor( HDC hdc, // handle of device context COLORREF crColor // text color ); ParametershdcIdentifies the device context. crColorSpecifies the color of the text. Trang 9/10 Benina’s TutASM # 2: Changing Colours of a Dialog – Ver 1.0Author: Benina Return ValuesIf the function succeeds, the return value is a color reference for the previous text color.If the function fails, the return value is CLR_INVALID. To get extended error information, call GetLastError. RemarksThe text color is used to draw the face of each character written by the TextOut and ExtTextOut functions. The text color is also used in converting bitmaps from color to monochrome and vice versa.Hy vọng các bạn hài lòng. Các bạn cần nghiên cứu thêm để Template của ta ngày càng đẹp.Các bạn có thể download Source theo link:http://h1.ripway.com/benina/files/Keygen 2.rar Benina 27/12/2005Update 27/12/2005Mail: benina@walla.com(Không đồng ý bất kỳ ai sử dụng tài liệu này cho mục đích thương mại nếu ko được phép của người dịch)Trang 10/10 . Benina’s TutASM # 2: Changing Colours of a Dialog – Ver 1.0Author: BeninaBenina’ s Tut ASM # 2 Changing Colours of a DialogAuthor: BeninaTut này. năng set màu text .Trang 2/10 Benina’s TutASM # 2: Changing Colours of a Dialog – Ver 1.0Author: BeninaCác bạn có thể tham khảo các hàm này trong

Ngày đăng: 10/11/2012, 14:15

Từ khóa liên quan

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

  • Đang cập nhật ...

Tài liệu liên quan