Sign in to follow this  
phil67rpg

drawing to windows with the mouse

Recommended Posts

phil67rpg    443
I am attempting to draw to a window using DrawText and WM_PAINT. I want to use the mouse and the WM_LBUTTONDOWN message. I understand that to actually draw to the screen one must use the WM_PAINT message. I am using the Win21 API. I am using this API because I want to learn how to program in windows better.

Share this post


Link to post
Share on other sites
You have: Your input, your rendering/drawing. You don't want your input to directly effect your rendering, but to effect the thing your rendering is displaying.

This is called the [url="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller"]Model View Controller[/url] pattern (MVC).

What is your window displaying? A picture? Some text? Your input (the [b]Controller[/b]) effects the data of the image (the [b]Model[/b]), and that data is displayed to the screen (the [b]View[/b]). Your painting shouldn't alter the data directly, just display it. Your input [i]should[/i] effect the data, but the data should not be dependent on either the input or the rendering.

In this case, you have what as data? Several groups of text? So your input effects the text, and then the text is drawn on the screen. Your input should not draw text to the screen itself.

Share this post


Link to post
Share on other sites
phil67rpg    443
here is the code I am working on.
[code]
LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
RECT rect;
HBRUSH newbrush;
HPEN hPen;
HFONT font;
WORD x,y;

switch (msg)
{

case WM_PAINT:
{
// Draw some text centered in the client area of the main window
hDC = BeginPaint(hWindow, &ps);
GetClientRect(hWindow, &rect);

SetBkColor(hDC,RGB(0,0,0));
SetTextColor(hDC,RGB(255,0,0));

DrawText(hDC, TEXT("Tic Tac Toe"), -1, &rect,DT_SINGLELINE | DT_CENTER);

font = CreateFont(30,30,0,0,FW_DONTCARE,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,CLIP_DEFAULT_PRECIS,CLEARTYPE_NATURAL_QUALITY,
VARIABLE_PITCH,TEXT(""));
SelectObject(hDC,font);

RECT rect_one;
rect_one.left=500;
rect_one.right=540;
rect_one.top=220;
rect_one.bottom=260;

DrawText(hDC, TEXT("X"), -1, &rect_one,DT_SINGLELINE | DT_CENTER);

hPen = CreatePen(PS_SOLID,1,RGB(0,255,0));
SelectObject(hDC,hPen);

//draw first vertical line
MoveToEx(hDC,560,200,NULL);
LineTo(hDC,560,400);

//draw second vertical line
MoveToEx(hDC,600,200,NULL);
LineTo(hDC,600,400);

//draw first horizontal line
MoveToEx(hDC,480,280,NULL);
LineTo(hDC,680,280);

//draw second horizontal line
MoveToEx(hDC,480,320,NULL);
LineTo(hDC,680,320);

DeleteObject(hPen);
ReleaseDC(hWindow,hDC);

EndPaint(hWindow, &ps);
break;
}

case WM_LBUTTONDOWN:
{

break;
}
case WM_CHAR:
if(wParam==VK_ESCAPE)
{
PostQuitMessage(0);
break;
}
return 0;
case WM_DESTROY:
{
// Exit the application
PostQuitMessage(0);
break;
}

}
return DefWindowProc(hWindow, msg, wParam, lParam);
}
[/code]

Share this post


Link to post
Share on other sites
phil67rpg    443
Well I looked this one up in a book and it appears to be correct, but it does not shut down the window when I click the left mouse button.
[code]
case WM_MOUSEMOVE:
{

int mouse_x = (int)LOWORD(lParam);
int mouse_y = (int)HIWORD(lParam);

if(wParam == VK_LBUTTON)
{
PostQuitMessage(0);
return 0;
}
[/code]
I will do more research on this problem.

Share this post


Link to post
Share on other sites
Win32 documentation says that WM_MOUSEMOVE's wParam attribute must be [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ms645616(v=vs.85).aspx"]one of these[/url] values.

Perhaps you want [b][u][color=#ff0000]M[/color][/u]K_LBUTTON[/b]? I'm not very familiar with Win32 as a API, so I might be mistaken here.

However, this would be saying, "[i]If the player moves the mouse AND has the left mouse button down, then close the program[/i]".

I think what you actually want is, "[i]If the player clicks the left mouse button, then close the program[/i]". You probably want WM_KEYDOWN, not WM_MOUSEMOVE (Actually, if this is for drawing code, you'll want to handle both). Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
phil67rpg    443
for some reason the program is not reaching the vk code.I tried vk_space as well and it does not work either.
[code]
LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
RECT rect;
HBRUSH newbrush;
HPEN hPen;
HFONT font;
WORD x,y;

switch (msg)
{

case WM_PAINT:
{
// Draw some text centered in the client area of the main window
hDC = BeginPaint(hWindow, &ps);
GetClientRect(hWindow, &rect);

SetBkColor(hDC,RGB(0,0,0));
SetTextColor(hDC,RGB(255,0,0));

DrawText(hDC, TEXT("Tic Tac Toe"), -1, &rect,DT_SINGLELINE | DT_CENTER);

hPen = CreatePen(PS_SOLID,1,RGB(0,255,0));
SelectObject(hDC,hPen);

//draw first vertical line
MoveToEx(hDC,560,200,NULL);
LineTo(hDC,560,400);

//draw second vertical line
MoveToEx(hDC,600,200,NULL);
LineTo(hDC,600,400);

//draw first horizontal line
MoveToEx(hDC,480,280,NULL);
LineTo(hDC,680,280);

//draw second horizontal line
MoveToEx(hDC,480,320,NULL);
LineTo(hDC,680,320);

DeleteObject(hPen);
ReleaseDC(hWindow,hDC);

EndPaint(hWindow, &ps);
break;
}

case WM_MOUSEMOVE:
{

int mouse_x = (int)LOWORD(lParam);
int mouse_y = (int)HIWORD(lParam);

if(wParam == VK_SPACE)
{
PostQuitMessage(0);
return 0;
}

hDC = GetDC(hWindow);

SetBkColor(hDC,RGB(0,0,0));
SetTextColor(hDC,RGB(255,0,0));

font = CreateFont(30,30,0,0,FW_DONTCARE,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,CLIP_DEFAULT_PRECIS,CLEARTYPE_NATURAL_QUALITY,
VARIABLE_PITCH,TEXT(""));
SelectObject(hDC,font);

RECT rect_one;
rect_one.left=500;
rect_one.right=540;
rect_one.top=220;
rect_one.bottom=260;

DrawText(hDC, TEXT("X"), -1, &rect_one,DT_SINGLELINE | DT_CENTER);

break;
}
case WM_CHAR:
if(wParam==VK_ESCAPE)
{
PostQuitMessage(0);
break;
}
return 0;
case WM_DESTROY:
{
// Exit the application
PostQuitMessage(0);
break;
}

}
return DefWindowProc(hWindow, msg, wParam, lParam);
}
[/code]

Share this post


Link to post
Share on other sites
BeerNutts    4401
You could also stop banging your head on the wall and leave the Win32 API behind for input and drawing and move to a modern system like SFML or even SDL for input/windowing.

But, we're been down this road, and you're going to continue to use that crappy API that is Win32.

Share this post


Link to post
Share on other sites
FLeBlanc    3141
If you want to learn DX9, learn DX9. Don't get side-tracked with that seething pit of ugliness that is Win32 API. Most rational people these days use an abstraction layer so they don't have to deal with it that much.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this