Reference: http://www.eclipse.org/swt/html
http://www.functionx.com/win32/Lesson01.htmshell
http://www.win32developer.com/tutorial/windows/windows_tutorial_2.shtmwindows
http://download.ourdev.cn/bbs_upload782111/files_35/ourdev_606687D1GBNL.pdfapp
http://msdn.microsoft.com/en-us/library/windows/desktop/ff381399%28v=vs.85%29.aspxeclipse
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/WinPanel/Introduction.html#//apple_ref/doc/uid/10000031-SW1async
Implementers of Drawable can have a graphics context (GC) created for them, and then they can be drawn on by sending messages to their associated GC. SWT images, and device objects such as the Display device and the Printer device, are drawables.Invokes platform specific functionality to dispose a GC handle or to allocate a new GC handle.ide
1 public interface Drawable { 2 public int /*long*/ internal_new_GC (GCData data); 3 public void internal_dispose_GC (int /*long*/ handle, GCData data); 4 }
1 public int /*long*/ hPalette = 0; 2 Font systemFont; 3 protected static Device CurrentDevice; 4 protected static Runnable DeviceFinder; 5 static { 6 try 7 { 8 Class.forName ("org.eclipse.swt.widgets.Display"); //$NON-NLS-1$ 9 } 10 catch (ClassNotFoundException e) 11 {} 12 }
1 public static final native int SetPixel (int /*long*/ hdc, int X, int Y, int crColor); 2 public static final native int SetPolyFillMode (int /*long*/ hdc, int iPolyFillMode); 3 public static final native boolean SetProcessDPIAware (); 4 public static final native boolean SetRect (RECT lprc, int xLeft, int yTop, int xRight, int yBottom); 5 public static final native boolean SetRectRgn (int /*long*/ hrgn, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); 6 public static final native int SetROP2 (int /*long*/ hdc, int fnDrawMode); 7 public static final native boolean SetScrollInfo (int /*long*/ hwnd, int flags, SCROLLINFO info, boolean fRedraw); 8 public static final native int SetStretchBltMode (int /*long*/ hdc, int iStretchMode); 9 /** 10 * @param hWnd cast=(HWND) 11 * @param lpString cast=(LPCWSTR) 12 * @param hData cast=(HANDLE) 13 */ 14 public static final native boolean SetPropW (int /*long*/ hWnd, int /*long*/ lpString, int /*long*/ hData); 15 /** 16 * @param hWnd cast=(HWND) 17 * @param lpString cast=(LPCTSTR) 18 * @param hData cast=(HANDLE) 19 */ 20 public static final native boolean SetPropA (int /*long*/ hWnd, int /*long*/ lpString, int /*long*/ hData); 21 /** @param hdc cast=(HDC) */ 22 public static final native int SetTextAlign (int /*long*/ hdc, int fMode); 23 /** 24 * @param hdc cast=(HDC) 25 * @param colorRef cast=(COLORREF) 26 */ 27 public static final native int SetTextColor (int /*long*/ hdc, int colorRef); 28 /** 29 * @param hWnd cast=(HWND) 30 * @param lpTimerFunc cast=(TIMERPROC) 31 */ 32 public static final native int /*long*/ SetTimer (int /*long*/ hWnd, int /*long*/ nIDEvent, int Elapse, int /*long*/ lpTimerFunc); 33 /** @param hdc cast=(HDC) */ 34 public static final native boolean SetViewportExtEx (int /*long*/ hdc, int nXExtent, int nYExtent, SIZE lpSize); 35 /** @param hdc cast=(HDC) */ 36 public static final native boolean SetViewportOrgEx (int /*long*/ hdc, int X, int Y, POINT lpPoint); 37 /** @param hWnd cast=(HWND) */ 38 public static final native int SetWindowLongW (int /*long*/ hWnd, int nIndex, int dwNewLong); 39 /** @param hWnd cast=(HWND) */ 40 public static final native int SetWindowLongA (int /*long*/ hWnd, int nIndex, int dwNewLong); 41 /** 42 * @param hWnd cast=(HWND) 43 * @param dwNewLong cast=(LONG_PTR) 44 */ 45 public static final native int /*long*/ SetWindowLongPtrW (int /*long*/ hWnd, int nIndex, int /*long*/ dwNewLong); 46 /** 47 * @param hWnd cast=(HWND) 48 * @param dwNewLong cast=(LONG_PTR) 49 */ 50 public static final native int /*long*/ SetWindowLongPtrA (int /*long*/ hWnd, int nIndex, int /*long*/ dwNewLong); 51 /** @param hdc cast=(HDC) */ 52 public static final native boolean SetWindowExtEx (int /*long*/ hdc, int nXExtent, int nYExtent, SIZE lpSize); 53 /** @param hdc cast=(HDC) */ 54 public static final native boolean SetWindowOrgEx (int /*long*/ hdc, int X, int Y, POINT lpPoint); 55 /** @param hWnd cast=(HWND) */ 56 public static final native boolean SetWindowPlacement (int /*long*/ hWnd, WINDOWPLACEMENT lpwndpl); 57 /** 58 * @param hWnd cast=(HWND) 59 * @param hWndInsertAfter cast=(HWND) 60 */ 61 public static final native boolean SetWindowPos(int /*long*/ hWnd, int /*long*/ hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); 62 /** 63 * @param hWnd cast=(HWND) 64 * @param hRgn cast=(HRGN) 65 */ 66 public static final native int SetWindowRgn (int /*long*/ hWnd, int /*long*/ hRgn, boolean bRedraw); 67 /** 68 * @param hWnd cast=(HWND) 69 * @param lpString cast=(LPWSTR) 70 */ 71 public static final native boolean SetWindowTextW (int /*long*/ hWnd, char [] lpString); 72 /** 73 * @param hWnd cast=(HWND) 74 * @param lpString cast=(LPSTR) 75 */ 76 public static final native boolean SetWindowTextA (int /*long*/ hWnd, byte [] lpString);
1 public Device() { 2 this(null); 3 } 4 public Device(DeviceData data) { 5 synchronized (Device.class) { 6 if (data != null) { 7 debug = data.debug; 8 tracking = data.tracking; 9 } 10 if (tracking) { 11 errors = new Error [128]; 12 objects = new Object [128]; 13 trackingLock = new Object (); 14 } 15 create (data); 16 init (); 17 } 18 } 19 protected void create (DeviceData data) { 20 } 21 protected void init () { 22 if (debug) { 23 if (!OS.IsWinCE) OS.GdiSetBatchLimit(1); 24 } 25 26 /* Initialize the system font slot */ 27 systemFont = getSystemFont(); 28 29 /* Initialize scripts list */ 30 if (!OS.IsWinCE) { 31 int /*long*/ [] ppSp = new int /*long*/ [1]; 32 int [] piNumScripts = new int [1]; 33 OS.ScriptGetProperties (ppSp, piNumScripts); 34 scripts = new int /*long*/ [piNumScripts [0]]; 35 OS.MoveMemory (scripts, ppSp [0], scripts.length * OS.PTR_SIZEOF); 36 } 37 38 /* 39 * If we're not on a device which supports palettes, 40 * don't create one. 41 */ 42 int /*long*/ hDC = internal_new_GC (null); 43 int rc = OS.GetDeviceCaps (hDC, OS.RASTERCAPS); 44 int bits = OS.GetDeviceCaps (hDC, OS.BITSPIXEL); 45 int planes = OS.GetDeviceCaps (hDC, OS.PLANES); 46 47 bits *= planes; 48 if ((rc & OS.RC_PALETTE) == 0 || bits != 8) { 49 internal_dispose_GC (hDC, null); 50 return; 51 } 52 53 int numReserved = OS.GetDeviceCaps (hDC, OS.NUMRESERVED); 54 int numEntries = OS.GetDeviceCaps (hDC, OS.SIZEPALETTE); 55 56 if (OS.IsWinCE) { 57 /* 58 * Feature on WinCE. For some reason, certain 8 bit WinCE 59 * devices return 0 for the number of reserved entries in 60 * the system palette. Their system palette correctly contains 61 * the usual 20 system colors. The workaround is to assume 62 * there are 20 reserved system colors instead of 0. 63 */ 64 if (numReserved == 0 && numEntries >= 20) numReserved = 20; 65 } 66 67 /* Create the palette and reference counter */ 68 colorRefCount = new int [numEntries]; 69 70 /* 4 bytes header + 4 bytes per entry * numEntries entries */ 71 byte [] logPalette = new byte [4 + 4 * numEntries]; 72 73 /* 2 bytes = special header */ 74 logPalette [0] = 0x00; 75 logPalette [1] = 0x03; 76 77 /* 2 bytes = number of colors, LSB first */ 78 logPalette [2] = 0; 79 logPalette [3] = 1; 80 81 /* 82 * Create a palette which contains the system entries 83 * as they are located in the system palette. The 84 * MSDN article 'Memory Device Contexts' describes 85 * where system entries are located. On an 8 bit 86 * display with 20 reserved colors, the system colors 87 * will be the first 10 entries and the last 10 ones. 88 */ 89 byte[] lppe = new byte [4 * numEntries]; 90 OS.GetSystemPaletteEntries (hDC, 0, numEntries, lppe); 91 /* Copy all entries from the system palette */ 92 System.arraycopy (lppe, 0, logPalette, 4, 4 * numEntries); 93 /* Lock the indices corresponding to the system entries */ 94 for (int i = 0; i < numReserved / 2; i++) { 95 colorRefCount [i] = 1; 96 colorRefCount [numEntries - 1 - i] = 1; 97 } 98 internal_dispose_GC (hDC, null); 99 hPalette = OS.CreatePalette (logPalette); 100 }
Applications which are built with SWT will almost always require only a single display. In particular, some platforms which SWT supports will not allow more than one active display. In other words, some platforms do not support creating a new display if one already exists that has not been sent the dispose()
message.In SWT, the thread which creates a Display
instance is distinguished as the user-interface thread for that display.The user-interface thread for a particular display has the following special attributes:oop
Widget
and its subclasses), may only be called from the thread. (To support multi-threaded user-interface applications, class Display
provides inter-thread communication methods which allow threads other than the user-interface thread to request that it perform operations on their behalf.)Display
s until that display has been disposed. (Note that, this is in addition to the restriction mentioned above concerning platform support for multiple displays. Thus, the only way to have multiple simultaneously active displays, even on platforms which support it, is to have multiple threads.)1 public class Display extends Device
Variables in Displaypost
1 public MSG msg = new MSG (); 2 static String APP_NAME = "SWT"; //$NON-NLS-1$ 3 static String APP_VERSION = ""; //$NON-NLS-1$ 4 /* Windows and Events */ 5 Event [] eventQueue; 6 Callback windowCallback; 7 int /*long*/ windowProc; 8 int threadId; 9 Control [] controlTable; 10 static final int GROW_SIZE = 1024; 11 static final int SWT_OBJECT_INDEX; 12 static final boolean USE_PROPERTY = !OS.IsWinCE; 13 Menu [] bars, popups; 14 MenuItem [] items; 15 Callback msgFilterCallback; 16 int /*long*/ msgFilterProc, filterHook; 17 MSG hookMsg = new MSG (); 18 /* Sync/Async Widget Communication */ 19 Synchronizer synchronizer = new Synchronizer (this); 20 /* Deferred Layout list */ 21 Composite[] layoutDeferred; 22 /* Timers */ 23 int /*long*/ [] timerIds; 24 Runnable [] timerList; 25 /* Message Only Window */ 26 Callback messageCallback; 27 int /*long*/ hwndMessage, messageProc; 28 static { 29 DeviceFinder = new Runnable () { 30 public void run () { 31 Device device = getCurrent (); 32 if (device == null) { 33 device = getDefault (); 34 } 35 setDevice (device); 36 } 37 }; 38 }
Methods in Display ui
1 void wakeThread () { 2 if (OS.IsWinCE) { 3 OS.PostMessage (hwndMessage, OS.WM_NULL, 0, 0); 4 } else { 5 OS.PostThreadMessage (threadId, OS.WM_NULL, 0, 0); 6 } 7 }
1 public void wake () { 2 synchronized (Device.class) { 3 if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED); 4 if (thread == Thread.currentThread ()) return; 5 wakeThread (); 6 } 7 }
1 public void update() { 2 checkDevice (); 3 if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (4, 10)) { 4 if (OS.IsHungAppWindow (hwndMessage)) { 5 MSG msg = new MSG (); 6 int flags = OS.PM_REMOVE | OS.PM_NOYIELD; 7 OS.PeekMessage (msg, hwndMessage, SWT_NULL, SWT_NULL, flags); 8 } 9 } 10 Shell[] shells = getShells (); 11 for (int i=0; i<shells.length; i++) { 12 Shell shell = shells [i]; 13 if (!shell.isDisposed ()) shell.update (true); 14 } 15 }
1 public void syncExec (Runnable runnable) { 2 Synchronizer synchronizer; 3 synchronized (Device.class) { 4 if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED); 5 synchronizer = this.synchronizer; 6 } 7 synchronizer.syncExec (runnable); 8 }
1 public boolean sleep () { 2 checkDevice (); 3 if (runMessages && getMessageCount () != 0) return true; 4 if (OS.IsWinCE) { 5 OS.MsgWaitForMultipleObjectsEx (0, 0, OS.INFINITE, OS.QS_ALLINPUT, OS.MWMO_INPUTAVAILABLE); 6 return true; 7 } 8 return OS.WaitMessage (); 9 }
1 boolean runTimer (int /*long*/ id) { 2 if (timerList != null && timerIds != null) { 3 int index = 0; 4 while (index <timerIds.length) { 5 if (timerIds [index] == id) { 6 OS.KillTimer (hwndMessage, timerIds [index]); 7 timerIds [index] = 0; 8 Runnable runnable = timerList [index]; 9 timerList [index] = null; 10 if (runnable != null) runnable.run (); 11 return true; 12 } 13 index++; 14 } 15 } 16 return false; 17 }
1 public void asyncExec (Runnable runnable) { 2 synchronized (Device.class) { 3 if (isDisposed ()) error (SWT.ERROR_DEVICE_DISPOSED); 4 synchronizer.asyncExec (runnable); 5 } 6 }
1 public void addListener (int eventType, Listener listener) { 2 checkDevice (); 3 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 4 if (eventTable == null) eventTable = new EventTable (); 5 eventTable.hook (eventType, listener); 6 }
1 Display display; 2 EventTable eventTable;
1 static { 2 if (!OS.IsWinCE) { 3 if (OS.COMCTL32_VERSION < OS.VERSION (MAJOR, MINOR)) { 4 System.out.println ("***WARNING: SWT requires comctl32.dll version " + MAJOR + "." + MINOR + " or greater"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 5 System.out.println ("***WARNING: Detected: " + OS.COMCTL32_MAJOR + "." + OS.COMCTL32_MINOR); //$NON-NLS-1$ //$NON-NLS-2$ 6 } 7 } 8 OS.InitCommonControls (); 9 }
1 /* Global state flags */ 2 static final int DISPOSED = 1<<0; 3 static final int CANVAS = 1<<1; 4 static final int KEYED_DATA = 1<<2; 5 static final int DISABLED = 1<<3; 6 static final int HIDDEN = 1<<4;
1 public Widget (Widget parent, int style) { 2 checkSubclass (); 3 checkParent (parent); 4 this.style = style; 5 display = parent.display; 6 reskinWidget (); 7 } 8 9 void _addListener (int eventType, Listener listener) { 10 if (eventTable == null) eventTable = new EventTable (); 11 eventTable.hook (eventType, listener); 12 }
1 protected void checkWidget () { 2 Display display = this.display; 3 if (display == null) error (SWT.ERROR_WIDGET_DISPOSED); 4 if (display.thread != Thread.currentThread ()) { 5 if (display.threadId != OS.GetCurrentThreadId ()) { 6 error (SWT.ERROR_THREAD_INVALID_ACCESS); 7 } 8 } 9 if ((state & DISPOSED) != 0) error (SWT.ERROR_WIDGET_DISPOSED); 10 }
1 public void notifyListeners (int eventType, Event event) { 2 checkWidget(); 3 if (event == null) event = new Event (); 4 sendEvent (eventType, event); 5 } 6 void postEvent (int eventType) { 7 sendEvent (eventType, null, false); 8 } 9 void postEvent (int eventType, Event event) { 10 sendEvent (eventType, event, false); 11 }
EventTable:sendEvent()
1 public void sendEvent (Event event) { 2 if (types == null) return; 3 level += level >= 0 ? 1 : -1; 4 try { 5 for (int i=0; i<types.length; i++) { 6 if (event.type == SWT.None) return; 7 if (types [i] == event.type) { 8 Listener listener = listeners [i]; 9 if (listener != null) listener.handleEvent (event); 10 } 11 } 12 } finally { 13 boolean compact = level < 0; 14 level -= level >= 0 ? 1 : -1; 15 if (compact && level == 0) { 16 int index = 0; 17 for (int i=0; i<types.length; i++) { 18 if (types [i] != 0) { 19 types [index] = types [i]; 20 listeners [index] = listeners [i]; 21 index++; 22 } 23 } 24 for (int i=index; i<types.length; i++) { 25 types [i] = 0; 26 listeners [i] = null; 27 } 28 } 29 } 30 }
1 boolean showMenu (int x, int y, int detail) { 2 Event event = new Event (); 3 event.x = x; 4 event.y = y; 5 event.detail = detail; 6 if (event.detail == SWT.MENU_KEYBOARD) { 7 updateMenuLocation (event); 8 } 9 sendEvent (SWT.MenuDetect, event); 10 // widget could be disposed at this point 11 if (isDisposed ()) return false; 12 if (!event.doit) return true; 13 Menu menu = getMenu (); 14 if (menu != null && !menu.isDisposed ()) { 15 if (x != event.x || y != event.y) { 16 menu.setLocation (event.x, event.y); 17 } 18 menu.setVisible (true); 19 return true; 20 } 21 return false; 22 }
1 public int /*long*/ handle; 2 Composite parent; 3 Cursor cursor; 4 Menu menu; 5 String toolTipText; 6 Object layoutData; 7 Accessible accessible; 8 Image backgroundImage; 9 Region region;
1 public Control (Composite parent, int style) { 2 super (parent, style); 3 this.parent = parent; 4 createWidget (); 5 }
1 void createWidget () { 2 state |= DRAG_DETECT; 3 foreground = background = -1; 4 checkOrientation (parent); 5 createHandle (); 6 checkBackground (); 7 checkBuffered (); 8 checkComposited (); 9 register (); 10 subclass (); 11 setDefaultFont (); 12 checkMirrored (); 13 checkBorder (); 14 checkGesture (); 15 if ((state & PARENT_BACKGROUND) != 0) { 16 setBackground (); 17 } 18 }
1 void createHandle () { 2 int /*long*/ hwndParent = widgetParent (); 3 handle = OS.CreateWindowEx ( 4 widgetExtStyle (), 5 windowClass (), 6 null, 7 widgetStyle (), 8 OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0, 9 hwndParent, 10 0, 11 OS.GetModuleHandle (null), 12 widgetCreateStruct ()); 13 if (handle == 0) error (SWT.ERROR_NO_HANDLES); 14 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE); 15 if ((bits & OS.WS_CHILD) != 0) { 16 OS.SetWindowLongPtr (handle, OS.GWLP_ID, handle); 17 } 18 if (OS.IsDBLocale && hwndParent != 0) { 19 int /*long*/ hIMC = OS.ImmGetContext (hwndParent); 20 OS.ImmAssociateContext (handle, hIMC); 21 OS.ImmReleaseContext (hwndParent, hIMC); 22 } 23 24 }
1 void subclass () { 2 int /*long*/ oldProc = windowProc (); 3 int /*long*/ newProc = display.windowProc; 4 if (oldProc == newProc) return; 5 OS.SetWindowLongPtr (handle, OS.GWLP_WNDPROC, newProc); 6 }
1 int /*long*/ windowProc (int /*long*/ hwnd, int msg, int /*long*/ wParam, int /*long*/ lParam) { 2 LRESULT result = null; 3 switch (msg) { 4 case OS.WM_ACTIVATE: result = WM_ACTIVATE (wParam, lParam); break; 5 case OS.WM_COMMAND: result = WM_COMMAND (wParam, lParam); break; 6 ....... 7 }
1 LRESULT WM_COMMAND (int /*long*/ wParam, int /*long*/ lParam) { 2 /* 3 * When the WM_COMMAND message is sent from a 4 * menu, the HWND parameter in LPARAM is zero. 5 */ 6 if (lParam == 0) { 7 Decorations shell = menuShell (); 8 if (shell.isEnabled ()) { 9 int id = OS.LOWORD (wParam); 10 MenuItem item = display.getMenuItem (id); 11 if (item != null && item.isEnabled ()) { 12 return item.wmCommandChild (wParam, lParam); 13 } 14 } 15 return null; 16 } 17 Control control = display.getControl (lParam); 18 if (control == null) return null; 19 return control.wmCommandChild (wParam, lParam); 20 }
1 Layout layout; 2 WINDOWPOS [] lpwp; 3 Control [] tabList; 4 int layoutCount, backgroundMode;
1 Control [] _getChildren () { 2 int count = 0; 3 int /*long*/ hwndChild = OS.GetWindow (handle, OS.GW_CHILD); 4 if (hwndChild == 0) return new Control [0]; 5 while (hwndChild != 0) { 6 count++; 7 hwndChild = OS.GetWindow (hwndChild, OS.GW_HWNDNEXT); 8 } 9 Control [] children = new Control [count]; 10 int index = 0; 11 hwndChild = OS.GetWindow (handle, OS.GW_CHILD); 12 while (hwndChild != 0) { 13 Control control = display.getControl (hwndChild); 14 if (control != null && control != this) { 15 children [index++] = control; 16 } 17 hwndChild = OS.GetWindow (hwndChild, OS.GW_HWNDNEXT); 18 } 19 if (count == index) return children; 20 Control [] newChildren = new Control [index]; 21 System.arraycopy (children, 0, newChildren, 0, index); 22 return newChildren; 23 } 24 25 Control [] _getTabList () { 26 if (tabList == null) return tabList; 27 int count = 0; 28 for (int i=0; i<tabList.length; i++) { 29 if (!tabList [i].isDisposed ()) count++; 30 } 31 if (count == tabList.length) return tabList; 32 Control [] newList = new Control [count]; 33 int index = 0; 34 for (int i=0; i<tabList.length; i++) { 35 if (!tabList [i].isDisposed ()) { 36 newList [index++] = tabList [i]; 37 } 38 } 39 tabList = newList; 40 return tabList; 41 }
1 Menu activeMenu; 2 ToolTip [] toolTips; 3 int /*long*/ hIMC, hwndMDIClient, lpstrTip, toolTipHandle, balloonTipHandle; 4 int minWidth = SWT.DEFAULT, minHeight = SWT.DEFAULT; 5 int /*long*/ [] brushes; 6 boolean showWithParent, fullScreen, wasMaximized, modified, center; 7 String toolTitle, balloonTitle; 8 int /*long*/ toolIcon, balloonIcon; 9 int /*long*/ windowProc; 10 Control lastActive; 11 SHACTIVATEINFO psai; 12 static /*final*/ int /*long*/ ToolTipProc; 13 static final int /*long*/ DialogProc; 14 static final TCHAR DialogClass = new TCHAR (0, OS.IsWinCE ? "Dialog" : "#32770", true); 15 final static int [] SYSTEM_COLORS = { 16 OS.COLOR_BTNFACE, 17 OS.COLOR_WINDOW, 18 OS.COLOR_BTNTEXT, 19 OS.COLOR_WINDOWTEXT, 20 OS.COLOR_HIGHLIGHT, 21 OS.COLOR_SCROLLBAR, 22 }; 23 final static int BRUSHES_SIZE = 32; 24 static { 25 WNDCLASS lpWndClass = new WNDCLASS (); 26 OS.GetClassInfo (0, DialogClass, lpWndClass); 27 DialogProc = lpWndClass.lpfnWndProc; 28 }
1 public Shell (Display display) { 2 this (display, OS.IsWinCE ? SWT.NONE : SWT.SHELL_TRIM); 3 }
1 Shell (Display display, Shell parent, int style, int /*long*/ handle, boolean embedded) { 2 super (); 3 checkSubclass (); 4 if (display == null) display = Display.getCurrent (); 5 if (display == null) display = Display.getDefault (); 6 if (!display.isValidThread ()) { 7 error (SWT.ERROR_THREAD_INVALID_ACCESS); 8 } 9 if (parent != null && parent.isDisposed ()) { 10 error (SWT.ERROR_INVALID_ARGUMENT); 11 } 12 this.center = parent != null && (style & SWT.SHEET) != 0; 13 this.style = checkStyle (parent, style); 14 this.parent = parent; 15 this.display = display; 16 this.handle = handle; 17 if (handle != 0 && !embedded) { 18 state |= FOREIGN_HANDLE; 19 } 20 reskinWidget(); 21 createWidget (); 22 }
1 public void addShellListener (ShellListener listener) { 2 checkWidget (); 3 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 4 TypedListener typedListener = new TypedListener (listener); 5 addListener (SWT.Close,typedListener); 6 addListener (SWT.Iconify,typedListener); 7 addListener (SWT.Deiconify,typedListener); 8 addListener (SWT.Activate, typedListener); 9 addListener (SWT.Deactivate, typedListener); 10 }
1 int /*long*/ callWindowProc (int /*long*/ hwnd, int msg, int /*long*/ wParam, int /*long*/ lParam) { 2 if (handle == 0) return 0; 3 if (hwnd == toolTipHandle || hwnd == balloonTipHandle) { 4 return OS.CallWindowProc (ToolTipProc, hwnd, msg, wParam, lParam); 5 } 6 if (hwndMDIClient != 0) { 7 return OS.DefFrameProc (hwnd, hwndMDIClient, msg, wParam, lParam); 8 } 9 if (windowProc != 0) { 10 return OS.CallWindowProc (windowProc, hwnd, msg, wParam, lParam); 11 } 12 if ((style & SWT.TOOL) != 0) { 13 int trim = SWT.TITLE | SWT.CLOSE | SWT.MIN | SWT.MAX | SWT.BORDER | SWT.RESIZE; 14 if ((style & trim) == 0) return OS.DefWindowProc (hwnd, msg, wParam, lParam); 15 } 16 if (parent != null) { 17 switch (msg) { 18 case OS.WM_KILLFOCUS: 19 case OS.WM_SETFOCUS: 20 return OS.DefWindowProc (hwnd, msg, wParam, lParam); 21 } 22 return OS.CallWindowProc (DialogProc, hwnd, msg, wParam, lParam); 23 } 24 return OS.DefWindowProc (hwnd, msg, wParam, lParam); 25 }
void createHandle () { boolean embedded = handle != 0 && (state & FOREIGN_HANDLE) == 0; if (handle == 0 || embedded) { super.createHandle (); } else { state |= CANVAS; if ((style & (SWT.H_SCROLL | SWT.V_SCROLL)) == 0) { state |= THEME_BACKGROUND; } windowProc = OS.GetWindowLongPtr (handle, OS.GWL_WNDPROC); } if (!embedded) { int bits = OS.GetWindowLong (handle, OS.GWL_STYLE); bits &= ~(OS.WS_OVERLAPPED | OS.WS_CAPTION); if (!OS.IsWinCE) bits |= OS.WS_POPUP; if ((style & SWT.TITLE) != 0) bits |= OS.WS_CAPTION; if ((style & SWT.NO_TRIM) == 0) { if ((style & (SWT.BORDER | SWT.RESIZE)) == 0) bits |= OS.WS_BORDER; } OS.SetWindowLong (handle, OS.GWL_STYLE, bits); int flags = OS.SWP_DRAWFRAME | OS.SWP_NOMOVE | OS.SWP_NOSIZE | OS.SWP_NOZORDER | OS.SWP_NOACTIVATE; SetWindowPos (handle, 0, 0, 0, 0, 0, flags); if (OS.IsWinCE) _setMaximized (true); if (OS.IsPPC) { psai = new SHACTIVATEINFO (); psai.cbSize = SHACTIVATEINFO.sizeof; } } if (OS.IsDBLocale) { hIMC = OS.ImmCreateContext (); if (hIMC != 0) OS.ImmAssociateContext (handle, hIMC); } }
1 void createToolTip (ToolTip toolTip) { 2 int id = 0; 3 if (toolTips == null) toolTips = new ToolTip [4]; 4 while (id < toolTips.length && toolTips [id] != null) id++; 5 if (id == toolTips.length) { 6 ToolTip [] newToolTips = new ToolTip [toolTips.length + 4]; 7 System.arraycopy (toolTips, 0, newToolTips, 0, toolTips.length); 8 toolTips = newToolTips; 9 } 10 toolTips [id] = toolTip; 11 toolTip.id = id + Display.ID_START; 12 if (OS.IsWinCE) return; 13 TOOLINFO lpti = new TOOLINFO (); 14 lpti.cbSize = TOOLINFO.sizeof; 15 lpti.hwnd = handle; 16 lpti.uId = toolTip.id; 17 lpti.uFlags = OS.TTF_TRACK; 18 lpti.lpszText = OS.LPSTR_TEXTCALLBACK; 19 OS.SendMessage (toolTip.hwndToolTip (), OS.TTM_ADDTOOL, 0, lpti); 20 }
1 void createToolTipHandle () { 2 toolTipHandle = OS.CreateWindowEx ( 3 0, 4 new TCHAR (0, OS.TOOLTIPS_CLASS, true), 5 null, 6 OS.TTS_ALWAYSTIP | OS.TTS_NOPREFIX, 7 OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0, 8 handle, 9 0, 10 OS.GetModuleHandle (null), 11 null); 12 if (toolTipHandle == 0) error (SWT.ERROR_NO_HANDLES); 13 if (ToolTipProc == 0) { 14 ToolTipProc = OS.GetWindowLongPtr (toolTipHandle, OS.GWLP_WNDPROC); 15 } 16 OS.SendMessage (toolTipHandle, OS.TTM_SETMAXTIPWIDTH, 0, 0x7FFF); 17 display.addControl (toolTipHandle, this); 18 OS.SetWindowLongPtr (toolTipHandle, OS.GWLP_WNDPROC, display.windowProc); 19 }
1 public Rectangle getBounds () { 2 checkWidget (); 3 if (!OS.IsWinCE) { 4 if (OS.IsIconic (handle)) return super.getBounds (); 5 } 6 RECT rect = new RECT (); 7 OS.GetWindowRect (handle, rect); 8 int width = rect.right - rect.left; 9 int height = rect.bottom - rect.top; 10 return new Rectangle (rect.left, rect.top, width, height); 11 }
1 public Point getLocation () { 2 checkWidget (); 3 if (!OS.IsWinCE) { 4 if (OS.IsIconic (handle)) { 5 return super.getLocation (); 6 } 7 } 8 RECT rect = new RECT (); 9 OS.GetWindowRect (handle, rect); 10 return new Point (rect.left, rect.top); 11 }
1 public class Button extends Control
1 Image image, image2, disabledImage; 2 ImageList imageList; 3 boolean ignoreMouse, grayed; 4 static final int MARGIN = 4; 5 static final int CHECK_WIDTH, CHECK_HEIGHT; 6 static final int ICON_WIDTH = 128, ICON_HEIGHT = 128; 7 static /*final*/ boolean COMMAND_LINK = false; 8 static final int /*long*/ ButtonProc;//WinProc function's adddress 9 static final TCHAR ButtonClass = new TCHAR (0, "BUTTON", true); 10 static { 11 int /*long*/ hBitmap = OS.LoadBitmap (0, OS.OBM_CHECKBOXES); 12 if (hBitmap == 0) { 13 CHECK_WIDTH = OS.GetSystemMetrics (OS.IsWinCE ? OS.SM_CXSMICON : OS.SM_CXVSCROLL); 14 CHECK_HEIGHT = OS.GetSystemMetrics (OS.IsWinCE ? OS.SM_CYSMICON : OS.SM_CYVSCROLL); 15 } else { 16 BITMAP bitmap = new BITMAP (); 17 OS.GetObject (hBitmap, BITMAP.sizeof, bitmap); 18 OS.DeleteObject (hBitmap); 19 CHECK_WIDTH = bitmap.bmWidth / 4; 20 CHECK_HEIGHT = bitmap.bmHeight / 3; 21 } 22 WNDCLASS lpWndClass = new WNDCLASS (); 23 OS.GetClassInfo (0, ButtonClass, lpWndClass); 24 ButtonProc = lpWndClass.lpfnWndProc; 25 }
1 public Button (Composite parent, int style) { 2 super (parent, checkStyle (style)); 3 }
1 static int checkStyle (int style) { 2 style = checkBits (style, SWT.PUSH, SWT.ARROW, SWT.CHECK, SWT.RADIO, SWT.TOGGLE, COMMAND_LINK ? SWT.COMMAND : 0); 3 if ((style & (SWT.PUSH | SWT.TOGGLE)) != 0) { 4 return checkBits (style, SWT.CENTER, SWT.LEFT, SWT.RIGHT, 0, 0, 0); 5 } 6 if ((style & (SWT.CHECK | SWT.RADIO)) != 0) { 7 return checkBits (style, SWT.LEFT, SWT.RIGHT, SWT.CENTER, 0, 0, 0); 8 } 9 if ((style & SWT.ARROW) != 0) { 10 style |= SWT.NO_FOCUS; 11 return checkBits (style, SWT.UP, SWT.DOWN, SWT.LEFT, SWT.RIGHT, 0, 0); 12 } 13 return style; 14 }
1 void click () { 2 ignoreMouse = true; 3OS.SendMessage (handle, OS.BM_CLICK, 0, 0); 4 ignoreMouse = false; 5 }
1 int /*long*/ callWindowProc (int /*long*/ hwnd, int msg, int /*long*/ wParam, int /*long*/ lParam) { 2 if (handle == 0) return 0; 3 return OS.CallWindowProc (ButtonProc, hwnd, msg, wParam, lParam); 4 }
1 public void setSelection (boolean selected) { 2 checkWidget (); 3 if ((style & (SWT.CHECK | SWT.RADIO | SWT.TOGGLE)) == 0) return; 4 int flags = selected ? OS.BST_CHECKED : OS.BST_UNCHECKED; 5 if ((style & SWT.CHECK) != 0) { 6 if (selected && grayed) flags = OS.BST_INDETERMINATE; 7 } 8 updateSelection (flags); 9 }
1 void updateSelection (int flags) { 2 if (flags != OS.SendMessage (handle, OS.BM_GETCHECK, 0, 0)) { 3 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE); 4 if ((style & SWT.CHECK) != 0) { 5 if (flags == OS.BST_INDETERMINATE) { 6 bits &= ~OS.BS_CHECKBOX; 7 bits |= OS.BS_3STATE; 8 } else { 9 bits |= OS.BS_CHECKBOX; 10 bits &= ~OS.BS_3STATE; 11 } 12 if (bits != OS.GetWindowLong (handle, OS.GWL_STYLE)) { 13 OS.SetWindowLong (handle, OS.GWL_STYLE, bits); 14 } 15 } 16 OS.SendMessage (handle, OS.BM_SETCHECK, flags, 0); 17 if (bits != OS.GetWindowLong (handle, OS.GWL_STYLE)) { 18 OS.SetWindowLong (handle, OS.GWL_STYLE, bits); 19 } 20 } 21 }
1 TCHAR windowClass () { 2 return ButtonClass; 3 } 4 int /*long*/ windowProc () { 5 return ButtonProc; 6 }
1 LRESULT WM_LBUTTONDOWN (int /*long*/ wParam, int /*long*/ lParam) { 2 if (ignoreMouse) return null; 3 return super.WM_LBUTTONDOWN (wParam, lParam); 4 } 5 LRESULT WM_LBUTTONUP (int /*long*/ wParam, int /*long*/ lParam) { 6 if (ignoreMouse) return null; 7 return super.WM_LBUTTONUP (wParam, lParam); 8 }
1 LRESULT wmCommandChild (int /*long*/ wParam, int /*long*/ lParam) { 2 int code = OS.HIWORD (wParam); 3 switch (code) { 4 case OS.BN_CLICKED: 5 case OS.BN_DOUBLECLICKED: 6 if ((style & (SWT.CHECK | SWT.TOGGLE)) != 0) { 7 setSelection (!getSelection ()); 8 } else { 9 if ((style & SWT.RADIO) != 0) { 10 if ((parent.getStyle () & SWT.NO_RADIO_GROUP) != 0) { 11 setSelection (!getSelection ()); 12 } else { 13 selectRadio (); 14 } 15 } 16 } 17 sendSelectionEvent (SWT.Selection); 18 } 19 return super.wmCommandChild (wParam, lParam); 20 }
CtlView.rc //====================================================================== // Resource file // // Written for the book Programming Windows CE // Copyright (C) 2001 Douglas Boling //====================================================================== #include "CtlView.h" // Program-specific stuff ID_ICON ICON "CtlView.ico" // Program icon TEXTICON ICON "btnicon.ico" // Icon used in static window STATICBMP BITMAP "statbmp.bmp" // Bitmap used in static window CtlView.h //====================================================================== // Header file // // Written for the book Programming Windows CE // Copyright (C) 2001 Douglas Boling //====================================================================== // Returns number of elements #define dim(x) (sizeof(x) / sizeof(x[0])) //---------------------------------------------------------------------- // Generic defines and data types // struct decodeUINT { // Structure associates UINT Code; // messages // with a function. LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM); }; struct decodeCMD { // Structure associates UINT Code; // menu IDs with a LRESULT (*Fxn)(HWND, WORD, HWND, WORD); // function. }; //---------------------------------------------------------------------- // Generic defines used by application #define IDI_BTNICON 20 // Icon used on button #define ID_ICON 1 // Icon ID #define IDC_CMDBAR 2 // Command bar ID #define IDC_RPTLIST 3 // Report window ID // Client window IDs go from 5 through 9. #define IDC_WNDSEL 5 // Starting client // window IDs // Radio button IDs go from 10 through 14. #define IDC_RADIOBTNS 10 // Starting ID of // radio buttons // Button window defines #define IDC_PUSHBTN 100 #define IDC_CHKBOX 101 #define IDC_ACHKBOX 102 #define IDC_A3STBOX 103 #define IDC_RADIO1 104 #define IDC_RADIO2 105 #define IDC_OWNRDRAW 106 // Edit window defines #define IDC_SINGLELINE 100 #define IDC_MULTILINE 101 #define IDC_PASSBOX 102 // List box window defines #define IDC_COMBOBOX 100 #define IDC_SNGLELIST 101 #define IDC_MULTILIST 102 // Static control window defines #define IDC_LEFTTEXT 100 #define IDC_RIGHTTEXT 101 #define IDC_CENTERTEXT 102 #define IDC_ICONCTL 103 #define IDC_BITMAPCTL 104 // Scroll bar window defines #define IDC_LRSCROLL 100 #define IDC_UDSCROLL 101 // User-defined message to add a line to the window #define MYMSG_ADDLINE (WM_USER + 10) typedef struct { TCHAR *szClass; INT nID; TCHAR *szTitle; INT x; INT y; INT cx; INT cy; DWORD lStyle; } CTLWNDSTRUCT, *PCTLWNDSTRUCT; typedef struct { WORD wMsg; INT nID; WPARAM wParam; LPARAM lParam; } CTLMSG, * PCTLMSG; typedef struct { TCHAR *pszLabel; WORD wNotification; } NOTELABELS, *PNOTELABELS; //---------------------------------------------------------------------- // Function prototypes // int InitApp (HINSTANCE); HWND InitInstance (HINSTANCE, LPWSTR, int); int TermInstance (HINSTANCE, int); // Window procedures LRESULT CALLBACK FrameWndProc (HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK ClientWndProc (HWND, UINT, WPARAM, LPARAM); // Message handlers LRESULT DoCreateFrame (HWND, UINT, WPARAM, LPARAM); LRESULT DoCommandFrame (HWND, UINT, WPARAM, LPARAM); LRESULT DoAddLineFrame (HWND, UINT, WPARAM, LPARAM); LRESULT DoDestroyFrame (HWND, UINT, WPARAM, LPARAM); //---------------------------------------------------------------------- // Window prototypes and defines for BtnWnd // #define BTNWND TEXT ("ButtonWnd") int InitBtnWnd (HINSTANCE); // Window procedures LRESULT CALLBACK BtnWndProc (HWND, UINT, WPARAM, LPARAM); LRESULT DoCreateBtnWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoCtlColorBtnWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoCommandBtnWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoDrawItemBtnWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoMeasureItemBtnWnd (HWND, UINT, WPARAM, LPARAM); //---------------------------------------------------------------------- // Window prototypes and defines for EditWnd // #define EDITWND TEXT ("EditWnd") int InitEditWnd (HINSTANCE); // Window procedures LRESULT CALLBACK EditWndProc (HWND, UINT, WPARAM, LPARAM); LRESULT DoCreateEditWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoCommandEditWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoDrawItemEditWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoMeasureItemEditWnd (HWND, UINT, WPARAM, LPARAM); //---------------------------------------------------------------------- // Window prototypes and defines for ListWnd // #define LISTWND TEXT ("ListWnd") int InitListWnd (HINSTANCE); // Window procedures LRESULT CALLBACK ListWndProc (HWND, UINT, WPARAM, LPARAM); LRESULT DoCreateListWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoCommandListWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoDrawItemListWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoMeasureItemListWnd (HWND, UINT, WPARAM, LPARAM); //---------------------------------------------------------------------- // Window prototypes and defines for StatWnd // #define STATWND TEXT ("StaticWnd") int InitStatWnd (HINSTANCE); // Window procedures LRESULT CALLBACK StatWndProc (HWND, UINT, WPARAM, LPARAM); LRESULT DoCreateStatWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoCommandStatWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoDrawItemStatWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoMeasureItemStatWnd (HWND, UINT, WPARAM, LPARAM); //---------------------------------------------------------------------- // Window prototypes and defines ScrollWnd // #define SCROLLWND TEXT ("ScrollWnd") int InitScrollWnd (HINSTANCE); // Window procedures LRESULT CALLBACK ScrollWndProc (HWND, UINT, WPARAM, LPARAM); LRESULT DoCreateScrollWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoVScrollScrollWnd (HWND, UINT, WPARAM, LPARAM); LRESULT DoHScrollScrollWnd (HWND, UINT, WPARAM, LPARAM); CtlView.c //====================================================================== // CtlView - Lists the available fonts in the system. // // Written for the book Programming Windows CE // Copyright (C) 2001 Douglas Boling //====================================================================== #include <windows.h> // For all that Windows stuff #include <commctrl.h> // Command bar includes #include "CtlView.h" // Program-specific stuff //---------------------------------------------------------------------- // Global data // const TCHAR szAppName[] = TEXT ("CtlView"); HINSTANCE hInst; // Program instance handle // Message dispatch table for FrameWindowProc const struct decodeUINT FrameMessages[] = { WM_CREATE, DoCreateFrame, WM_COMMAND, DoCommandFrame, MYMSG_ADDLINE, DoAddLineFrame, WM_DESTROY, DoDestroyFrame, }; typedef struct { TCHAR *szTitle; INT nID; TCHAR *szCtlWnds; HWND hWndClient; } RBTNDATA; // Text for main window radio buttons TCHAR *szBtnTitle[] = {TEXT ("Buttons"), TEXT ("Edit"), TEXT ("List"), TEXT ("Static"), TEXT ("Scroll")}; // Class names for child windows containing controls TCHAR *szCtlWnds[] = {BTNWND, EDITWND, LISTWND, STATWND, SCROLLWND}; INT nWndSel = 0; //====================================================================== // Program entry point // int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { MSG msg; int rc = 0; HWND hwndFrame; // Initialize application. rc = InitApp (hInstance); if (rc) return rc; // Initialize this instance. hwndFrame = InitInstance (hInstance, lpCmdLine, nCmdShow); if (hwndFrame == 0) return 0x10; // Application message loop while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } // Instance cleanup return TermInstance (hInstance, msg.wParam); } //---------------------------------------------------------------------- // InitApp - Application initialization // int InitApp (HINSTANCE hInstance) { WNDCLASS wc; #if defined(WIN32_PLATFORM_PSPC) // If Pocket PC, allow only one instance of the application HWND hWnd = FindWindow (szAppName, NULL); if (hWnd) { SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01)); return -1; } #endif // Register application frame window class. wc.style = 0; // Window style wc.lpfnWndProc = FrameWndProc; // Callback function wc.cbClsExtra = 0; // Extra class data wc.cbWndExtra = 0; // Extra window data wc.hInstance = hInstance; // Owner handle wc.hIcon = NULL, // Application icon wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor wc.hbrBackground = (HBRUSH) GetSysColorBrush (COLOR_STATIC); wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = szAppName; // Window class name if (RegisterClass (&wc) == 0) return 1; // Initialize client window classes if (InitBtnWnd (hInstance) != 0) return 2; if (InitEditWnd (hInstance) != 0) return 2; if (InitListWnd (hInstance) != 0) return 2; if (InitStatWnd (hInstance) != 0) return 2; if (InitScrollWnd (hInstance) != 0) return 2; return 0; } //---------------------------------------------------------------------- // InitInstance - Instance initialization // HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) { HWND hWnd; // Save program instance handle in global variable. hInst = hInstance; // Create frame window. hWnd = CreateWindow (szAppName, TEXT ("Control View"), WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); // Return fail code if window not created. if (!IsWindow (hWnd)) return 0; // Standard show and update calls ShowWindow (hWnd, nCmdShow); UpdateWindow (hWnd); return hWnd; } //---------------------------------------------------------------------- // TermInstance - Program cleanup // int TermInstance (HINSTANCE hInstance, int nDefRC) { return nDefRC; } //====================================================================== // Message handling procedures for FrameWindow // //---------------------------------------------------------------------- // FrameWndProc - Callback function for application window // LRESULT CALLBACK FrameWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; // // Search message list to see if we need to handle this // message. If in list, call procedure. // for (i = 0; i < dim(FrameMessages); i++) { if (wMsg == FrameMessages[i].Code) return (*FrameMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc (hWnd, wMsg, wParam, lParam); } //---------------------------------------------------------------------- // DoCreateFrame - Process WM_CREATE message for window. // LRESULT DoCreateFrame (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { LPCREATESTRUCT lpcs; HWND hwndCB, hwndChild; INT sHeight, i, x, y, cx, cy; BOOL bWide = TRUE; // These arrays are used to adjust between wide and narrow screens. POINT ptRBtnsN[] = {{5,0}, {90,0}, {180,0}, {5,20}, {90,20}}; POINT ptRBtnsW[] = {{5,0}, {90,0}, {180,0}, {270,0}, {360,0}}; LPPOINT pptRbtns = ptRBtnsW; // Convert lParam into pointer to create struct. lpcs = (LPCREATESTRUCT) lParam; // Set currently viewed window nWndSel = 0; // Create a command bar, and add an exit button. hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR); CommandBar_AddAdornments (hwndCB, 0, 0); sHeight = CommandBar_Height (GetDlgItem (hWnd, IDC_CMDBAR)); // Use different layouts for narrow (Pocket PC) screens. if (GetSystemMetrics (SM_CXSCREEN) < 480) { pptRbtns = ptRBtnsN; bWide = FALSE; } // Create the radio buttons. for (i = 0; i < dim(szBtnTitle); i++) { hwndChild = CreateWindow (TEXT ("BUTTON"), szBtnTitle[i], BS_AUTORADIOBUTTON | WS_VISIBLE | WS_CHILD, pptRbtns[i].x, pptRbtns[i].y + sHeight, 80, 20, hWnd, (HMENU)(IDC_RADIOBTNS+i), hInst, NULL); // Destroy frame if window not created. if (!IsWindow (hwndChild)) { DestroyWindow (hWnd); break; } } // Create report window. Size it so that it fits either on the right // or below the control windows, depending on the size of the screen. x = bWide ? lpcs->cx/2 : lpcs->x; y = bWide ? sHeight + 20 : (lpcs->cy-sHeight)/2+sHeight + 40; cx = bWide ? lpcs->cx/2 : lpcs->cx; cy = bWide ? lpcs->cy - sHeight : lpcs->cy - y; hwndChild = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("listbox"), TEXT (""), WS_VISIBLE | WS_CHILD | WS_VSCROLL | LBS_USETABSTOPS | LBS_NOINTEGRALHEIGHT, x, y, cx, cy,hWnd, (HMENU)IDC_RPTLIST, hInst, NULL); // Destroy frame if window not created. if (!IsWindow (hwndChild)) { DestroyWindow (hWnd); return 0; } // Initialize tab stops for display list box. i = 24; SendMessage (hwndChild, LB_SETTABSTOPS, 1, (LPARAM)&i); // Create the child windows. Size them so that they fit under // the command bar and fill the left side of the child area. x = lpcs->x; y = bWide ? sHeight + 20 : sHeight + 40; cx = bWide ? lpcs->cx/2 : lpcs->cx; cy = bWide ? lpcs->cy - sHeight : (lpcs->cy-sHeight)/2+sHeight+40; for (i = 0; i < dim(szCtlWnds); i++) { hwndChild = CreateWindowEx (WS_EX_CLIENTEDGE, szCtlWnds[i], TEXT (""), WS_CHILD, x, y, cx, cy, hWnd, (HMENU)(IDC_WNDSEL+i), hInst, NULL); // Destroy frame if client window not created. if (!IsWindow (hwndChild)) { DestroyWindow (hWnd); return 0; } } // Check one of the auto radio buttons. SendDlgItemMessage (hWnd, IDC_RADIOBTNS+nWndSel, BM_SETCHECK, 1, 0); hwndChild = GetDlgItem (hWnd, IDC_WNDSEL+nWndSel); ShowWindow (hwndChild, SW_SHOW); return 0; } //---------------------------------------------------------------------- // DoCommandFrame - Process WM_COMMAND message for window. // LRESULT DoCommandFrame (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { HWND hwndTemp; int nBtn; // Don't look at list box messages. if (LOWORD (wParam) == IDC_RPTLIST) return 0; nBtn = LOWORD (wParam) - IDC_RADIOBTNS; if (nWndSel != nBtn) { // Hide the currently visible window. hwndTemp = GetDlgItem (hWnd, IDC_WNDSEL+nWndSel); ShowWindow (hwndTemp, SW_HIDE); // Save the current selection. nWndSel = nBtn; // Show the window selected via the radio button. hwndTemp = GetDlgItem (hWnd, IDC_WNDSEL+nWndSel); ShowWindow (hwndTemp, SW_SHOW); } return 0; } //---------------------------------------------------------------------- // DoAddLineFrame - Process MYMSG_ADDLINE message for window. // LRESULT DoAddLineFrame (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { TCHAR szOut[128]; INT i; if (LOWORD (wParam) == 0xffff) wsprintf (szOut, TEXT (" \t %s"), (LPTSTR)lParam); else wsprintf (szOut, TEXT ("id:%3d \t %s"), LOWORD (wParam), (LPTSTR)lParam); i = SendDlgItemMessage (hWnd, IDC_RPTLIST, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szOut); if (i != LB_ERR) SendDlgItemMessage (hWnd, IDC_RPTLIST, LB_SETTOPINDEX, i, (LPARAM)(LPCTSTR)szOut); return 0; } //---------------------------------------------------------------------- // DoDestroyFrame - Process WM_DESTROY message for window. // LRESULT DoDestroyFrame (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { PostQuitMessage (0); return 0; } BtnWnd.c //====================================================================== // BtnWnd - Button window code // // Written for the book Programming Windows CE // Copyright (C) 2001 Douglas Boling //====================================================================== #include <windows.h> // For all that Windows stuff #include "Ctlview.h" // Program-specific stuff extern HINSTANCE hInst; LRESULT DrawButton (HWND hWnd, LPDRAWITEMSTRUCT pdi); //---------------------------------------------------------------------- // Global data // // Message dispatch table for BtnWndWindowProc const struct decodeUINT BtnWndMessages[] = { WM_CREATE, DoCreateBtnWnd, WM_CTLCOLORSTATIC, DoCtlColorBtnWnd, WM_COMMAND, DoCommandBtnWnd, WM_DRAWITEM, DoDrawItemBtnWnd, }; // Structure defining the controls in the window CTLWNDSTRUCT Btns [] = { {TEXT ("BUTTON"), IDC_PUSHBTN, TEXT ("Button"), 10, 10, 120, 23, BS_PUSHBUTTON | BS_NOTIFY}, {TEXT ("BUTTON"), IDC_CHKBOX, TEXT ("Check box"), 10, 35, 120, 23, BS_CHECKBOX}, {TEXT ("BUTTON"), IDC_ACHKBOX, TEXT ("Auto check box"), 10, 60, 110, 23, BS_AUTOCHECKBOX}, {TEXT ("BUTTON"), IDC_A3STBOX, TEXT ("Multiline auto 3-state box"), 140, 60, 90, 52, BS_AUTO3STATE | BS_MULTILINE}, {TEXT ("BUTTON"), IDC_RADIO1, TEXT ("Auto radio button 1"), 10, 85, 120, 23, BS_AUTORADIOBUTTON}, {TEXT ("BUTTON"), IDC_RADIO2, TEXT ("Auto radio button 2"), 10, 110, 120, 23, BS_AUTORADIOBUTTON}, {TEXT ("BUTTON"), IDC_OWNRDRAW, TEXT ("OwnerDraw"), 150, 10, 44, 44, BS_PUSHBUTTON | BS_OWNERDRAW}, }; // Structure labeling the button control WM_COMMAND notifications NOTELABELS nlBtn[] = {{TEXT ("BN_CLICKED "), 0}, {TEXT ("BN_PAINT "), 1}, {TEXT ("BN_HILITE "), 2}, {TEXT ("BN_UNHILITE"), 3}, {TEXT ("BN_DISABLE "), 4}, {TEXT ("BN_DOUBLECLICKED"), 5}, {TEXT ("BN_SETFOCUS "), 6}, {TEXT ("BN_KILLFOCUS"), 7} }; // Handle for icon used in owner-draw icon HICON hIcon = 0; //---------------------------------------------------------------------- // InitBtnWnd - BtnWnd window initialization // int InitBtnWnd (HINSTANCE hInstance) { WNDCLASS wc; // Register application BtnWnd window class. wc.style = 0; // Window style wc.lpfnWndProc = BtnWndProc; // Callback function wc.cbClsExtra = 0; // Extra class data wc.cbWndExtra = 0; // Extra window data wc.hInstance = hInstance; // Owner handle wc.hIcon = NULL, // Application icon wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = BTNWND; // Window class name if (RegisterClass (&wc) == 0) return 1; return 0; } //====================================================================== // Message handling procedures for BtnWindow //---------------------------------------------------------------------- // BtnWndWndProc - Callback function for application window // LRESULT CALLBACK BtnWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; // // Search message list to see if we need to handle this // message. If in list, call procedure. // for (i = 0; i < dim(BtnWndMessages); i++) { if (wMsg == BtnWndMessages[i].Code) return (*BtnWndMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc (hWnd, wMsg, wParam, lParam); } //---------------------------------------------------------------------- // DoCreateBtnWnd - Process WM_CREATE message for window. // LRESULT DoCreateBtnWnd (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; for (i = 0; i < dim(Btns); i++) { CreateWindow (Btns[i].szClass, Btns[i].szTitle, Btns[i].lStyle | WS_VISIBLE | WS_CHILD, Btns[i].x, Btns[i].y, Btns[i].cx, Btns[i].cy, hWnd, (HMENU) Btns[i].nID, hInst, NULL); } hIcon = LoadIcon (hInst, TEXT ("TEXTICON")); // We need to set the initial state of the radio buttons. CheckRadioButton (hWnd, IDC_RADIO1, IDC_RADIO2, IDC_RADIO1); return 0; } //---------------------------------------------------------------------- // DoCtlColorBtnWnd - process WM_CTLCOLORxx messages for window. // LRESULT DoCtlColorBtnWnd (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { return (LRESULT)GetStockObject (WHITE_BRUSH); } //---------------------------------------------------------------------- // DoCommandBtnWnd - Process WM_COMMAND message for window. // LRESULT DoCommandBtnWnd (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { TCHAR szOut[128]; INT i; // Since the Check Box button is not an auto check box, it // must be set manually. if ((LOWORD (wParam) == IDC_CHKBOX) && (HIWORD (wParam) == BN_CLICKED)) { // Get the current state, complement, and set. i = SendDlgItemMessage (hWnd, IDC_CHKBOX, BM_GETCHECK, 0, 0); if (i == 0) SendDlgItemMessage (hWnd, IDC_CHKBOX, BM_SETCHECK, 1, 0); else SendDlgItemMessage (hWnd, IDC_CHKBOX, BM_SETCHECK, 0, 0); } // Report WM_COMMAND messages to main window. for (i = 0; i < dim(nlBtn); i++) { if (HIWORD (wParam) == nlBtn[i].wNotification) { lstrcpy (szOut, nlBtn[i].pszLabel); break; } } if (i == dim(nlBtn)) wsprintf (szOut, TEXT ("notification: %x"), HIWORD (wParam)); SendMessage (GetParent (hWnd), MYMSG_ADDLINE, wParam, (LPARAM)szOut); return 0; } //---------------------------------------------------------------------- // DoDrawItemBtnWnd - Process WM_DRAWITEM message for window. // LRESULT DoDrawItemBtnWnd (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { return DrawButton (hWnd, (LPDRAWITEMSTRUCT)lParam); } //--------------------------------------------------------------------- // DrawButton - Draws an owner-draw button // LRESULT DrawButton (HWND hWnd, LPDRAWITEMSTRUCT pdi) { HPEN hPenShadow, hPenLight, hPenDkShadow, hOldPen; HBRUSH hBr, hOldBr; LOGPEN lpen; TCHAR szOut[128]; POINT ptOut[3], ptIn[3]; // Reflect the messages to the report window. wsprintf (szOut, TEXT ("WM_DRAWITEM Act:%x State:%x"), pdi->itemAction, pdi->itemState); SendMessage (GetParent (hWnd), MYMSG_ADDLINE, pdi->CtlID, (LPARAM)szOut); // Create pens for drawing. lpen.lopnStyle = PS_SOLID; lpen.lopnWidth.x = 3; lpen.lopnWidth.y = 3; lpen.lopnColor = GetSysColor (COLOR_3DSHADOW); hPenShadow = CreatePenIndirect (&lpen); lpen.lopnWidth.x = 1; lpen.lopnWidth.y = 1; lpen.lopnColor = GetSysColor (COLOR_3DLIGHT); hPenLight = CreatePenIndirect (&lpen); lpen.lopnColor = GetSysColor (COLOR_3DDKSHADOW); hPenDkShadow = CreatePenIndirect (&lpen); // Create a brush for the face of the button. hBr = CreateSolidBrush (GetSysColor (COLOR_3DFACE)); // Draw a rectangle with a thick outside border to start the // frame drawing. hOldPen = SelectObject (pdi->hDC, hPenShadow); hOldBr = SelectObject (pdi->hDC, hBr); Rectangle (pdi->hDC, pdi->rcItem.left, pdi->rcItem.top, pdi->rcItem.right, pdi->rcItem.bottom); // Draw the upper left inside line. ptIn[0].x = pdi->rcItem.left + 1; ptIn[0].y = pdi->rcItem.bottom - 2; ptIn[1].x = pdi->rcItem.left + 1; ptIn[1].y = pdi->rcItem.top + 1; ptIn[2].x = pdi->rcItem.right - 2; ptIn[2].y = pdi->rcItem.top + 1; // Select a pen to draw shadow or light side of button. if (pdi->itemState & ODS_SELECTED) { SelectObject (pdi->hDC, hPenDkShadow); } else { SelectObject (pdi->hDC, hPenLight); } Polyline (pdi->hDC, ptIn, 3); // If selected, also draw a bright line inside the lower // right corner. if (pdi->itemState & ODS_SELECTED) { SelectObject (pdi->hDC, hPenLight); ptIn[1].x = pdi->rcItem.right - 2; ptIn[1].y = pdi->rcItem.bottom - 2; Polyline (pdi->hDC, ptIn, 3); } // Now draw the black outside line on either the upper left or lower // right corner. ptOut[0].x = pdi->rcItem.left; ptOut[0].y = pdi->rcItem.bottom - 1; ptOut[2].x = pdi->rcItem.right - 1; ptOut[2].y = pdi->rcItem.top; SelectObject (pdi->hDC, hPenDkShadow); if (pdi->itemState & ODS_SELECTED) { ptOut[1].x = pdi->rcItem.left; ptOut[1].y = pdi->rcItem.top; } else { ptOut[1].x = pdi->rcItem.right - 1; ptOut[1].y = pdi->rcItem.bottom - 1; } Polyline (pdi->hDC, ptOut, 3); // Draw the icon. if (hIcon) { ptIn[0].x = (pdi->rcItem.right - pdi->rcItem.left)/2 - GetSystemMetrics (SM_CXICON)/2 - 2; ptIn[0].y = (pdi->rcItem.bottom - pdi->rcItem.top)/2 - GetSystemMetrics (SM_CYICON)/2 - 2; // If pressed, shift image down one pel to simulate depress. if (pdi->itemState & ODS_SELECTED) { ptOut[1].x += 2; ptOut[1].y += 2; } DrawIcon (pdi->hDC, ptIn[0].x, ptIn[0].y, hIcon); } // If button has the focus, draw the dotted rect inside the button. if (pdi->itemState & ODS_FOCUS) { pdi->rcItem.left += 3; pdi->rcItem.top += 3; pdi->rcItem.right -= 4; pdi->rcItem.bottom -= 4; DrawFocusRect (pdi->hDC, &pdi->rcItem); } // Clean up. First select the original brush and pen into the DC. SelectObject (pdi->hDC, hOldBr); SelectObject (pdi->hDC, hOldPen); // Now delete the brushes and pens created. DeleteObject (hBr); DeleteObject (hPenShadow); DeleteObject (hPenDkShadow); DeleteObject (hPenLight); return 0; } EditWnd.c //====================================================================== // EditWnd - Edit control window code // // Written for the book Programming Windows CE // Copyright (C) 2001 Douglas Boling //====================================================================== #include <windows.h> // For all that Windows stuff #include "Ctlview.h" // Program-specific stuff extern HINSTANCE hInst; //---------------------------------------------------------------------- // Global data // // Message dispatch table for EditWndWindowProc const struct decodeUINT EditWndMessages[] = { WM_CREATE, DoCreateEditWnd, WM_COMMAND, DoCommandEditWnd, }; // Structure defining the controls in the window CTLWNDSTRUCT Edits[] = { {TEXT ("edit"), IDC_SINGLELINE, TEXT ("Single line edit control"), 10, 10, 180, 23, ES_AUTOHSCROLL}, {TEXT ("edit"), IDC_MULTILINE, TEXT ("Multiline edit control"), 10, 35, 180, 70, ES_MULTILINE | ES_AUTOVSCROLL}, {TEXT ("edit"), IDC_PASSBOX, TEXT (""), 10, 107, 180, 23, ES_PASSWORD}, }; // Structure labeling the edit control WM_COMMAND notifications NOTELABELS nlEdit[] = {{TEXT ("EN_SETFOCUS "), 0x0100}, {TEXT ("EN_KILLFOCUS"), 0x0200}, {TEXT ("EN_CHANGE "), 0x0300}, {TEXT ("EN_UPDATE "), 0x0400}, {TEXT ("EN_ERRSPACE "), 0x0500}, {TEXT ("EN_MAXTEXT "), 0x0501}, {TEXT ("EN_HSCROLL "), 0x0601}, {TEXT ("EN_VSCROLL "), 0x0602}, }; //---------------------------------------------------------------------- // InitEditWnd - EditWnd window initialization // int InitEditWnd (HINSTANCE hInstance) { WNDCLASS wc; // Register application EditWnd window class. wc.style = 0; // Window style wc.lpfnWndProc = EditWndProc; // Callback function wc.cbClsExtra = 0; // Extra class data wc.cbWndExtra = 0; // Extra window data wc.hInstance = hInstance; // Owner handle wc.hIcon = NULL, // Application icon wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = EDITWND; // Window class name if (RegisterClass (&wc) == 0) return 1; return 0; } //====================================================================== // Message handling procedures for EditWindow //---------------------------------------------------------------------- // EditWndWndProc - Callback function for application window // LRESULT CALLBACK EditWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; // // Search message list to see if we need to handle this // message. If in list, call procedure. // for (i = 0; i < dim(EditWndMessages); i++) { if (wMsg == EditWndMessages[i].Code) return (*EditWndMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc (hWnd, wMsg, wParam, lParam); } //---------------------------------------------------------------------- // DoCreateEditWnd - Process WM_CREATE message for window. // LRESULT DoCreateEditWnd (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; for (i = 0; i < dim(Edits); i++) { CreateWindow (Edits[i].szClass, Edits[i].szTitle, Edits[i].lStyle | WS_VISIBLE | WS_CHILD | WS_BORDER, Edits[i].x, Edits[i].y, Edits[i].cx, Edits[i].cy, hWnd, (HMENU) Edits[i].nID, hInst, NULL); } return 0; } //---------------------------------------------------------------------- // DoCommandEditWnd - Process WM_COMMAND message for window. // LRESULT DoCommandEditWnd (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { TCHAR szOut[128]; INT i; for (i = 0; i < dim(nlEdit); i++) { if (HIWORD (wParam) == nlEdit[i].wNotification) { lstrcpy (szOut, nlEdit[i].pszLabel); break; } } if (i == dim(nlEdit)) wsprintf (szOut, TEXT ("notification: %x"), HIWORD (wParam)); SendMessage (GetParent (hWnd), MYMSG_ADDLINE, wParam, (LPARAM)szOut); return 0; } ListWnd.c //====================================================================== // ListWnd - List box control window code // // Written for the book Programming Windows CE // Copyright (C) 2001 Douglas Boling //====================================================================== #include <windows.h> // For all that Windows stuff #include "Ctlview.h" // Program-specific stuff extern HINSTANCE hInst; //---------------------------------------------------------------------- // Global data // // Message dispatch table for ListWndWindowProc const struct decodeUINT ListWndMessages[] = { WM_CREATE, DoCreateListWnd, WM_COMMAND, DoCommandListWnd, }; // Structure defining the controls in the window CTLWNDSTRUCT Lists[] = { {TEXT ("combobox"), IDC_COMBOBOX, TEXT (""), 10, 10, 205, 100, WS_VSCROLL}, {TEXT ("Listbox"), IDC_SNGLELIST, TEXT (""), 10, 35, 100, 90, WS_VSCROLL | LBS_NOTIFY}, {TEXT ("Listbox"), IDC_MULTILIST, TEXT (""), 115, 35, 100, 90, WS_VSCROLL | LBS_EXTENDEDSEL | LBS_NOTIFY} }; // Structure labeling the list box control WM_COMMAND notifications NOTELABELS nlList[] = {{TEXT ("LBN_ERRSPACE "), (-2)}, {TEXT ("LBN_SELCHANGE"), 1}, {TEXT ("LBN_DBLCLK "), 2}, {TEXT ("LBN_SELCANCEL"), 3}, {TEXT ("LBN_SETFOCUS "), 4}, {TEXT ("LBN_KILLFOCUS"), 5}, }; // Structure labeling the combo box control WM_COMMAND notifications NOTELABELS nlCombo[] = {{TEXT ("CBN_ERRSPACE "), (-1)}, {TEXT ("CBN_SELCHANGE "), 1}, {TEXT ("CBN_DBLCLK "), 2}, {TEXT ("CBN_SETFOCUS "), 3}, {TEXT ("CBN_KILLFOCUS "), 4}, {TEXT ("CBN_EDITCHANGE "), 5}, {TEXT ("CBN_EDITUPDATE "), 6}, {TEXT ("CBN_DROPDOWN "), 7}, {TEXT ("CBN_CLOSEUP "), 8}, {TEXT ("CBN_SELENDOK "), 9}, {TEXT ("CBN_SELENDCANCEL"), 10}, }; //---------------------------------------------------------------------- // InitListWnd - ListWnd window initialization // int InitListWnd (HINSTANCE hInstance) { WNDCLASS wc; // Register application ListWnd window class. wc.style = 0; // Window style wc.lpfnWndProc = ListWndProc; // Callback function wc.cbClsExtra = 0; // Extra class data wc.cbWndExtra = 0; // Extra window data wc.hInstance = hInstance; // Owner handle wc.hIcon = NULL, // Application icon wc.hCursor = LoadCursor (NULL, IDC_ARROW); // Default cursor wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = LISTWND; // Window class name if (RegisterClass (&wc) == 0) return 1; return 0; } //====================================================================== // Message handling procedures for ListWindow //---------------------------------------------------------------------- // ListWndProc - Callback function for application window // LRESULT CALLBACK ListWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; // // Search message list to see if we need to handle this // message. If in list, call procedure. // for (i = 0; i < dim(ListWndMessages); i++) { if (wMsg == ListWndMessages[i].Code) return (*ListWndMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc (hWnd, wMsg, wParam, lParam); } //---------------------------------------------------------------------- // DoCreateListWnd - Process WM_CREATE message for window. // LRESULT DoCreateListWnd (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; TCHAR szOut[64]; for (i = 0; i < dim(Lists); i++) { CreateWindow (Lists[i].szClass, Lists[i].szTitle, Lists[i].lStyle | WS_VISIBLE | WS_CHILD | WS_BORDER, Lists[i].x, Lists[i].y, Lists[i].cx, Lists[i].cy, hWnd, (HMENU) Lists[i].nID, hInst, NULL); } for (i = 0; i < 20; i++) { wsprintf (szOut, TEXT ("Item %d"), i); SendDlgItemMessage (hWnd, IDC_SNGLELIST, LB_ADDSTRING, 0, (LPARAM)szOut); SendDlgItemMessage (hWnd, IDC_MULTILIST, LB_ADDSTRING, 0, (LPARAM)szOut); SendDlgItemMessage (hWnd, IDC_COMBOBOX, CB_ADDSTRING, 0, (LPARAM)szOut); } // Set initial selection. SendDlgItemMessage (hWnd, IDC_COMBOBOX, CB_SETCURSEL, 0, 0); return 0; } //---------------------------------------------------------------------- // DoCommandListWnd - Process WM_COMMAND message for window. // LRESULT DoCommandListWnd (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { TCHAR szOut[128]; INT i; if (LOWORD (wParam) == IDC_COMBOBOX) { for (i = 0; i < dim(nlCombo); i++) { if (HIWORD (wParam) == nlCombo[i].wNotification) { lstrcpy (szOut, nlCombo[i].pszLabel); break; } } if (i == dim(nlList)) wsprintf (szOut, TEXT ("notification: %x"), HIWORD (wParam)); } else { for (i = 0; i < dim(nlList); i++) { if (HIWORD (wParam) == nlList[i].wNotification) { lstrcpy (szOut, nlList[i].pszLabel); break; } } if (i == dim(nlList)) wsprintf (szOut, TEXT ("notification: %x"), HIWORD (wParam)); } SendMessage (GetParent (hWnd), MYMSG_ADDLINE, wParam, (LPARAM)szOut); return 0; } StatWnd.c //====================================================================== // StatWnd - Static control window code // // Written for the book Programming Windows CE // Copyright (C) 2001 Douglas Boling //====================================================================== #include <windows.h> // For all that Windows stuff #include "Ctlview.h" // Program-specific stuff extern HINSTANCE hInst; //---------------------------------------------------------------------- // Global data // // Message dispatch table for StatWndWindowProc const struct decodeUINT StatWndMessages[] = { WM_CREATE, DoCreateStatWnd, WM_COMMAND, DoCommandStatWnd, }; // Structure defining the controls in the window CTLWNDSTRUCT Stats [] = { {TEXT ("static"), IDC_LEFTTEXT, TEXT ("Left text"), 10, 10, 120, 23, SS_LEFT | SS_NOTIFY}, {TEXT ("static"), IDC_RIGHTTEXT, TEXT ("Right text"), 10, 35, 120, 23, SS_RIGHT}, {TEXT ("static"), IDC_CENTERTEXT, TEXT ("Center text"), 10, 60, 120, 23, SS_CENTER | WS_BORDER}, {TEXT ("static"), IDC_ICONCTL, TEXT ("TEXTICON"), 10, 85, 120, 23, SS_ICON}, {TEXT ("static"), IDC_BITMAPCTL, TEXT ("STATICBMP"), 170, 10, 44, 44, SS_BITMAP | SS_NOTIFY}, }; // Structure labeling the static control WM_COMMAND notifications NOTELABELS nlStatic[] = {{TEXT ("STN_CLICKED"), 0}, {TEXT ("STN_ENABLE "), 2}, {TEXT ("STN_DISABLE"), 3}, }; //---------------------------------------------------------------------- // InitStatWnd - StatWnd window initialization // int InitStatWnd (HINSTANCE hInstance) { WNDCLASS wc; // Register application StatWnd window class. wc.style = 0; // Window style wc.lpfnWndProc = StatWndProc; // Callback function wc.cbClsExtra = 0; // Extra class data wc.cbWndExtra = 0; // Extra window data wc.hInstance = hInstance; // Owner handle wc.hIcon = NULL, // Application icon wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = STATWND; // Window class name if (RegisterClass (&wc) == 0) return 1; return 0; } //====================================================================== // Message handling procedures for StatWindow //---------------------------------------------------------------------- // StatWndProc - Callback function for application window // LRESULT CALLBACK StatWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; // // Search message list to see if we need to handle this // message. If in list, call procedure. // for (i = 0; i < dim(StatWndMessages); i++) { if (wMsg == StatWndMessages[i].Code) return (*StatWndMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc (hWnd, wMsg, wParam, lParam); } //---------------------------------------------------------------------- // DoCreateStatWnd - Process WM_CREATE message for window. // LRESULT DoCreateStatWnd (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; for (i = 0; i < dim(Stats); i++) { CreateWindow (Stats[i].szClass, Stats[i].szTitle, Stats[i].lStyle | WS_VISIBLE | WS_CHILD, Stats[i].x, Stats[i].y, Stats[i].cx, Stats[i].cy, hWnd, (HMENU) Stats[i].nID, hInst, NULL); } return 0; } //---------------------------------------------------------------------- // DoCommandStatWnd - Process WM_COMMAND message for window. // LRESULT DoCommandStatWnd (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { TCHAR szOut[128]; INT i; for (i = 0; i < dim(nlStatic); i++) { if (HIWORD (wParam) == nlStatic[i].wNotification) { lstrcpy (szOut, nlStatic[i].pszLabel); break; } } if (i == dim(nlStatic)) wsprintf (szOut, TEXT ("notification: %x"), HIWORD (wParam)); SendMessage (GetParent (hWnd), MYMSG_ADDLINE, wParam, (LPARAM)szOut); return 0; } ScrollWnd.c //====================================================================== // ScrollWnd - Scroll bar control window code // // Written for the book Programming Windows CE // Copyright (C) 2001 Douglas Boling //====================================================================== #include <windows.h> // For all that Windows stuff #include "Ctlview.h" // Program-specific stuff extern HINSTANCE hInst; //---------------------------------------------------------------------- // Global data // // Message dispatch table for ScrollWndWindowProc const struct decodeUINT ScrollWndMessages[] = { WM_CREATE, DoCreateScrollWnd, WM_HSCROLL, DoVScrollScrollWnd, WM_VSCROLL, DoVScrollScrollWnd, }; // Structure defining the controls in the window CTLWNDSTRUCT Scrolls [] = { {TEXT ("Scroll bar"), IDC_LRSCROLL, TEXT (""), 10, 10, 150, 23, SBS_HORZ}, {TEXT ("Scroll bar"), IDC_UDSCROLL, TEXT (""), 180, 10, 23, 120, SBS_VERT}, }; // Structure labeling the scroll bar control scroll codes for WM_VSCROLL NOTELABELS nlVScroll[] = {{TEXT ("SB_LINEUP "), 0}, {TEXT ("SB_LINEDOWN "), 1}, {TEXT ("SB_PAGEUP "), 2}, {TEXT ("SB_PAGEDOWN "), 3}, {TEXT ("SB_THUMBPOSITION"), 4}, {TEXT ("SB_THUMBTRACK "), 5}, {TEXT ("SB_TOP "), 6}, {TEXT ("SB_BOTTOM "), 7}, {TEXT ("SB_ENDSCROLL "), 8}, }; // Structure labeling the scroll bar control scroll codes for WM_HSCROLL NOTELABELS nlHScroll[] = {{TEXT ("SB_LINELEFT "), 0}, {TEXT ("SB_LINERIGHT "), 1}, {TEXT ("SB_PAGELEFT "), 2}, {TEXT ("SB_PAGERIGHT "), 3}, {TEXT ("SB_THUMBPOSITION"), 4}, {TEXT ("SB_THUMBTRACK "), 5}, {TEXT ("SB_LEFT "), 6}, {TEXT ("SB_RIGHT "), 7}, {TEXT ("SB_ENDSCROLL "), 8}, }; //---------------------------------------------------------------------- // InitScrollWnd - ScrollWnd window initialization // int InitScrollWnd (HINSTANCE hInstance) { WNDCLASS wc; // Register application ScrollWnd window class. wc.style = 0; // Window style wc.lpfnWndProc = ScrollWndProc; // Callback function wc.cbClsExtra = 0; // Extra class data wc.cbWndExtra = 0; // Extra window data wc.hInstance = hInstance; // Owner handle wc.hIcon = NULL, // Application icon wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = SCROLLWND; // Window class name if (RegisterClass (&wc) == 0) return 1; return 0; } //====================================================================== // Message handling procedures for ScrollWindow //---------------------------------------------------------------------- // ScrollWndProc - Callback function for application window // LRESULT CALLBACK ScrollWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; // // Search message list to see if we need to handle this // message. If in list, call procedure. // for (i = 0; i < dim(ScrollWndMessages); i++) { if (wMsg == ScrollWndMessages[i].Code) return (*ScrollWndMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); } return DefWindowProc (hWnd, wMsg, wParam, lParam); } //---------------------------------------------------------------------- // DoCreateScrollWnd - Process WM_CREATE message for window. // LRESULT DoCreateScrollWnd (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { INT i; for (i = 0; i < dim(Scrolls); i++) { CreateWindow (Scrolls[i].szClass, Scrolls[i].szTitle, Scrolls[i].lStyle | WS_VISIBLE | WS_CHILD, Scrolls[i].x, Scrolls[i].y, Scrolls[i].cx, Scrolls[i].cy, hWnd, (HMENU) Scrolls[i].nID, hInst, NULL); } return 0; } //---------------------------------------------------------------------- // DoVScrollScrollWnd - Process WM_VSCROLL message for window. // LRESULT DoVScrollScrollWnd (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) { TCHAR szOut[128]; SCROLLINFO si; INT i, sPos; // Update the report window. if (GetDlgItem (hWnd, 101) == (HWND)lParam) { for (i = 0; i < dim(nlVScroll); i++) { if (LOWORD (wParam) == nlVScroll[i].wNotification) { lstrcpy (szOut, nlVScroll[i].pszLabel); break; } } if (i == dim(nlVScroll)) wsprintf (szOut, TEXT ("notification: %x"), HIWORD (wParam)); } else { for (i = 0; i < dim(nlHScroll); i++) { if (LOWORD (wParam) == nlHScroll[i].wNotification) { lstrcpy (szOut, nlHScroll[i].pszLabel); break; } } if (i == dim(nlHScroll)) wsprintf (szOut, TEXT ("notification: %x"), HIWORD (wParam)); } SendMessage (GetParent (hWnd), MYMSG_ADDLINE, -1, (LPARAM)szOut); // Get scroll bar position. si.cbSize = sizeof (si); si.fMask = SIF_POS; GetScrollInfo ((HWND)lParam, SB_CTL, &si); sPos = si.nPos; // Act on the scroll code. switch (LOWORD (wParam)) { case SB_LINEUP: // Also SB_LINELEFT sPos -= 2; break; case SB_LINEDOWN: // Also SB_LINERIGHT sPos += 2; break; case SB_PAGEUP: // Also SB_PAGELEFT sPos -= 10; break; case SB_PAGEDOWN: // Also SB_PAGERIGHT sPos += 10; break; case SB_THUMBPOSITION: sPos = HIWORD (wParam); break; } // Check range. if (sPos < 0) sPos = 0; if (sPos > 100) sPos = 100; // Update scroll bar position. si.cbSize = sizeof (si); si.nPos = sPos; si.fMask = SIF_POS; SetScrollInfo ((HWND)lParam, SB_CTL, &si, TRUE); return 0; }
#include <windows.h> LONG WINAPI WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { WNDCLASS wc; HWND hwnd; MSG msg; wc.style = 0; // Class style wc.lpfnWndProc = (WNDPROC) WndProc; // Window procedure address wc.cbClsExtra = 0; // Class extra bytes wc.cbWndExtra = 0; // Window extra bytes wc.hInstance = hInstance; // Instance handle wc.hIcon = LoadIcon (NULL, IDI_WINLOGO); // Icon handle wc.hCursor = LoadCursor (NULL, IDC_ARROW); // Cursor handle wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // Background color wc.lpszMenuName = NULL; // Menu name wc.lpszClassName = "MyWndClass"; // WNDCLASS name RegisterClass (&wc); hwnd = CreateWindow ( "MyWndClass", // WNDCLASS name "SDK Application", // Window title WS_OVERLAPPEDWINDOW, // Window style CW_USEDEFAULT, // Horizontal position CW_USEDEFAULT, // Vertical position CW_USEDEFAULT, // Initial width CW_USEDEFAULT, // Initial height HWND_DESKTOP, // Handle of parent window NULL, // Menu handle hInstance, // Application's instance handle NULL // Window-creation data ); ShowWindow (hwnd, nCmdShow); UpdateWindow (hwnd); while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint (hwnd, &ps); Ellipse (hdc, 0, 0, 200, 100); EndPaint (hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage (0); return 0; } return DefWindowProc (hwnd, message, wParam, lParam); }