DialogTemplate.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | #include "stdafx.h" #include <Windows.h> class DialogTemplate { public : DialogTemplate( LPCTSTR caption, DWORD style, int x, int y, int w, int h, LPCTSTR font = NULL, WORD fontSize = 8) { usedBufferLength = sizeof (DLGTEMPLATE); totalBufferLength = usedBufferLength; dialogTemplate = (DLGTEMPLATE*) malloc (totalBufferLength); dialogTemplate->style = style; if (font != NULL) { dialogTemplate->style |= DS_SETFONT; } dialogTemplate->x = x; dialogTemplate->y = y; dialogTemplate->cx = w; dialogTemplate->cy = h; dialogTemplate->cdit = 0; dialogTemplate->dwExtendedStyle = 0; // The dialog box doesn't have a menu or a special class AppendData(_T( "\0" ), 2); AppendData(_T( "\0" ), 2); // Add the dialog's caption to the template AppendString(caption); if (font != NULL) { AppendData(&fontSize, sizeof ( WORD )); AppendString(font); } } void AddComponent( LPCTSTR type, LPCTSTR caption, DWORD style, DWORD exStyle, int x, int y, int w, int h, WORD id) { DLGITEMTEMPLATE item; item.style = style; item.x = x; item.y = y; item.cx = w; item.cy = h; item.id = id; item.dwExtendedStyle = exStyle; AppendData(&item, sizeof (DLGITEMTEMPLATE)); AppendString(type); AppendString(caption); WORD creationDataLength = 0; AppendData(&creationDataLength, sizeof ( WORD )); // Increment the component count dialogTemplate->cdit++; } void AddButton( LPCTSTR caption, DWORD style, DWORD exStyle, int x, int y, int w, int h, WORD id) { AddStandardComponent(0x0080, caption, style, exStyle, x, y, w, h, id); WORD creationDataLength = 0; AppendData(&creationDataLength, sizeof ( WORD )); } void AddEditBox( LPCTSTR caption, DWORD style, DWORD exStyle, int x, int y, int w, int h, WORD id) { AddStandardComponent(0x0081, caption, style, exStyle, x, y, w, h, id); WORD creationDataLength = 0; AppendData(&creationDataLength, sizeof ( WORD )); } void AddStatic( LPCTSTR caption, DWORD style, DWORD exStyle, int x, int y, int w, int h, WORD id) { AddStandardComponent(0x0082, caption, style, exStyle, x, y, w, h, id); WORD creationDataLength = 0; AppendData(&creationDataLength, sizeof ( WORD )); } void AddListBox( LPCTSTR caption, DWORD style, DWORD exStyle, int x, int y, int w, int h, WORD id) { AddStandardComponent(0x0083, caption, style, exStyle, x, y, w, h, id); WORD creationDataLength = 0; AppendData(&creationDataLength, sizeof ( WORD )); } void AddScrollBar( LPCTSTR caption, DWORD style, DWORD exStyle, int x, int y, int w, int h, WORD id) { AddStandardComponent(0x0084, caption, style, exStyle, x, y, w, h, id); WORD creationDataLength = 0; AppendData(&creationDataLength, sizeof ( WORD )); } void AddComboBox( LPCTSTR caption, DWORD style, DWORD exStyle, int x, int y, int w, int h, WORD id) { AddStandardComponent(0x0085, caption, style, exStyle, x, y, w, h, id); WORD creationDataLength = 0; AppendData(&creationDataLength, sizeof ( WORD )); } /** * Returns a pointer to the Win32 dialog template which the object * represents. This pointer may become invalid if additional * components are added to the template. */ operator const DLGTEMPLATE*() const { return dialogTemplate; } virtual ~DialogTemplate() { free (dialogTemplate); } protected : void AddStandardComponent( WORD type, LPCTSTR caption, DWORD style, DWORD exStyle, int x, int y, int w, int h, WORD id) { DLGITEMTEMPLATE item; // DWORD algin the beginning of the component data AlignData( sizeof ( DWORD )); item.style = style; item.x = x; item.y = y; item.cx = w; item.cy = h; item.id = id; item.dwExtendedStyle = exStyle; AppendData(&item, sizeof (DLGITEMTEMPLATE)); WORD preType = 0xFFFF; AppendData(&preType, sizeof ( WORD )); AppendData(&type, sizeof ( WORD )); AppendString(caption); // Increment the component count dialogTemplate->cdit++; } void AlignData( int size) { int paddingSize = usedBufferLength % size; if (paddingSize != 0) { EnsureSpace(paddingSize); usedBufferLength += paddingSize; } } void AppendString( LPCTSTR string) { AppendData( LPVOID (string), _tcslen(string) * sizeof ( TCHAR )); } void AppendData( void * data, int dataLength) { EnsureSpace(dataLength); memcpy (( char *)dialogTemplate + usedBufferLength, data, dataLength); usedBufferLength += dataLength; } void EnsureSpace( int length) { if (length + usedBufferLength > totalBufferLength) { totalBufferLength += length * 2; void * newBuffer = malloc (totalBufferLength); memcpy (newBuffer, dialogTemplate, usedBufferLength); free (dialogTemplate); dialogTemplate = (DLGTEMPLATE*)newBuffer; } } private : DLGTEMPLATE* dialogTemplate; int totalBufferLength; int usedBufferLength; }; |
References
- http://www.flipcode.com/archives/Dialog_Template.shtml
- https://stackoverflow.com/questions/24995712/a-simple-message-box-with-please-wait-message-without-buttons
- https://stackoverflow.com/questions/61634/windows-api-dialogs-without-using-resource-files