Dprogramming.com - The D Programming Language [archived content]
Directory

Home
News
Wiki
Entice Designer
DCode editor
Linkdef
bintod
Tutorial
D FAQ
Code
    bintod
    DFL GUI
    D irclib IRC
    fileprompt
    ini files
    Linkdef
    list linked list
    mtext
    Splat sockets
    trayicon
    wildcard
Contact
Paste
Links

dwindows

Note: This module is no longer being updated. It was only made to help me make Windows GUI applications, it was not a serious project. You should use my new windowing library, DFL!

This is a library I started working on to simplify the Win32 API. I tried to keep it similar to the API, so it's not a whole new world that won't help you at anything else. Download it here. Click here for an example using this module. Now I'll give an explanation of what is in this module:

Window

A class called Window is the base of this module. With this class, you can easily register a window class, superclass an existing class, subclass a window, create a window, as well as manipulate a window with ease.

Window.registerClass

This static function is for registering a window class for use with this class only (I know it's pretty confusing talking about two different types of classes at the same time, please bear with me). This function takes a parameter of type WNDCLASSEX but does not need all of its members filled. Optionally, this function can take a parameter of type WndClassFlags, which is an enum of values that can be OR'ed together indicating which members of WNDCLASSEX are valid, the rest get default values (WndClassFlags.NONE may be used by itself). Members lpszClassName and hInstance of WNDCLASSEX are always required, member cbSize is never used. Here is an example:
WNDCLASSEX wce;
wce.lpszClassName = "hello_dwindows";
wce.hInstance = hInstance;
wce.hIcon = wce.hIconSm = LoadIcon(hInstance, IDR_ICON1);
wce.style = CS_OWNDC;
Window.registerClass(&wce, WndClassFlags.ICONS | WndClassFlags.STYLE);
This function throws an RegisterWindowException exception on failure, which is derived from WindowException.

Window.create

This member function is almost the same as CreateWindowEx:
void create(
  DWORD exStyle,
  char[] className,
  char[] windowName,
  DWORD style,
  int x,
  int y,
  int width,
  int height,
  HWND hwParent,
  HMENU hmenu,
  HINSTANCE hinstance
  )
Here is an example:
Window w = new Window;
w.create(0, "hello_dwindows", "Hi!",
  WS_VISIBLE | WS_OVERLAPPEDWINDOW,
  0, 0, 300, 300,
  null, null, hInstance);
This function throws an WindowException exception on failure.

Window.windowProc

This member function is called for all window messages. It is intended to be overridden by a derived class, and super.windowProc called and returned if the message isn't processed. Here is an example class:
class FunWindow: Window
{
  LRESULT windowProc(UINT msg, WPARAM wparam, LPARAM lparam)
  {
    switch(msg)
    {
      //process messages here
      
      default:
        return super.windowProc(msg, wparam, lparam);
    }
    return 0;
  }
}

Window.subClass

This member function will subclass an existing window. It takes a HWND parameter and throws an WindowException excepton on failure. You should not subclass a window created by another process. Instead of calling super.windowProc when a message isn't handled inside your windowProc function, you should call and return prevWindowProc to call the previous window procedure:
LRESULT windowProc(UINT msg, WPARAM wparam, LPARAM lparam)
{
  switch(msg)
  {
    //process messages here
    
    default:
      return prevWindowProc(msg, wparam, lparam);
  }
  return 0;
}
Alternately, you can use the WindowSubClass class and use windowProc as normal.

Window members

There are several other member functions of Window, such as:
hwnd returns the window handle,
bind bind the object to the specified HWND,
unbind unbind the current window from this object,
isSubClass returns true if this object is subclassing a window,
text to get and set the window text,
show will show the window, or optionally take a parameter ShowWindow takes (one of the SW_* constants),
hide to hide the window,
destroy to destroy it,
minimize to minimize,
maximize to maximize,
resore to restore,
display to show a window if it's hidden, restore if minimized, and activate if not active,
item takes a control identifier for a child window, and returns its HWND, or null if the window cannot be found.
enable to enable the window, or optionally take a bit parameter: true to enable, false to disable,
disable to disable it,
style to get or set the window style,
exStyle to get or set the window extended style,
parent get or set the parent window (when setting, the parent and child windows must belong to the same application)
id returns the control identifier,
rect takes a RECT parameter and fills it with the window coordinates,
clientRect takes a RECT parameter and fills it with the client coordinates,
parentArea takes a RECT parameter and fills it with the parent's coordinates relative to the current window,
center centers the window in the parent's window, or the working desktop area if no parent,
alignment takes a parameter of OR'ed values of the enum Align, to align the window in the parent's window, or the working desktop area if no parent,
icon to get or set the window icon,
font to get or set the current font,
redraw takes a bit parameter indicating if the window can be redrawn,
invalidate optionally takes a RECT or HRGN parameter, or null to invalidate the entire window,
validate optionally takes a RECT or HRGN parameter, or null to validate the entire window.

Button

This is a derived class of Window, specifically designed for the windows standard button. This class has a somewhat simplified create member function:
void create(
  DWORD exStyle,
  char[] text,
  DWORD style,
  int x,
  int y,
  int width,
  int height,
  HWND hwParent,
  int ctrlId
  )
This class does not process the windowProc member, although you may subclass itself and it will:
Button button = new Button;
button.create(0, "Click me",
  WS_CHILD | WS_VISIBLE,
  0, 0, 70, 30,
  w.hwnd(), ID_CLICKME);
button.subClass(button.hwnd());
This class has several members for simplifying buttons:
click will simulate a click event,
check to get or set the button's check state (one of the BST_CHECKED, BST_INTERMEDIATE or BST_UNCHECKED constants),
image to set the image, the first parameter is either IMAGE_BITMAP or IMAGE_ICON and the second is a handle to the image,
state to set the highlight state, takes a bit parameter, set to true to highlight,
buttonStyle to set the button style (not to confuse with Window.style).

Edit

This is a derived class of Window, specifically designed for the windows standard edit box. This class has a somewhat simplified create member function:
void create(
  DWORD exStyle,
  char[] text,
  DWORD style,
  int x,
  int y,
  int width,
  int height,
  HWND hwParent,
  int ctrlId
  )
This class does not process the windowProc member, although you may subclass itself and it will (see Button for similar example). This class has several members for simplifying edit boxes:
canUndo returns a bit, true if something can be undone,
charFromPos takes two parameters: (short x, short y), returns the character index at the specified point, or -1,
emptyUndoBuffer clears the undo buffer,
limit to get or set the text length limit,
lineFromChar takes a character index parameter, returns the line index,
lineIndex takes a line index parameter, returns the index of the first character on the line,
lineLength takes a character index parameter, returns the length of that line,
line takes a character index parameter, returns a dynamic char array of the text on the line,
posFromChar takes a pointer to a POINT structure parameter that receives the client coordinates of the character index supplied in the second parameter,
replaceSel replaces the selected text, the first parameter is a bit value, true indicates that the replacement can be undone, the second parameter is a dynamic character array that will replace the current selection,
modify to get or set the current modify flag (when setting, takes a bit parameter, true to set as modified),
setSel sets the beginning of the selection to the first parameter, and the end to the second parameter,
getSel takes two parameters that are pointers to int, the first receives the start character index of selection, second receives the end.
Copyright © 2004-2008 Christopher E. Miller