11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6. Dialog Box<br />

Figure 6-5. All controls have transparent background, but their text doesn’t<br />

We can call function CDC::SetBkMode(…) and use TRANSPARENT flag to set transparent background<br />

drawing mode for text, otherwise it will be drawn with the default background color.<br />

The background of a 3-D looking pushdown button can not be changed this way. Also, if we include<br />

drop down or drop list combo box, the background color of its list box will not be customized by this<br />

method because it is not created as the child window of the dialog box. To modify it, we need to derive new<br />

class from CComboBox and override its OnCtlColor(…) member function.<br />

6.6 Resizing the Form View<br />

Form view is very similar to a dialog box. Usually we create form view from a dialog box template,<br />

which can contain all the standard common controls. While they are similar, a form view is usually created<br />

with a document/view structure, and has some properties that a standard dialog box lacks. For example, a<br />

form view will be automatically implemented with scroll bars. If the window size becomes smaller than the<br />

size of the dialog template, scroll bars will automatically be activated. They can be scrolled to allow the<br />

user to see the hidden part of the dialog.<br />

Since a form view is usually resizable, we sometimes need to move and resize the common controls<br />

contained in the form view to make its appearance well balanced. For example, if we have an edit box<br />

embedded in the form view, instead of fixing its size, we may want to adjust it dynamically according to<br />

the dimension of the form view. This is usually a desired feature of form view because it will make the<br />

controls and the window well balanced.<br />

Coordinates Conversion<br />

Every window can be moved and resized by calling function CWnd::MoveWindow(…) or CWnd::<br />

SetWindowPos(…). Also, a window’s size and position can be retrieved by calling function CWnd::<br />

GetClientRect(…) and CWnd::GetWindowRect(…). The points retrieved using the former function are<br />

measured in the client window’s coordinate system, and the points retrieved from the latter function are<br />

measured in the screen (desktop) coordinate system. To convert coordinates from one system to another,<br />

we can call function CWnd::MapWindowPoints(…) or CWnd::ScreenToClient(…).<br />

For example, if there are two windows: window A and window B, which are attached two CWnd type<br />

variables wndA and wndB. If we want to know the size and position of window A measured in window B’s<br />

coordinate system, we can first obtain the size and position of window A in its local coordinate system, and<br />

then convert them to window B system as follows:<br />

wndA.GetClientRect(rect);<br />

wndA.MapWindowPoints(&wndB, rect);<br />

Or we can find the position and size of window A in the screen coordinate system, and call CWnd::<br />

ScreenToClient(…) to convert them to window B’s coordinate system:<br />

154

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!