drawing to windows with the mouse

Started by
13 comments, last by phil67rpg 11 years, 5 months ago
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.
Advertisement
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 Model View Controller pattern (MVC).

What is your window displaying? A picture? Some text? Your input (the Controller) effects the data of the image (the Model), and that data is displayed to the screen (the View). Your painting shouldn't alter the data directly, just display it. Your input should 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.
here is the code I am working on.

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);
}

hey I figured it out thanks for all the help
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.

case WM_MOUSEMOVE:
{

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

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

I will do more research on this problem.
Win32 documentation says that WM_MOUSEMOVE's wParam attribute must be one of these values.

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

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

I think what you actually want is, "If the player clicks the left mouse button, then close the program". You probably want WM_KEYDOWN, not WM_MOUSEMOVE (Actually, if this is for drawing code, you'll want to handle both).
I have tried MK_LBUTTON also but it does not work either.
for some reason the program is not reaching the vk code.I tried vk_space as well and it does not work either.

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);
}

WM_MOUSEMOVE's wParam doesn't gjve VK_ values. WM_KEYDOWN does, though.
I have tried everything I can think of, but I cant get the left mouse button to work properly.

This topic is closed to new replies.

Advertisement