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