Lab for Monday, Feb. 16, 1998

 

How to Center A Canvas within a Window on the Screen.

 

By default, Oracle forms will put the top left corner of a canvas in the top left corner of the screen during run time. That can look a bit messy, especially if the canvases in your application are all different sizes (which is usually the case). Your application will look a lot better if all canvases are properly sized and centered on the screen. In order to get this to work, the first thing you should do is change the coordinate system of the form you are working on (note: this should be the first thing you do when starting a new form, even before you define any blocks or canvases). To do so, click on the form name (object) and pull up the property sheet. Find the entry labelled "coordinate system". Click on it, and change the values to "real" and "pixel".

 

Next, create the windows that you need for your form, followed by the canvases and the blocks. Make sure that each canvas has its own window, and each block has its own canvas. You should know how to do that from the other labs. Next, position the items on each canvas just the way you like them. Don’t forget to label items using the text tool. Also add navigation buttons, as was done in the last lab. After you have done all that, resize each canvas so that it encompasses all of the items (you do that by clicking on the edge of the gray area in the canvas layout editor and dragging the box until it encompasses that area of the screen that you like). Close the canvas layout editor, click on the canvas, and pull up the property sheet object. One of the entries in the property sheet is the width and the height of the canvas expressed in pixels (you changed that when you dragged the gray area). Write down the number, and pull up the property sheet of the window that the canvas is based on. This property sheet also has a width and height. Change these to be the same as that of the canvas.

 

Do this for all canvases and windows in your application (you can add blocks, canvases and windows as you go along; you don't have to create them all at once). Now, click on "program units" in the object navigator, and create a new procedure called "Center_Window". In the procedure editor type in and compile the following code:

 

 

--------------------------------------------------------

-- Procedure Center_Window

-- Written by: Andreas M. Olligschlaeger

-- Date Written: October 1996

-- Last Modified

-- Purpose: Generic procedure to center a window.

--------------------------------------------------------

 

PROCEDURE CENTER_WINDOW(P_WINDOW VARCHAR2) IS

 

WID WINDOW;

X NUMBER;

Y NUMBER;

BEGIN

-- get the internal id of the window you want to center

 

WID := FIND_WINDOW(P_WINDOW);

 

-- get the width and height of the window using the

-- get_window_property function

 

Y := GET_WINDOW_PROPERTY(WID,HEIGHT);

X := GET_WINDOW_PROPERTY(WID,WIDTH);

 

-- assuming a screen resolution of 800x600 pixels, and

-- subtracting from that the size of the task bar, calculate the

-- proper position of the window. Adjust the figures for other

-- resolutions

 

X := ROUND((790 - X) / 2);

Y := ROUND((460 - Y) / 2);

 

-- set the correct window position

 

IF (X < 0) THEN

SET_WINDOW_PROPERTY(WID,POSITION,0,Y);

ELSE

SET_WINDOW_PROPERTY(WID,POSITION,X,Y);

END IF;

END;

 

 

You're almost done! In order to center the window for each block, create a when-new-block-instance trigger for each block, and call the above procedure from that trigger. For example, if you have a block called B_BOAT, and the corresponding window is named W_BOAT, insert the following line into the trigger:

 

CENTER_WINDOW('W_BOAT');

 

When you navigate out of a block, you can hide the block by calling the HIDE_WINDOW procedure, which is predefined. For example, if you have a done button on your C_BOAT canvas which navigates to another block, add the following line to the when-button-pressed trigger for the button:

 

HIDE_WINDOW('W_BOAT');

 

Return to Main Page