simple gui in c++
DESCRIPTION
Simple Gui in C++. Project Selecting. So Far. Registering the window class Creating the window. So Far. The major function/Handling Messages. WndProc Return value: LRESULT CALLBACK Parameters: HWND hWnd UINT message WPARAM wParam - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/1.jpg)
Simple Gui in C++
![Page 2: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/2.jpg)
Project Selecting
![Page 3: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/3.jpg)
![Page 4: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/4.jpg)
So Far
• Registering the window class
• Creating the window
![Page 5: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/5.jpg)
So Far
![Page 6: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/6.jpg)
The major function/Handling Messages
• WndProc• Return value: LRESULT CALLBACK• Parameters: HWND hWnd
UINT message WPARAM wParam LPARAM lParam):• The function handles all events (message):
– WM_CREATE– WM_PAINT– WM_LBUTTONUP
![Page 7: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/7.jpg)
Handling Messages(Cont)
• Historically wParam 16 bit lParam 32 bit; Today both 32 bit
• Each message use those parameters differently
• For example– WM_LBUTTON - lParam stores coordinates
• LOWORD, HIWORD – extract two words
![Page 8: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/8.jpg)
Message Queue
• There are no interrupts• When messages are posted they are added
to the message queue • After handling them they are removed
![Page 9: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/9.jpg)
Working with Rectangles
• GetWindowRect(m_hWnd,&rt1) – return the window rectangle in screen coordinates
• GetClientRect(m_hWnd,&rt2) – return window rectangle in "itself" coordinates
•SetWindowPos(m_hWnd,NULL,0,0,x,y ,SWP_NOMOVE|SWP_NOZORDER);– Determines the window position and size.
![Page 10: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/10.jpg)
Painting
• When does painting occur?
• After receiving WM_PAINT
![Page 11: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/11.jpg)
How to Paint
• First get an handle to the screenhdc = BeginPaint(hWnd, &ps);
• Using hdc in each “painting” function.
• Do not forget– EndPaint(hWnd, &ps);
![Page 12: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/12.jpg)
Painting functions
• LineTo(hdc, int x, int y);– Line from current position to (x,y)
• MoveToEx(hdc, int x, int y, NULL)– Move the position to (x,y)
![Page 13: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/13.jpg)
case WM_PAINT:hdc = BeginPaint(hWnd, &ps);// TODO: Add any drawing code here...RECT rt,rt1;GetClientRect(hWnd, &rt);GetWindowRect(hWnd, &rt1);int x, y;y = (rt1.bottom - rt1.top) - rt.bottom;x = (rt1.right - rt1.left) - rt.right;
//DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);MoveToEx(hdc,0,0, NULL);LineTo(hdc, 100,0);LineTo(hdc, 100,100);LineTo(hdc, 0,100);LineTo(hdc,0,0);
SetWindowPos(hWnd,NULL,0,0,100 + x + 1, 100 + y + 1,SWP_NOMOVE|SWP_NOZORDER);
EndPaint(hWnd, &ps);
![Page 14: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/14.jpg)
Painting (Cont)
• CreatePen( int nPenStyle, int nWidth, COLORREF crColor )– Can paint in different styles and colors
• SelectObject(hdc, HPEN pen) – The next object will be painted by pen
• Ellipse(x,y,x1,y1)– The coordinates of the bounding rectangle
![Page 15: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/15.jpg)
Alternative Ways• CPen* SelectObject( hdc, CPen* pPen );• CBrush* SelectObject( hdc, CBrush* pBrush );
• CBitmap* SelectObject(hdc, CBitmap* pBitmap );
• Etc…
![Page 16: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/16.jpg)
Working with Bitmap(See Roy Bubis and Keren Michaeli
Proj)• Insert Bitmaps to Resources• LoadImage• CreateCompatibleDC ( hdc ); -
– Working on another hdc to avoid flickering• SelectObject(memdc, bmp); • TransparentBlt(hdc,x,y, dx, dy,
memdc,0,0,width, height, RGB(255,0,0));• //passing the content of medc to hdc•
![Page 17: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/17.jpg)
Forcing RePaint
• InvalidateRect(m_hWnd,&rect,TRUE); – signal the OS that this window is invalid and
need to be repaint. – The OS send all the invalid windows the
WM_PAINT message
![Page 18: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/18.jpg)
How does it look?
![Page 19: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/19.jpg)
Adding Options
![Page 21: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/21.jpg)
Java - Graphics
• Working with Graphics object• Graphics g
– DrawLine– DrawRect– SetColor– ….
![Page 22: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/22.jpg)
Applet
• An applet is a small, embeddable Java Program.
• “Usually” implementing a subclass of applet.
![Page 23: Simple Gui in C++](https://reader034.vdocuments.net/reader034/viewer/2022042516/568144db550346895db1a6bb/html5/thumbnails/23.jpg)
Handling events
• The events that we want to handle should be define in our subclass
• boolean mouseDown(Event e, int x, int y)• Boolean mouseDrag(Event e, int x, int y)• boolean keyDown(Event e, int x, int y)• Etc…