//+--------------------------------------------------------------------------- // // HELLO_WIN.C - Windows GUI 'Hello World!' Example // // https://stackoverflow.com/questions/12537456/how-to-append-text-to-a-textbox+--------------------------------------------------------------------------- /* usage ..\tcc hello_win_GUI.c hello_win_GUI.c * I need a gui witha few buttons and status. I need a structure to act as the interface. Buttons: quit hangup dial send Status: modem connection incoming text outgoing text Interface: use MicroCip flags. send available */ /* interface */ struct interfaceT { int flag1; int flag2; int flag3; } io ; #include #define APPNAME "HELLO_WIN" #define APPNAME2 "HELLO_WIN - 2" #define BUFFERLEN 200 #define BTN_START 100 #define BTN_STOP 101 #define BTN_PLUS 102 #define BTN_QUIT 103 #define BTN_HANGUP 104 #define BTN_DIAL 105 #define BTN_SEND 106 #define IDC_EDIT1 110 #define IDC_EDIT2 111 #define IDC_EDIT3 112 #define ID_EDITCHILD 113 #define IDT_TIMER1 1 #define IDT_TIMER2 2 char szAppName[] = APPNAME; // The name of this application char szAppName2[] = APPNAME2; // The name of this application char szTitle[] = APPNAME; // The title bar text char szTitle2[] = APPNAME2; // The title bar text char buffer[ BUFFERLEN+1 ]; const char *pWindowText; static HWND hWndEdit1; static HWND hWndEdit2; static HWND hWndEdit3; static HWND hWndEdit4; static HWND hWndEdit5; static HWND hWndEdit6; SCROLLINFO sb1; LPSTR lpString; HPEN hPausePen; HPEN hRedPen; HPEN hGreenPen; void CenterWindow(HWND hWnd); //+--------------------------------------------------------------------------- // // Function: WndProc // // Synopsis: very unusual type of function - gets called by system to // process windows messages. // // Arguments: same as always. //---------------------------------------------------------------------------- LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { // ----------------------- first and last case WM_CREATE: CenterWindow(hwnd); hRedPen = CreatePen(PS_SOLID,4,RGB(255,32,32)); hGreenPen = CreatePen(PS_SOLID,4,RGB(32,255,32)); break; case WM_DESTROY: PostQuitMessage(0); break; // ----------------------- get out of it... case WM_RBUTTONUP: DestroyWindow(hwnd); break; case WM_KEYDOWN: if (VK_ESCAPE == wParam) DestroyWindow(hwnd); if (wParam == 'G'){ pWindowText = "You pressed G"; io.flag1 = 1; io.flag2 = 1; InvalidateRect(hwnd,NULL,TRUE); } if (wParam == 'Z'){ io.flag1 = 0; io.flag2 = 0; pWindowText = "You pressed Z"; InvalidateRect(hwnd,NULL,TRUE); } if (wParam == 'a'){ pWindowText = "You pressed a"; InvalidateRect(hwnd,NULL,TRUE); GetWindowTextA(hWndEdit1, buffer,BUFFERLEN); SetWindowTextA(hWndEdit2, buffer ); } break; case WM_COMMAND: if ( LOWORD( wParam ) == BTN_QUIT ){ io.flag1 = 0; io.flag2 = 0; } if ( LOWORD( wParam ) == BTN_HANGUP ){ io.flag1 = 0; io.flag2 = 1; } if ( LOWORD( wParam ) == BTN_DIAL ){ io.flag1 = 1; io.flag2 = 0; SetTimer(hwnd, // handle to main window IDT_TIMER1, // timer identifier 1750, // 1 second interval (TIMERPROC) NULL); // no timer callback SendMessage(hWndEdit1,EM_SETSEL,0,-1); //no difference between passing 0 or -1 SendMessage(hWndEdit1,EM_REPLACESEL,TRUE,(LPARAM)"Dial"); } if ( LOWORD( wParam ) == BTN_SEND ){ io.flag1 = 1; io.flag2 = 1; } //pWindowText = "You pressed the button"; //InvalidateRect(hwnd,NULL,TRUE); if ( LOWORD( wParam ) == BTN_START ){ pWindowText = "You pressed the START button"; // https://docs.microsoft.com/en-us/windows/desktop/Controls/em-setsel SendMessage(hWndEdit1,EM_SETSEL,0,-1); //no difference between passing 0 or -1 SendMessage(hWndEdit1,EM_REPLACESEL,TRUE,(LPARAM)"===="); SendMessage(hWndEdit4,EM_SETSEL,2,4); //no difference between passing 0 or -1 SendMessage(hWndEdit4,EM_REPLACESEL,TRUE,(LPARAM)"hello"); //InvalidateRect(hwnd,NULL,TRUE); hPausePen = CreatePen(PS_SOLID,4,RGB(32,255,32)); io.flag1 = 0; io.flag2 = 1; } if ( LOWORD( wParam ) == BTN_STOP ){ pWindowText = "You pressed the STOP button"; //InvalidateRect(hwnd,NULL,TRUE); hPausePen = CreatePen(PS_SOLID,4,RGB(255,32,32)); GetWindowTextA(hWndEdit1, buffer,BUFFERLEN); SetWindowTextA(hWndEdit2, buffer ); /* NOT WORKING OR COMPLETE https://docs.microsoft.com/en-gb/windows/desktop/api/winuser/ns-winuser-tagscrollinfo SCROLLINFO sb1; GetScrollInfo( hWndEdit5, SB_CTL, &sb1 ); sb1.nPos += 5; SetScrollInfo( hWndEdit5, SB_CTL, &sb1, TRUE ); */ io.flag1 = 1; io.flag2 = 0; } InvalidateRect(hwnd,NULL,TRUE); break; // https://stackoverflow.com/questions/20640330/winapi-bn-clicked-how-to-identify-which-button-was-clicked // #define LINE(b,x1,y1,x2,y2) { SelectObject(hDC,b & New ? hUsePen : hOffPen);MoveToEx(hDC,x1,y1,(LPPOINT) NULL );LineTo(hDC,x2,y2); } case WM_TIMER: switch (wParam) { case IDT_TIMER1: // pWindowText = pT2; // GetCursorPos(&ptNow); io.flag1 = 0; io.flag2 = 0; SendMessage(hWndEdit1,EM_SETSEL,0,-1); //no difference between passing 0 or -1 SendMessage(hWndEdit1,EM_REPLACESEL,TRUE,(LPARAM)"timed out"); InvalidateRect(hwnd,NULL,TRUE); return 0; case IDT_TIMER2: // process the five-minute timer //break; return 0; } break; // ----------------------- display our minimal info case WM_PAINT: { PAINTSTRUCT ps; HDC hdc; RECT rc; int xpos, ypos; hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rc); ypos = rc.top +5; if ( io.flag1 ) { SelectObject(hdc, hGreenPen ); } else { SelectObject(hdc, hRedPen ); } MoveToEx( hdc,rc.left+10, ypos ,(LPPOINT) NULL ); LineTo( hdc,rc.left+20, ypos ); if ( io.flag2 ) { SelectObject(hdc, hGreenPen ); } else { SelectObject(hdc, hRedPen ); } MoveToEx( hdc,rc.left+30, ypos ,(LPPOINT) NULL ); LineTo( hdc,rc.left+40, ypos ); ypos = rc.bottom - ( rc.bottom - rc.top )/4; SelectObject(hdc, hPausePen ); MoveToEx( hdc,rc.left, ypos ,(LPPOINT) NULL ); LineTo( hdc,rc.right, ypos ); SetTextColor(hdc, RGB(240,240,96)); SetBkMode(hdc, TRANSPARENT); DrawText(hdc, pWindowText, -1, &rc, DT_CENTER|DT_SINGLELINE|DT_VCENTER); EndPaint(hwnd, &ps); break; } // ----------------------- let windows do all other stuff default: return DefWindowProc(hwnd, message, wParam, lParam); } return 0; } //+--------------------------------------------------------------------------- // // Function: WinMain // // Synopsis: standard entrypoint for GUI Win32 apps // //---------------------------------------------------------------------------- int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { MSG msg; WNDCLASS wc; HWND hwnd; HWND hwnd2; /* HWND hWndEdit1; HWND hWndEdit2; HWND hWndEdit3; */ HWND hWndButton1; HWND hWndButton2; /* Buttons: quit hangup dial send */ HWND hWndButtonQuit; HWND hWndButtonHangup; HWND hWndButtonDial; HWND hWndButtonSend; pWindowText = lpCmdLine[0] ? lpCmdLine : "Hello Windows! - doug "; // Fill in window class structure with parameters that describe // the main window. ZeroMemory(&wc, sizeof wc); wc.hInstance = hInstance; wc.lpszClassName = szAppName; wc.lpfnWndProc = (WNDPROC)WndProc; wc.style = CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW; // wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // wc.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH); wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); wc.hIcon = LoadIcon( NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); if (FALSE == RegisterClass(&wc)) return 0; hPausePen = CreatePen(PS_SOLID,4,RGB(128,128,128)); // create the browser hwnd = CreateWindow( szAppName, szTitle, WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 360,//CW_USEDEFAULT, 440,//CW_USEDEFAULT, 0, 0, hInstance, 0); /* display 2nd pop up window */ // create the browser /* hwnd2 = CreateWindow( szAppName, szTitle2, WS_OVERLAPPEDWINDOW|WS_VISIBLE, //WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 380,//CW_USEDEFAULT, 220,//CW_USEDEFAULT, 0, 0, hInstance, 0); */ hWndEdit1 = CreateWindow(TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 10, 140, 20, hwnd, NULL, NULL, NULL); hWndEdit2 = CreateWindow(TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 30, 140, 20, hwnd, NULL, NULL, NULL); hWndEdit3 = CreateWindow(TEXT("Edit"), TEXT("start"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 50, 140, 20, hwnd, ( HMENU )BTN_PLUS, NULL, NULL); // hWndButton1 = CreateWindow(TEXT("Button"), TEXT("start"), WS_CHILD | WS_VISIBLE | WS_BORDER | BS_RADIOBUTTON, 10, 70, 70, 20, hwnd, ( HMENU )BTN_START, NULL, NULL); hWndButton1 = CreateWindow(TEXT("Button"), TEXT("start"), WS_CHILD | WS_VISIBLE | WS_BORDER , 100, 70, 70, 20, hwnd, ( HMENU )BTN_START, NULL, NULL); hWndButton2 = CreateWindow(TEXT("Button"), TEXT("stop"), WS_CHILD | WS_VISIBLE | WS_BORDER , 180, 70, 70, 20, hwnd, ( HMENU )BTN_STOP, NULL, NULL); /* HWND hWndButtonQuit; HWND hWndButtonHangup; HWND hWndButtonDial; HWND hWndButtonSend; */ hWndButtonQuit = CreateWindow(TEXT("Button"), TEXT("Quit"), WS_CHILD | WS_VISIBLE | WS_BORDER , 10, 10, 70, 20, hwnd, ( HMENU )BTN_QUIT, NULL, NULL); hWndButtonHangup = CreateWindow(TEXT("Button"), TEXT("Hangup"), WS_CHILD | WS_VISIBLE | WS_BORDER , 10, 30, 70, 20, hwnd, ( HMENU )BTN_HANGUP, NULL, NULL); hWndButtonDial = CreateWindow(TEXT("Button"), TEXT("Dial"), WS_CHILD | WS_VISIBLE | WS_BORDER , 10, 50, 70, 20, hwnd, ( HMENU )BTN_DIAL, NULL, NULL); hWndButtonSend = CreateWindow(TEXT("Button"), TEXT("Send"), WS_CHILD | WS_VISIBLE | WS_BORDER , 10, 70, 70, 20, hwnd, ( HMENU )BTN_SEND, NULL, NULL); // hInst hWndEdit4 = CreateWindow(TEXT("EDIT"), TEXT("Edit\n4\ntxt"), // WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL | ES_WANTRETURN, WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL, 10, 100, 200, 90, hwnd, (HMENU)IDC_EDIT1, // edit control ID (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), 0); hWndEdit5 = CreateWindow(TEXT("ScrollBar"), TEXT("Edit\n5\ntxt"), // WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL | ES_WANTRETURN, WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL, 10, 210, 200, 20, hwnd, (HMENU)IDC_EDIT2, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), 0); hWndEdit6 = CreateWindow(TEXT("Static"), TEXT("Edit\n5\ntxt"), // WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL | ES_WANTRETURN, WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL, 10, 240, 200, 50, hwnd, (HMENU)IDC_EDIT3, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), 0); /* Class Description Button The class for a button. ComboBox The class for a combo box. Edit The class for an edit control. ListBox The class for a list box. MDIClient The class for an MDI client window. ScrollBar The class for a scroll bar. Static The class for a static control. */ /* ShowWindow(hWndEdit1,nCmdShow); ShowWindow(hWndEdit2,nCmdShow); ShowWindow(hWndEdit3,nCmdShow); */ /* https://docs.microsoft.com/en-us/windows/desktop/controls/create-a-command-link */ if ( NULL == hwnd ) return 0; /* if ( NULL == hwnd2 ) return 0; */ if ( hWndEdit1 == NULL ) return 0; if ( hWndEdit2 == NULL ) return 0; if ( hWndEdit3 == NULL ) return 0; // Main message loop: while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } //+--------------------------------------------------------------------------- //+--------------------------------------------------------------------------- void CenterWindow(HWND hwnd_self) { HWND hwnd_parent; RECT rw_self, rc_parent, rw_parent; int xpos, ypos; hwnd_parent = GetParent(hwnd_self); if (NULL == hwnd_parent) hwnd_parent = GetDesktopWindow(); GetWindowRect(hwnd_parent, &rw_parent); GetClientRect(hwnd_parent, &rc_parent); GetWindowRect(hwnd_self, &rw_self); xpos = rw_parent.left + (rc_parent.right + rw_self.left - rw_self.right) / 2; ypos = rw_parent.top + (rc_parent.bottom + rw_self.top - rw_self.bottom) / 2; SetWindowPos( hwnd_self, NULL, xpos, ypos, 0, 0, SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE ); } //+---------------------------------------------------------------------------