Thread: Rectangle

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    Rectangle

    Im tryin to use this Win32 API thingy and I have no idea what im doin...

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    HDC hdc;
    HPEN hpen;
    HBRUSH hbrush;
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    								PSTR szCmdLine, int iCmdShow)
    	{
       HWND hwnd;
       MSG msg;
       WNDCLASSEX wndclass;
    
       wndclass.cbSize        = sizeof (wndclass);
       wndclass.style         = CS_HREDRAW | CS_VREDRAW;
       wndclass.lpfnWndProc   = WndProc;
       wndclass.cbClsExtra    = 0;
       wndclass.cbWndExtra    = 0;
       wndclass.hInstance     = hInstance;
       wndclass.hIcon         = LoadIcon (NULL, IDI_WINLOGO);
       wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
    
       wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
       wndclass.lpszMenuName  = NULL;
       wndclass.lpszClassName = "Window Class 1";
       wndclass.hIconSm       = LoadIcon (NULL, IDI_WINLOGO);
    
       	RegisterClassEx (&wndclass);
    
    
       hwnd = CreateWindow ("Window Class 1",
       							"My First Window",
                      		WS_OVERLAPPEDWINDOW,
                      		CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            NULL,
                            NULL,
                            hInstance,
                            NULL);
    
       ShowWindow (hwnd, iCmdShow);
       UpdateWindow (hwnd);
    
       	while (GetMessage (&msg, NULL, 0, 0))
       {
          	TranslateMessage (&msg);
             DispatchMessage (&msg);
       }
    
       	return msg.wParam ;
    }
    
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
    {
    
       PAINTSTRUCT ps;
    
    	switch (iMsg)
       {
       	case WM_CREATE:
    
          	break;
    
          case WM_SIZE:
    
          	break;
    
          case WM_PAINT:
    
             BeginPaint ( hwnd, &ps );
    
             hpen = CreatePen(PS_SOLID,2,RGB(200,20,2));
             hbrush = CreateSolidBrush(RGB(15,15,200));
    
             Rectangle ( hdc, 10, 10, 30, 30 );
    
             EndPaint ( hwnd, &ps );
    
          	break;
    
          case WM_DESTROY:
    
          	PostQuitMessage(0);
             break;
       }
    
       return DefWindowProc (hwnd, iMsg, wParam, lParam);
    
    }
    Why doesnt it draw a rectangle?

    All I get is an empty window.
    What is C++?

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    doh... i got it...

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    HDC hdc;
    
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    								PSTR szCmdLine, int iCmdShow)
    	{
       HWND hwnd;
       MSG msg;
       WNDCLASSEX wndclass;
    
       wndclass.cbSize        = sizeof (wndclass);
       wndclass.style         = CS_HREDRAW | CS_VREDRAW;
       wndclass.lpfnWndProc   = WndProc;
       wndclass.cbClsExtra    = 0;
       wndclass.cbWndExtra    = 0;
       wndclass.hInstance     = hInstance;
       wndclass.hIcon         = LoadIcon (NULL, IDI_WINLOGO);
       wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
    
       wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
       wndclass.lpszMenuName  = NULL;
       wndclass.lpszClassName = "Window Class 1";
       wndclass.hIconSm       = LoadIcon (NULL, IDI_WINLOGO);
    
       	RegisterClassEx (&wndclass);
    
    
       hwnd = CreateWindow ("Window Class 1",
       							"My First Window",
                      		WS_OVERLAPPEDWINDOW,
                      		CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            NULL,
                            NULL,
                            hInstance,
                            NULL);
    
       ShowWindow (hwnd, iCmdShow);
       UpdateWindow (hwnd);
    
       	while (GetMessage (&msg, NULL, 0, 0))
       {
          	TranslateMessage (&msg);
             DispatchMessage (&msg);
       }
    
       	return msg.wParam ;
    }
    
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
    {
    
    
    
    	switch (iMsg)
       {
       	case WM_CREATE:
    
          	break;
    
          case WM_SIZE:
    
          	break;
    
          case WM_PAINT:
    
             PAINTSTRUCT ps;
    			HDC hdc;
    			HPEN hpen;
    
    			hdc=BeginPaint(hwnd, &ps);
    
    			hpen=CreatePen(PS_SOLID, 1, RGB(0,0,0));
    
    			SelectObject(hdc, hpen);
    
    			Rectangle(hdc, 10, 10, 50, 50);
    
    			EndPaint(hwnd, &ps);
    			DeleteObject(hpen);
    
    
          	break;
    
          case WM_DESTROY:
    
          	PostQuitMessage(0);
             break;
       }
    
       return DefWindowProc (hwnd, iMsg, wParam, lParam);
    
    }
    now, will I be able to use these shapes like i could with graphics.h?

    Like asign x and y for the positions and detect keys.. if left is mashed x++.

    How can I detect arrow keys in windows?
    What is C++?

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    oh... what header file are these shapes in... I want to know what shapes are available..

    And how do I fill the shapes?
    Last edited by Vicious; 06-03-2002 at 05:57 PM.
    What is C++?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try FillRegion()

    Also, if using a CreateX() or GetX() function you are allocating GDI memory.
    When you then select the GDI object into a DC with
    SelectObject(hdc, hpen);
    you are releasing a GDI object (the return in this case is the original pen).

    Use

    hOldPen=SelectObject(hdc, hpen);
    //draw
    SelectObject(hdc, hOldPen);//put the original back
    DeleteObject(hPen);

    or you will soon run out of drawing mem.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rectangle class
    By blackant in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2009, 08:33 AM
  2. segmetation fault (reading and writing file in c)
    By tasosa in forum C Programming
    Replies: 5
    Last Post: 04-13-2009, 06:04 AM
  3. Struct Program to find Point in Rectangle
    By DMJKobam in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 08:56 PM
  4. Point passed rectangle...
    By Hunter2 in forum Game Programming
    Replies: 15
    Last Post: 10-10-2003, 09:57 AM
  5. Collision detection algorithm
    By Hannwaas in forum Game Programming
    Replies: 5
    Last Post: 11-30-2001, 01:27 PM