Jump to content

  • Log In with Google      Sign In   
  • Create Account

drawing to windows with the mouse


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 phil67rpg   Members   -  Reputation: 767

Like
-1Likes
Like

Posted 18 November 2012 - 07:03 PM

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.

Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 19568

Like
0Likes
Like

Posted 18 November 2012 - 07:54 PM

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.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#3 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 18 November 2012 - 07:58 PM

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


#4 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 18 November 2012 - 08:24 PM

hey I figured it out thanks for all the help


#5 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 18 November 2012 - 09:05 PM

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.

#6 Servant of the Lord   Crossbones+   -  Reputation: 19568

Like
0Likes
Like

Posted 18 November 2012 - 09:08 PM

Win32 documentation says that WM_MOUSEMOVE's wParam attribute must be one of these values.

Perhaps you want 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).

Edited by Servant of the Lord, 18 November 2012 - 09:11 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#7 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 18 November 2012 - 09:11 PM

I have tried MK_LBUTTON also but it does not work either.

#8 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 18 November 2012 - 09:20 PM

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


#9 Servant of the Lord   Crossbones+   -  Reputation: 19568

Like
1Likes
Like

Posted 18 November 2012 - 09:53 PM

WM_MOUSEMOVE's wParam doesn't gjve VK_ values. WM_KEYDOWN does, though.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#10 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 18 November 2012 - 10:15 PM

I have tried everything I can think of, but I cant get the left mouse button to work properly.

#11 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 18 November 2012 - 10:41 PM

I finally tried WM_LBUTTONDOWN and it worked.

#12 BeerNutts   Crossbones+   -  Reputation: 2945

Like
0Likes
Like

Posted 18 November 2012 - 11:48 PM

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.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#13 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 19 November 2012 - 03:47 PM

I wanted to learn windows because it leads to dx9

#14 FLeBlanc   Crossbones+   -  Reputation: 3101

Like
0Likes
Like

Posted 19 November 2012 - 03:53 PM

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.

#15 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 19 November 2012 - 04:00 PM

ok I have learned a lot I will go back to dx9




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS