Owner draw buttons allow the application to control the appearance of the window whilst the button still controls the keyboard and mouse interaction. The example of this section provides two owner draw buttons. A snapshot of the application in action is shown below.
As may be observed, the buttons are centered vertically in the window and are distributed evenly along a horizontal centering line. The left most button decreases the size of the containing window by 10%; whereas, the right button increases the size of the window by 10%. Most owner draw buttons contain bitmaps; however, this application merely draws the buttons as a number of triangles (pointing inwards or outwards).
The two buttons handles are contained in the window data structure as shown below.
struct window_data { handle smaller, larger; int width_of_client, height_of_client, width_of_character, height_of_character; };
The client window procedure initializes these variables upon receiving the message message::create.
case message::create: .... width_of_character = low_part(get_dialog_base_units()); height_of_character = high_part(get_dialog_base_units()); data->smaller = create_window("BUTTON", style::child | style::visible | button_style::owner_draw, button_identity::smaller, window_handle); data->larger = create_window("BUTTON", style::child | style::visible | button_style::owner_draw, button_identity::larger, window_handle); break;
Upon receiving a size message, the button windows are positioned and scaled as follows.
case message::size: width_of_client = low_part(parameter2); height_of_client = high_part(parameter2); move_window(data->smaller, data->width_of_client/2 - 3*button_dimensions::width*data->width_of_character/2, data->height_of_client/2 - button_dimensions::height*data->height_of_character/2, button_dimensions::width*data->width_of_character, button_dimensions::height*data->height_of_character,true); move_window(larger, width_of_client/2 + button_dimensions::width*data->width_of_character/2, height_of_client/2 - button_dimensions::height*data->height_of_character/2, button_dimensions::width*data->width_of_character, button_dimensions::height*data->height_of_character,true); break;
When selected, the buttons notify the owner (being the client window) of the event using command messages. The code that handles these notifications is shown below.
case message::command: { irectangle rectangle_window; get_window_rectangle(window_handle,&rectangle_window); switch (parameter1) // Make the rectangle 10% smaller or larger { case button_identity::smaller: rectangle_window[0](0) += data->width_of_client/20; rectangle_window[1](0) -= data->width_of_client/20; rectangle_window[0](1) += data->height_of_client/20; rectangle_window[1](1) -= data->height_of_client/20; break; case button_identity::larger: rectangle_window[0](0) -= data->width_of_client/20; rectangle_window[1](0) += data->width_of_client/20; rectangle_window[0](1) -= data->height_of_client/20; rectangle_window[1](1) += data->height_of_client/20; break; } move_window(window_handle, rectangle_window[0](0), rectangle_window[0](1), rectangle_window[1](0)-rectangle_window[0](0), rectangle_window[1](1)-rectangle_window[0](1),true); } break;
The rectangle of the client window is made 10% smaller or 10% bigger as requested (based upon the identity of the selected control window). If this was all the code that was presented, the program would be completely functional, but the buttons would not be visible. The code shown below draws the buttons upon receiving the notification message::draw_item.
case message::draw_item: { draw_item* item = (draw_item*)parameter2; // fill area with white and frame it black fill_rectangle(item->device, &item->bounds, get_standard_object(standard_brush::White)); frame_rectangle(item->device, &item->bounds, get_standard_object(standard_brush::Black)); // draw inward and outward black triangles int width = item->bounds[1](0) - item->bounds[0](0); int height = item->bounds[1](1) - item->bounds[0](1) ; point points[3]; switch (item->control) { case button_identity::Smaller: points[0](0) = 3 * width / 8; points[0](1) = 1 * height / 8; points[1](0) = 5 * width / 8; points[1](1) = 1 * height / 8; points[2](0) = 4 * width / 8; points[2](1) = 3 * height / 8; triangle(item->device,points); points[0](0) = 7 * width / 8; points[0](1) = 3 * height / 8; points[1](0) = 7 * width / 8; points[1](1) = 5 * height / 8; points[2](0) = 5 * width / 8; points[2](1) = 4 * height / 8; triangle(item->device,points); points[0](0) = 5 * width / 8; points[0](1) = 7 * height / 8; points[1](0) = 3 * width / 8; points[1](1) = 7 * height / 8; points[2](0) = 4 * width / 8; points[2](1) = 5 * height / 8; triangle(item->device,points); points[0](0) = 1 * width / 8; points[0](1) = 5 * height / 8; points[1](0) = 1 * width / 8; points[1](1) = 3 * height / 8; points[2](0) = 3 * width / 8; points[2](1) = 4 * height / 8; triangle(item->device,points); break; case button_identity::Larger: points[0](0) = 5 * width / 8; points[0](1) = 3 * height / 8; points[1](0) = 3 * width / 8; points[1](1) = 3 * height / 8; points[2](0) = 4 * width / 8; points[2](1) = 1 * height / 8; triangle(item->device,points); points[0](0) = 5 * width / 8; points[0](1) = 5 * height / 8; points[1](0) = 5 * width / 8; points[1](1) = 3 * height / 8; points[2](0) = 7 * width / 8; points[2](1) = 4 * height / 8; triangle(item->device,points); points[0](0) = 3 * width / 8; points[0](1) = 5 * height / 8; points[1](0) = 5 * width / 8; points[1](1) = 5 * height / 8; points[2](0) = 4 * width / 8; points[2](1) = 7 * height / 8; triangle(item->device,points); points[0](0) = 3 * width / 8; points[0](1) = 3 * height / 8; points[1](0) = 3 * width / 8; points[1](1) = 5 * height / 8; points[2](0) = 1 * width / 8; points[2](1) = 4 * height / 8; triangle(item->device,points); break; } // Invert the rectangle if the button is selected if (item->State & owner_draw_state::Selected) invert_rectangle(item->device,&item->bounds); // Draw a Focus rectangle if the button has the Focus if (item->State & owner_draw_state::Focus) { item->bounds[0](0) += width / 16; item->bounds[0](1) += height / 16; item->bounds[1](0) -= width / 16; item->bounds[1](1) -= height / 16; draw_focus_rectangle(item->device,&item->bounds); } } break;
The device context for drawing is found in an object of the class draw_item. The button is filled with a white background and framed with a black border. The identity of the control is then used to determine whether the inwards or outwards pointing triangles are drawn. The function that actually draws the triangles is shown below.
void triangle(handle device_context, point points[]) { select_object(device_context,get_standard_object(standard_brush::Black)); draw_polygon(device_context,points,3); select_object(device_context,get_standard_object(standard_brush::White)); }
after the triangles are drawn, the state of the button is tested and the button is color inverted if it is selected. The state is also tested to see whether the button has the focus. When the button has the focus, a focus rectangle is drawn.