View Full Version: problems placing items?

C++ Learning Community > C++ Tips > problems placing items?


Title: problems placing items?
Description: like the coordinates of em


ih8censorship - March 6, 2004 12:53 AM (GMT)
does anyone besides me spend a quite a bit of time messing with the coordinates of images and childwindows and text and things? well i figure i spend quite a bit of time getting things where i want them and i decided to write a function for windows that you can draw a grid of a specifyed size, so you can figure out what the values should be almost right away.here it is:
CODE
void drawgrid( HDC hdc,int gridX,int gridY,int winH,int winW)
{


 for(int i=0;i<width;++i)
 {
 MoveToEx(hdc,gridX*i,0,NULL);
 LineTo(hdc,gridX*i,winH);
 }

 for(int j=0;j<height;++j)
 {
 MoveToEx(hdc,0,gridY*j,NULL);
 LineTo(hdc,winW,gridY*j);
 }


}

allright hdc is just an hdc you get by doing like hdc=GetDC(hwnd) . gridX is the x size of the grid, gridY is the Ysise of the grid. i was originaly going to make it a fixed size, but then i figured it might be handy to have it several sizes. winH is the height of the window you want to draw your grid in and winW is the width of the window you want to draw the grid in. well i hope this will help some people make stuff faster, i know i could have added color just in case someone had a black backround but i figured someone else might need something to do ;)

hmm this might be a nice thing to include in a game making program.... *cough* dr voodoo *cough* anothersomething/consumed

Sam Fisher vs Solid Snake - March 6, 2004 01:35 AM (GMT)
nice.useful.how long did ya spend on it?

ih8censorship - March 6, 2004 01:37 AM (GMT)
heh actually i think i spent about 5 minutes writing that code... but i figured it should save me at least a half hour on every project i have to place items in and more on a real big project.

Sam Fisher vs Solid Snake - March 6, 2004 01:39 AM (GMT)
thats the cool thing abot C++. make a file forget about it, make a project use the file and deliver a kicka$$ app/game

legacyc0der - March 24, 2004 11:19 PM (GMT)
Good sample! I run into this problem a lot. No more pen/paper for me! I tried getting your example to work, but uh.. I didn't. heh. By the time I was trying to change what width and height specified, I decided to just start clean on my own function (and learn for myself). Here it is..

CODE
void drawgrid(HDC hdc, int gridX, int gridY, int winW, int winH)
{
  for(int i = 0; i < winH/gridY+1; i++)   //the +1 is for if it doesn't divide evenly, catch the remainder
  {
     MoveToEx(hdc, 0, gridY*i, NULL);
     LineTo(hdc, winW, gridY*i);
  }
  for(int i = 0; i < winW/gridX+1; i++)
  {
     MoveToEx(hdc, gridX*i, 0, NULL);
     LineTo(hdc, gridX*i, winH);
  }  

  char x[4] = {0};
  sprintf(x, "%d", gridX);

  char y[4] = {0};
  sprintf(y, "%d", gridY);
 
  TextOut(hdc, gridX/2-10, gridY-7, x, strlen(x));   //the numbers here are just for looks
  TextOut(hdc, gridX-7, gridY/2-10, y, strlen(y));   //kind of just throwin it together

}


I was thinking of adding maybe coordinate pairs at the intersections, that'd be cool. Iunno, maybe later. Thanks for the inspiration..

ih8censorship - March 25, 2004 12:01 AM (GMT)
glad to help ^_^ what compiler are you using? i originaly compiled that on dev c++ 4.9.8.0

legacyc0der - March 25, 2004 01:53 AM (GMT)
Dev-C++ 4.9.8.0

I guess I really didn't take a look at how your code works, everything seems so intimidating the first time through! What I was confused about were the two variables here

CODE
void drawgrid( HDC hdc,int gridX,int gridY,int winH,int winW)
{

for(int i=0;i<width;++i)            //width
{
MoveToEx(hdc,gridX*i,0,NULL);
LineTo(hdc,gridX*i,winH);
}

for(int j=0;j<height;++j)         //height
{
MoveToEx(hdc,0,gridY*j,NULL);
LineTo(hdc,winW,gridY*j);
}

}


I think i'm just stupid.. heh

inline_skater20032001 - March 25, 2004 02:33 AM (GMT)
I'm having a hard time getting your function to work for some reason. I got a couple of 'undefined errors' but then I went through and changed some things and now I get a 'parse error' before the first use of the function. There is something I know I'm missing. Here is the I'm using:
CODE

#include <windows.h>

const char g_szClassName[] = "myWindowClass";

void drawgrid( HDC hdc,int gridX,int gridY,int winH,int winW)
{


for(int i=0;i<width;++i)
{
MoveToEx(hdc,gridX*i,0,NULL);
LineTo(hdc,gridX*i,winH);
}

for(int j=0;j<height;++j)
{
MoveToEx(hdc,0,gridY*j,NULL);
LineTo(hdc,winW,gridY*j);
}


}




// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
    case WM_CREATE:
           drawgrid( HDC hdc,int gridX,int gridY,int winH,int winW);
       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, "Window Registration Failed!", "Error!",
  MB_ICONEXCLAMATION | MB_OK);
 return 0;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
 WS_EX_CLIENTEDGE,
 g_szClassName,
 "The title of my window",
 WS_OVERLAPPEDWINDOW,
 CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
 NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
 MessageBox(NULL, "Window Creation Failed!", "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;
}

I'm have only tried this code on Dev C++ so far.

ih8censorship - March 25, 2004 03:18 AM (GMT)
nah your trying to draw the grid in WM_CREATE i tryed that... doesnt work right here just use my original code ;)
CODE
#include <windows.h>
HWND hwnd;
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HDC hdc;
const int height=375;
const int width=544;

void drawgrid( HDC hdc,int gridX,int gridY,int winH,int winW)
{


 for(int i=0;i<width;++i)
 {
 MoveToEx(hdc,gridX*i,0,NULL);
 LineTo(hdc,gridX*i,winH);
 }

 for(int j=0;j<height;++j)
 {
 MoveToEx(hdc,0,gridY*j,NULL);
 LineTo(hdc,winW,gridY*j);
 }


}

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpszArgument,
                   int nFunsterStil)

{
                /* This is the handle for our window */
   MSG messages;            /* Here messages to the application are saved */
   WNDCLASSEX wincl;        /* Data structure for the windowclass */

   /* The Window structure */
   wincl.hInstance = hThisInstance;
   wincl.lpszClassName = szClassName;
   wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
   wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
   wincl.cbSize = sizeof (WNDCLASSEX);

   /* Use default icon and mouse-pointer */
   wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
   wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
   wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
   wincl.lpszMenuName = NULL;                 /* No menu */
   wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
   wincl.cbWndExtra = 0;                      /* structure or the window instance */
   /* Use Windows's default color as the background of the window */
   wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

   /* Register the window class, and if it fails quit the program */
   if (!RegisterClassEx (&wincl))
       return 0;

   /* The class is registered, let's create the program*/
   hwnd = CreateWindowEx (
          0,                   /* Extended possibilites for variation */
          szClassName,         /* Classname */
          "Windows App",       /* Title Text */
          WS_OVERLAPPEDWINDOW, /* default window */
          CW_USEDEFAULT,       /* Windows decides the position */
          CW_USEDEFAULT,       /* where the window ends up on the screen */
          width,                 /* The programs width */
          height,                 /* and height in pixels */
          HWND_DESKTOP,        /* The window is a child-window to desktop */
          NULL,                /* No menu */
          hThisInstance,       /* Program Instance handler */
          NULL                 /* No Window Creation data */
          );

   /* Make the window visible on the screen */
   ShowWindow (hwnd, nFunsterStil);
   hdc=GetDC(hwnd);
       drawgrid(hdc,10,10,height,width);
   /* Run the message loop. It will run until GetMessage() returns 0 */
   while (GetMessage (&messages, NULL, 0, 0))
   {
       /* Translate virtual-key messages into character messages */
       TranslateMessage(&messages);
       /* Send message to WindowProcedure */
       DispatchMessage(&messages);
   }

   /* The program return-value is 0 - The value that PostQuitMessage() gave */
   return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   switch (message)                  /* handle the messages */
   {
       case WM_CREATE:
       
       break;
       
       case WM_PAINT:
       
       break;
       
       case WM_DESTROY:
           PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
           break;
       default:                      /* for messages that we don't deal with */
           return DefWindowProc (hwnd, message, wParam, lParam);
   }

   return 0;
}
looks like i forgot to tell you people what width and height were :lol: sorry

legacyc0der - March 25, 2004 03:23 AM (GMT)
when calling the function from inside your WndProc(), you'll need to pass in actuall initialized variables or constants, without the type of the parameter and also omitting the name.

As an example, one may write:

CODE
case WM_CREATE:
{
  HDC hdc = GetDC(hwnd);  //Get a DC to the this window

  drawgrid(hdc,           //Handle to a device context of the window to use for drawing
           10,            //X cell spacing of grid
           10,            //Y cell spacing of grid
           120,           //Height of window; height of area "to be gridded"
           240);          //Width of window; width of area "to be gridded"

  ReleaseDC(hwnd, hdc);   //Release the device context to the window
  break;
}

inline_skater20032001 - March 25, 2004 03:40 AM (GMT)
Ah, I have not gotten into GDI programming yet so I do not know all of those needed calls to the GDI. Thanks for the help




* Hosted for free by InvisionFree