Changeset 126 for pjproject/trunk/pjlib/src/pjlib-test/main_win32.c
- Timestamp:
- Jan 20, 2006 9:03:36 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/src/pjlib-test/main_win32.c
r125 r126 20 20 21 21 #include <pj/string.h> 22 #include <pj/ compat/unicode.h>22 #include <pj/unicode.h> 23 23 #include <pj/sock.h> 24 24 #include <pj/log.h> 25 25 26 #define WIN32_LEAN_AND_MEAN 27 #define NONAMELESSUNION 26 28 #include <windows.h> 27 29 #include <commctrl.h> 28 29 #define MAX_LOADSTRING 100 30 31 #define IDC_HELLO_WINCE 3 32 #define ID_LOGWINDOW 104 33 ///#define IDI_HELLO_WINCE 101 34 ///#define IDM_MENU 102 35 ///#define IDD_ABOUTBOX 103 36 ///#define IDM_FILE_EXIT 40002 37 ///#define IDM_HELP_ABOUT 40003 38 39 // Global Variables: 40 HINSTANCE hInst; // The current instance 41 ///HWND hwndCB; // The command bar handle 42 HWND hwLogWnd; 43 44 // Forward declarations of functions included in this code module: 45 ATOM MyRegisterClass (HINSTANCE, LPTSTR); 46 BOOL InitInstance (HINSTANCE, int); 47 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); 48 ///LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM); 49 50 static TCHAR logbuf[8192]; 51 PJ_DECL_UNICODE_TEMP_BUF(wdata,256); 30 #include <tchar.h> 31 32 #define MAX_LOADSTRING 100 33 #define THIS_FILE "main_win32.c" 34 35 #define IDC_HELLO_WINCE 3 36 #define ID_LOGWINDOW 104 37 38 39 ATOM MyRegisterClass (HINSTANCE, LPTSTR); 40 BOOL InitInstance (HINSTANCE, int); 41 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); 42 43 44 extern int param_log_decor; // in test.c 45 46 static HINSTANCE hInst; 47 static HWND hwndLog; 48 static HFONT hFixedFont; 49 52 50 53 51 static void write_log(int level, const char *data, int len) 54 52 { 55 GetWindowText(hwLogWnd, logbuf, PJ_ARRAY_SIZE(logbuf));56 wcscat(logbuf, PJ_NATIVE_STRING(data,wdata)); 57 SetWindowText(hwLogWnd, logbuf);58 UpdateWindow(hwLogWnd);59 } 60 61 62 int WINAPI WinMain(HINSTANCE hInstance, 63 HINSTANCE hPrevInstance, 64 LPTSTR lpCmdLine, 65 intnCmdShow)53 PJ_DECL_UNICODE_TEMP_BUF(wdata,256); 54 55 PJ_UNUSED_ARG(level); 56 PJ_UNUSED_ARG(len); 57 SendMessage(hwndLog, EM_REPLACESEL, FALSE, 58 (LPARAM)PJ_STRING_TO_NATIVE(data,wdata)); 59 } 60 61 62 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 63 LPTSTR lpCmdLine, int nCmdShow) 66 64 { 67 65 MSG msg; 68 HACCEL hAccelTable; 69 70 // Perform application initialization: 71 if (!InitInstance (hInstance, nCmdShow)) 72 { 66 67 PJ_UNUSED_ARG(lpCmdLine); 68 PJ_UNUSED_ARG(hPrevInstance); 69 70 71 if (!InitInstance (hInstance, nCmdShow)) 73 72 return FALSE; 73 74 pj_log_set_log_func( &write_log ); 75 param_log_decor = PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR; 76 77 // Run the test! 78 test_main(); 79 80 PJ_LOG(3,(THIS_FILE,"")); 81 PJ_LOG(3,(THIS_FILE,"Press ESC to quit")); 82 83 // Message loop, waiting to quit. 84 while (GetMessage(&msg, NULL, 0, 0)) { 85 TranslateMessage(&msg); 86 DispatchMessage(&msg); 74 87 } 75 88 76 pj_log_set_log_func( &write_log ); 77 pj_log_set_decor(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR); 78 79 test_main(); 80 81 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_HELLO_WINCE); 82 83 // Main message loop: 84 while (GetMessage(&msg, NULL, 0, 0)) 85 { 86 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 87 { 88 TranslateMessage(&msg); 89 DispatchMessage(&msg); 90 } 91 } 92 89 DeleteObject(hFixedFont); 93 90 return msg.wParam; 94 91 } 95 92 96 // 97 // FUNCTION: MyRegisterClass() 98 // 99 // PURPOSE: Registers the window class. 100 // 101 // COMMENTS: 102 // 103 // It is important to call this function so that the application 104 // will get 'well formed' small icons associated with it. 105 // 93 94 #ifdef _CONSOLE 95 int main() 96 { 97 return WinMain(GetModuleHandle(NULL), NULL, NULL, SW_SHOW); 98 } 99 #endif 100 101 106 102 ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass) 107 103 { … … 123 119 } 124 120 125 // 126 // FUNCTION: InitInstance(HANDLE, int) 127 // 128 // PURPOSE: Saves instance handle and creates main window 129 // 130 // COMMENTS: 131 // 132 // In this function, we save the instance handle in a global variable and 133 // create and display the main program window. 134 // 121 135 122 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 136 123 { 137 124 HWND hWnd; 138 TCHAR *szTitle = L"PJSIP Test"; 139 TCHAR *szWindowClass = L"PJSIP_TEST"; 140 141 hInst = hInstance; // Store instance handle in our global variable 125 TCHAR *szTitle = _T("PJSIP Test"); 126 TCHAR *szWindowClass = _T("PJSIP_TEST"); 127 LOGFONT lf; 128 129 130 memset(&lf, 0, sizeof(lf)); 131 lf.lfHeight = 13; 132 #if PJ_NATIVE_STRING_IS_UNICODE 133 wcscpy(lf.lfFaceName, _T("Courier New")); 134 #else 135 strcpy(lf.lfFaceName, "Lucida Console"); 136 #endif 137 138 hFixedFont = CreateFontIndirect(&lf); 139 if (!hFixedFont) 140 return FALSE; 141 142 hInst = hInstance; 142 143 143 144 MyRegisterClass(hInstance, szWindowClass); 144 145 145 146 hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE, 146 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); 147 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 148 CW_USEDEFAULT, NULL, NULL, hInstance, NULL); 147 149 148 150 if (!hWnd) 149 {150 151 return FALSE; 151 }152 152 153 153 ShowWindow(hWnd, nCmdShow); 154 154 UpdateWindow(hWnd); 155 ///if (hwndCB) 156 /// CommandBar_Show(hwndCB, TRUE); 157 if (hwLogWnd) 158 ShowWindow(hwLogWnd, TRUE); 155 156 if (hwndLog) { 157 SendMessage(hwndLog, WM_SETFONT, (WPARAM) hFixedFont, (LPARAM) 0); 158 ShowWindow(hwndLog, TRUE); 159 } 160 159 161 return TRUE; 160 162 } 161 163 162 // 163 // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) 164 // 165 // PURPOSE: Processes messages for the main window. 166 // 167 // WM_COMMAND - process the application menu 168 // WM_PAINT - Paint the main window 169 // WM_DESTROY - post a quit message and return 170 // 171 // 164 172 165 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 173 166 { 174 int wmId, wmEvent;175 167 RECT rt; 176 168 DWORD dwStyle; 177 TCHAR *szHello = L"Hello world!";178 169 179 170 switch (message) 180 171 { 181 case WM_COMMAND: 182 wmId = LOWORD(wParam); 183 wmEvent = HIWORD(wParam); 184 // Parse the menu selections: 185 switch (wmId) 186 { 187 ///case IDM_HELP_ABOUT: 188 ///DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); 189 /// break; 190 ///case IDM_FILE_EXIT: 191 /// DestroyWindow(hWnd); 192 /// break; 193 default: 194 return DefWindowProc(hWnd, message, wParam, lParam); 172 case WM_CREATE: 173 // Create text control. 174 GetClientRect(hWnd, &rt); 175 dwStyle = WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | 176 WS_BORDER | ES_LEFT | ES_MULTILINE | ES_NOHIDESEL | 177 ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_READONLY; 178 hwndLog = CreateWindow( TEXT("edit"), // class 179 NULL, // window text 180 dwStyle, // style 181 0, // x-left 182 0, // y-top 183 rt.right-rt.left, // w 184 rt.bottom-rt.top, // h 185 hWnd, // parent 186 (HMENU)ID_LOGWINDOW,// id 187 hInst, // instance 188 NULL); // NULL for control. 189 break; 190 case WM_ACTIVATE: 191 if (LOWORD(wParam) == WA_INACTIVE) 192 DestroyWindow(hWnd); 193 break; 194 case WM_CHAR: 195 if (wParam == 27) { 196 DestroyWindow(hWnd); 195 197 } 196 198 break; 197 case WM_CREATE: 198 ///hwndCB = CommandBar_Create(hInst, hWnd, 1); 199 ///CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0); 200 ///CommandBar_AddAdornments(hwndCB, 0, 0); 201 GetClientRect(hWnd, &rt); 202 dwStyle = WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | 203 WS_BORDER | ES_LEFT | ES_MULTILINE | ES_NOHIDESEL | 204 ES_AUTOHSCROLL | ES_AUTOVSCROLL; 205 hwLogWnd = CreateWindow( TEXT("edit"), // class 206 NULL, // window text 207 dwStyle, // style 208 0, // x-left 209 0, // y-top 210 rt.right-rt.left, // w 211 rt.bottom-rt.top, // h 212 hWnd, // parent 213 (HMENU)ID_LOGWINDOW, // id 214 hInst, // instance 215 NULL); // NULL for control. 216 break; 217 case WM_PAINT: 218 ///hdc = BeginPaint(hWnd, &ps); 219 ///GetClientRect(hWnd, &rt); 220 ///DrawText(hdc, szHello, _tcslen(szHello), &rt, 221 /// DT_SINGLELINE | DT_VCENTER | DT_CENTER); 222 ///EndPaint(hWnd, &ps); 223 break; 224 case WM_ACTIVATE: 225 if (LOWORD(wParam) == WA_INACTIVE) 226 DestroyWindow(hWnd); 227 break; 228 case WM_CLOSE: 229 DestroyWindow(hWnd); 230 break; 231 case WM_DESTROY: 232 ///CommandBar_Destroy(hwndCB); 233 PostQuitMessage(0); 234 break; 235 default: 236 return DefWindowProc(hWnd, message, wParam, lParam); 199 case WM_CLOSE: 200 DestroyWindow(hWnd); 201 break; 202 case WM_DESTROY: 203 PostQuitMessage(0); 204 break; 205 default: 206 return DefWindowProc(hWnd, message, wParam, lParam); 237 207 } 238 208 return 0;
Note: See TracChangeset
for help on using the changeset viewer.