|
App Part 1: Creating controls at runtimeExample: app_one I thought that since an example on creating controls on the fly, although usefull, would be quite pointless unless the application actually did something, so in this entry I will start the workings of a text editor and build upon it untill we reach a nearly useful program that supports opening, editing and saving text documents. The first step, which this particular page covers will be simply creating the window and the EDIT control that will serve as the center of our program.
Starting with the skeleton code from the Simple Window application we add a CONST IDC_MAIN_EDIT = 101; | WM_CREATE : hEdit := CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD BOR WS_VISIBLE BOR WS_VSCROLL BOR WS_HSCROLL BOR ES_MULTILINE BOR ES_AUTOVSCROLL BOR ES_AUTOHSCROLL, 0, 0, 100, 100, hwnd, CAST(HMENU,IDC_MAIN_EDIT), Instance, NIL); IF hEdit=NIL THEN MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK BOR MB_ICONERROR); PostQuitMessage(0); ELSE hfDefault := GetStockFont(DEFAULT_GUI_FONT); SendMessage(hEdit, WM_SETFONT, CAST(WPARAM,hfDefault),0); END; | WM_SIZE : FUNC GetClientRect(hwnd, rcClient); hEdit := GetDlgItem(hwnd, IDC_MAIN_EDIT); SetWindowPos(hEdit, NIL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER); Creating controlsCreating controls, like creating any other window, is done through the hEdit := CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD BOR WS_VISIBLE BOR WS_VSCROLL BOR WS_HSCROLL BOR ES_MULTILINE BOR ES_AUTOVSCROLL BOR ES_AUTOHSCROLL, 0, 0, 100, 100, hwnd, CAST(HMENU,IDC_MAIN_EDIT), Instance, NIL); IF hEdit=NIL THEN MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK BOR MB_ICONERROR); You can see that this call to
The 3 styles that are specific to EDIT controls (
The regular (
We have specified our window handle as the parent of the control, and assigned it an ID of Sizing of dynamically created controlsGenerally if your window is sizeable you'll want some code to resize or reposition the controls you created within it so that they are always layed out properly. FUNC GetClientRect(hwnd, rcClient); hEdit := GetDlgItem(hwnd, IDC_MAIN_EDIT); SetWindowPos(hEdit, NIL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
Since we only have one control for now, the task is relatively simple. We use
Next we simply get a handle to our edit control using Creating other controls at runtimeI'm not going to give examples of dynamically creating the other controls like LISTBOX, BUTTON, etc... because it's basically the same and it gets kinda boring after a while :) If you follow the links into MSDN above, or look in your local Win32 API reference you will be able to find all of the information needed to create any of the other standard controls. We'll be doing more of this with the common controls in the next couple of sections so you'll get more practice eventually.
Copyright © 1998-2011, Brook Miles. All rights reserved. Adapted for Modula-2 by Frank Schoonjans, with permission. |