//http://www.winprog.org/tutorial/simple_window.html #include <windows.h> const wchar_t g_szClassName[] = L"myWindowClass"; // Step 4: the Window Procedure LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static int red = 128; static int green = 128; static int blue = 128; static int x = 100; static int y = 100; HDC hDC; PAINTSTRUCT Ps; HPEN hPen1; HBRUSH hBrush; RECT rect; HFONT hf; long lfHeight=100; switch(msg) { case WM_PAINT: hDC=BeginPaint(hwnd,&Ps); hPen1=CreatePen(PS_SOLID, 15 , RGB(red,green,blue)); SelectObject(hDC, hPen1); hBrush = CreateSolidBrush(RGB(0,0,255)); SelectObject(hDC,hBrush); // text is "drawn" with fonts lfHeight=50; hf = CreateFont(lfHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, L"Imprint MT Shadow"); SelectObject(hDC, hf); rect.top=320; rect.bottom=500; rect.left=125; rect.right=425; DrawText(hDC,L"The sum of all the dice is ", -1,&rect, DT_CENTER | DT_WORDBREAK); Rectangle(hDC,x,y,x+300,y+200); Rectangle(hDC,x,y,x+100,y+200); DeleteObject(hBrush); DeleteObject(hPen1); EndPaint(hwnd,&Ps); break; case WM_KEYDOWN: if(wParam==VK_UP) { y=y-1; InvalidateRect(hwnd,NULL,true); } if(wParam==VK_DOWN) { y=y+1; InvalidateRect(hwnd,NULL,true); } if(wParam==VK_LEFT) { x=x-1; InvalidateRect(hwnd,NULL,true); } if(wParam==VK_RIGHT) { x=x+1; InvalidateRect(hwnd,NULL,true); } break; case WM_CHAR: if(wParam=='r') { InvalidateRect(hwnd,NULL,true); } else if(wParam=='e') { InvalidateRect(hwnd,NULL,true); } else if(wParam=='c') { red = rand()%256; green = rand()%256; blue = rand()%256; InvalidateRect(hwnd,NULL,true); } else if(wParam=='m') { //IDCANCEL int result = MessageBox(NULL, L"An m key has been pressed.", L"MMMMMM!", MB_ICONHAND | MB_ABORTRETRYIGNORE); if(result=IDIGNORE) { red = rand()%256; green = rand()%256; blue = rand()%256; InvalidateRect(hwnd,NULL,true); } } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; HWND hwnd; MSG Msg; //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = g_szClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) { MessageBox(NULL, L"Window Registration Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } // Step 2: Creating the Window hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, L"John Ryan Tinio [email protected]", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, L"Window Creation Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // Step 3: The Message Loop while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }
78 Replies - 4773 Views - Last Post: 28 November 2011 - 10:53 PM
#1
Windows Application coding
Posted 22 November 2011 - 02:22 PM
I am writing a windows application code that is supposed to simulate dice being rolled. I am supposed to have displayed five rectangles, formed into squares, showing that there are 5 dices that are going to be rolled. My problem right now is getting the five dices(rectangles to actually be displayed on the screen. What I am getting displayed right now is one rectangle being divided into two. But one of the rectangles is smaller because of the x coordinate. How do I get the program to print out five separate rectangles, but all along the same line? Here is my code so far.
Replies To: Windows Application coding
#2
Re: Windows Application coding
Posted 22 November 2011 - 02:55 PM
Since this is a very visual question, could you take a screen shot of your current results? It would help if we saw what you saw.
Also, I would suggest making each die a different color. That way you can better see which pieces are from which die, because it sounds like they are overlapping.
Also, I would suggest making each die a different color. That way you can better see which pieces are from which die, because it sounds like they are overlapping.
#4
Re: Windows Application coding
Posted 22 November 2011 - 03:08 PM
Am I correct in this interpretation:
pressing C changes color
pressing R rolls the dice, which invalidates, causing the WM_PAINT to be called. So R only rolls 1 die at a time. Is that right?
The left, right, up, down keys are only incrementing the starting position by 1 pixel. So each die is pretty much on top of each other. Try increasing that increment to a few points more than the size of your dice.
Also, dice are square. So I don't get why you are sizing the rectangle by x+300, y+200. That would make a rectangle not a square.
The one die, with a second a bit smaller would be because that's what you're doing in lines 55 and 56. One die at x+300, y+200 and one at x+100, y+200
But you say you want 5 die side by side.
Maybe I'm just not seeing it but I don't see where you are trying to make 5 of anything. I would have expected some type of loop from 0 to 4 to make the 5 dice. Then in the loop increment the starting location.x value so each die starts to the right of the last one.
I recommend you stop. Take a break. Get a burger. Then go to a white board and draw out the logic of what you are trying to code. I think you are merely confused because you just sat down and started typing before forming a solid plan.
pressing C changes color
pressing R rolls the dice, which invalidates, causing the WM_PAINT to be called. So R only rolls 1 die at a time. Is that right?
The left, right, up, down keys are only incrementing the starting position by 1 pixel. So each die is pretty much on top of each other. Try increasing that increment to a few points more than the size of your dice.
Also, dice are square. So I don't get why you are sizing the rectangle by x+300, y+200. That would make a rectangle not a square.
The one die, with a second a bit smaller would be because that's what you're doing in lines 55 and 56. One die at x+300, y+200 and one at x+100, y+200
But you say you want 5 die side by side.
Maybe I'm just not seeing it but I don't see where you are trying to make 5 of anything. I would have expected some type of loop from 0 to 4 to make the 5 dice. Then in the loop increment the starting location.x value so each die starts to the right of the last one.
I recommend you stop. Take a break. Get a burger. Then go to a white board and draw out the logic of what you are trying to code. I think you are merely confused because you just sat down and started typing before forming a solid plan.
#5
Re: Windows Application coding
Posted 22 November 2011 - 05:34 PM
the first part of what you have written is for a later part in the assignment. I wanted to get the color change out of the way, and using the arrows to move the dice out of the way as well.
and yes, they are suppose to be squares. i was going to change them later, but i was more concerned about the layout of it. i was able to get the layout of it correct and how I wanted it to be. it was something simple that i didn't think of. i just use this code
but I've been having a problem drawing the dots on the squares. I have really no clue on where to start. I don't know if I should start a new topic for this one, or just use this one.
and yes, they are suppose to be squares. i was going to change them later, but i was more concerned about the layout of it. i was able to get the layout of it correct and how I wanted it to be. it was something simple that i didn't think of. i just use this code
Rectangle(hDC,x,y,x+100,y+100); x=x+125; Rectangle(hDC,x,y,x+100,y+100); x=x+125; Rectangle(hDC,x,y,x+100,y+100); x=x+125; Rectangle(hDC,x,y,x+100,y+100); x=x+125; Rectangle(hDC,x,y,x+100,y+100);
but I've been having a problem drawing the dots on the squares. I have really no clue on where to start. I don't know if I should start a new topic for this one, or just use this one.
#6
Re: Windows Application coding
Posted 22 November 2011 - 05:45 PM
Use this one.
Stop and think it through.
YOu have a location coordinate for the top left corner of the die: X, Y
Use that as the measuring point for each dot. The upper left dot might be x+5, y+5 and so on.
Personally I would suggest making all the drawing very dynamic and calculating on the fly instead of hard coded the way you have.
By that I mean set a variable to the size of the die then do all your calculations based on the x,y location and the size. That way if you decide to increase the size of the die from 100 to 500 you don't have to scrub through the code and make dozens of changes. This is one example of what experienced coders refer to as 'maintainability'
Stop and think it through.
YOu have a location coordinate for the top left corner of the die: X, Y
Use that as the measuring point for each dot. The upper left dot might be x+5, y+5 and so on.
Personally I would suggest making all the drawing very dynamic and calculating on the fly instead of hard coded the way you have.
By that I mean set a variable to the size of the die then do all your calculations based on the x,y location and the size. That way if you decide to increase the size of the die from 100 to 500 you don't have to scrub through the code and make dozens of changes. This is one example of what experienced coders refer to as 'maintainability'
int dieSize = 500; int margin = 10; // Space between dice ... Part of drawing method Rectangle(hDC, x, y, x + dieSize, y + dieSize); DrawDots(hDC, x, y, DotValue); x = x + dieSize + margin; ... Part of drawing method DrawDots(handle hDC, int x, int y, int DotValue) { // Draw circles based on the location and size of die and value }
#7
Re: Windows Application coding
Posted 23 November 2011 - 10:29 AM
When I tried writing the code this way, there was a break in between the last rectangle. Here is what I put in my case WM_PAINT
and also, here is the result that I was receiving.
case WM_PAINT: hDC=BeginPaint(hwnd,&Ps); hPen1=CreatePen(PS_SOLID, 15 , RGB(red,green,blue)); SelectObject(hDC, hPen1); hBrush = CreateSolidBrush(RGB(0,100,255)); SelectObject(hDC,hBrush); // text is "drawn" with fonts lfHeight=50; hf = CreateFont(lfHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, L"Imprint MT Shadow"); SelectObject(hDC, hf); rect.top=320; rect.bottom=500; rect.left=125; rect.right=425; DrawText(hDC,L"The sum of all the dice is ", -1,&rect, DT_CENTER | DT_WORDBREAK); Rectangle(hDC,x,y,x+dieSize,y+dieSize); x=x+dieSize+margin; Rectangle(hDC,x,y,x+dieSize,y+dieSize); x=x+dieSize+margin; Rectangle(hDC,x,y,x+dieSize,y+dieSize); x=x+dieSize+margin; /*Rectangle(hDC,x,y,x+dieSize,y+dieSize);*/ /*x=x+dieSize+margin; Rectangle(hDC,x,y,x+dieSize,y+dieSize);*/ DeleteObject(hBrush); DeleteObject(hPen1); EndPaint(hwnd,&Ps); break;
and also, here is the result that I was receiving.

#8
Re: Windows Application coding
Posted 23 November 2011 - 05:16 PM
The x variable is static so the following will be incorrect after each call to WM_PAINT,
if you draw just one die you will probably see the result.
x=x+dieSize+margin;
if you draw just one die you will probably see the result.
#9
Re: Windows Application coding
Posted 23 November 2011 - 09:26 PM
yes, i see the result with just one die. how do i fix the problem so that i can have 5 dice displayed?
#10
Re: Windows Application coding
Posted 23 November 2011 - 09:45 PM
You can use another variable
eg
eg
int dx = x; ... dx = dx + dieSize + margin;
#11
Re: Windows Application coding
Posted 23 November 2011 - 09:51 PM
when i try to use another variable like that, only one rectangle is being outputted and drawn.
#12
Re: Windows Application coding
Posted 23 November 2011 - 10:20 PM
Hi, post some code please.
Have you updated the Rectangle calls? :
Have you updated the Rectangle calls? :
Rectangle(hDC, dx, dy, dx+dieSize, dy+dieSize); dx = dx + dieSize + margin;
#13
Re: Windows Application coding
Posted 23 November 2011 - 10:27 PM
i just fixed it. i found what i was doing wrong. i forgot to change the x's and y' inside of rectangle. another thing, i've been trying to get the dots to be drawn on each die at random, to indicate what the number is on the die, but i can't seem to come up with anything at all.
#14
Re: Windows Application coding
Posted 23 November 2011 - 10:51 PM
#15
Re: Windows Application coding
Posted 23 November 2011 - 11:17 PM
those shows only how to draw circles, but i'm not sure how i would code it so that it would draw circles on the rectangles, and also at random