Maximize MenuMinimize Menu

 

 

TOP

Trajecto's Win32 API Programming Tutorial for Pocket PC

Let's Draw!

Now that we have our blank canvas, LET'S DRAW! We are going to learn two drawing functions: Ellipse() and PolyLine(). This code has been donated by Pelle himself from Pelles C. All drawing is done when the program receives a WM_PAINT message. The code contains two examples, one draws a red square (four lines), the other draws some circles. Change the line '#if 1' to '#if 0' to change between the two examples.

1. Start a new Pelles C WinCE Pocket PC Program (EXE) project just like we did before in the "Prototype Windows Application" lesson, but this time use the code below and compile it:

 

#include <windows.h>
#include <aygshell.h>

LRESULT MainWndProc(HWND hWnd, UINT message, WPARAM uParam, LPARAM lParam)
{
if(message == WM_LBUTTONDOWN)
{
DestroyWindow(hWnd);
return 0;
}
else if (message == WM_PAINT) // All painting is done here.
{
PAINTSTRUCT ps;

// BeginPaint() and EndPaint() are required for the WM_PAINT message.
// This will give us the very important 'Device context'.
BeginPaint(hWnd, &ps);

#if 1 // CHANGE THIS '1' to '0' FOR A SIMPLER EXAMPLE WITH LINES !!!!!
int x = 70, y = 100, radius = 20;

// Select a predefined (stock) brush into the 'Device context'. This 'hollow'
// brush will make sure the interior of the circles isn't filled. Remember
// the previous brush.
HBRUSH hbrSave = SelectObject(ps.hdc, GetStockObject(HOLLOW_BRUSH));
HPEN hpen, hpenSave;

// Create a 'pen' with the desired color. The pen is used to draw the circle.
hpen = CreatePen(PS_SOLID, 5, RGB(0,0,255));
// Select the pen into the 'Device context' and remember the previous pen.
hpenSave = SelectObject(ps.hdc, hpen);
// Draw the circle.
Ellipse(ps.hdc, x - radius, y - radius, x + radius, y + radius);
// Restore the previous pen in the 'Device context'.
SelectObject(ps.hdc, hpenSave);
// Destroy the pen we created in CreatePen() above.
DeleteObject(hpen);

// Same steps as above, but in new location with different color.
x += 25; y += 15;
hpen = CreatePen(PS_SOLID, 5, RGB(128,255,0));
hpenSave = SelectObject(ps.hdc, hpen);
Ellipse(ps.hdc, x - radius, y - radius, x + radius, y + radius);
SelectObject(ps.hdc, hpenSave);
DeleteObject(hpen);

// Same steps as above, but in new location with different color.
x += 25; y -= 15;
hpen = CreatePen(PS_SOLID, 5, RGB(0,0,0));
hpenSave = SelectObject(ps.hdc, hpen);
Ellipse(ps.hdc, x - radius, y - radius, x + radius, y + radius);
SelectObject(ps.hdc, hpenSave);
DeleteObject(hpen);

// Same steps as above, but in new location with different color.
x += 25; y += 15;
hpen = CreatePen(PS_SOLID, 5, RGB(0,192,0));
hpenSave = SelectObject(ps.hdc, hpen);
Ellipse(ps.hdc, x - radius, y - radius, x + radius, y + radius);
SelectObject(ps.hdc, hpenSave);
DeleteObject(hpen);

// Same steps as above, but in new location with different color.
x += 25; y -= 15;
hpen = CreatePen(PS_SOLID, 5, RGB(255,0,0));
hpenSave = SelectObject(ps.hdc, hpen);
Ellipse(ps.hdc, x - radius, y - radius, x + radius, y + radius);
SelectObject(ps.hdc, hpenSave);
DeleteObject(hpen);

// Restore the previous brush. It was a 'stock' brush, so no need to destroy it.
SelectObject(ps.hdc, hbrSave);
#else
// Create a pen, one pixel, bright red.
HPEN hpen = CreatePen(PS_SOLID, 1, RGB(255,0,0));
// Select the pen into the 'Device context' and remember the previous pen.
HPEN hpenSave = SelectObject(ps.hdc, hpen);
// Array of points to 'connect'.
POINT apt[] = {
{ 20,20 },
{ 50,20 },
{ 50,50 },
{ 20,50 },
{ 20,20 }
};

// Draw lines between the points in the 'apt' array.
Polyline(ps.hdc, apt, sizeof(apt) / sizeof(apt[0]));

// Restore the previous pen in the 'Device context'.
SelectObject(ps.hdc, hpenSave);
// Destroy the pen we created in CreatePen() above.
DeleteObject(hpen);
#endif

// Match the BeginPaint() above.
EndPaint(hWnd, &ps);
}

return DefWindowProc(hWnd, message, uParam, lParam);
}

//--------------------------------------------------------------------------------------

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc = {0};

wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) MainWndProc;
wc.hInstance = hInstance;
wc.hbrBackground = GetStockObject(WHITE_BRUSH);
wc.lpszClassName = L"myapp";
if(RegisterClass(&wc))
{
HWND hWndMain;

if(hWndMain = CreateWindow(L"myapp",L"Prototype",WS_VISIBLE,0,0,240,320,0,0,hInstance,0))
{
MSG msg;

SHFullScreen(hWndMain, SHFS_HIDETASKBAR | SHFS_HIDESTARTICON | SHFS_HIDESIPBUTTON);
ShowWindow(hWndMain, SW_MAXIMIZE);
UpdateWindow(hWndMain);

ShowWindow(hWndMain, SW_MAXIMIZE);
UpdateWindow(hWndMain);

while(GetMessage(&msg,hWndMain,0,0))
DispatchMessage(&msg);

return msg.wParam;
}
}
return 0;
}

 

 


Look at that!

Now you can draw!

 

 

Radiosity Games
See our new game site

©Copyright TrajectoryLabs.com. All Rights Reserved.