Results 1 to 6 of 6

Thread: Please look at this simple code!

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Please look at this simple code!

    i want to fill in the ellipse with a gray background but when i run this code, it return the following error:

    cannot convert from 'void *' to 'struct HBRUSH__ *'


    Here is the code:

    Code:
    #include <windows.h>
    LRESULT CALLBACK WndProc(HWND hwnd, UINT imsg, WPARAM wparam, LPARAM lparam)
    {
    HDC hdc;
    PAINTSTRUCT ps;
    HPEN hpen;
    HBRUSH hbrush;
    hbrush = GetStockObject(GRAY_BRUSH);
    switch(imsg)
    {
    	case WM_PAINT:
    		hdc=  BeginPaint(hwnd, &ps);
    		SelectObject(hdc, hbrush);
    		Ellipse(hdc, 0, 0, 23, 23);
    		EndPaint(hwnd, &ps);
    		DeleteObject(hbrush);
    	return 0;
    	case WM_CLOSE:
    		DestroyWindow(hwnd);
    		return 0;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    }
    		return DefWindowProc(hwnd, imsg, wparam, lparam);
    }
    	int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    	WNDCLASSEX wc;
        MSG msg;
    	HWND winhwnd;
    
    	wc.cbSize = sizeof(WNDCLASSEX);
    	wc.style = CS_HREDRAW | CS_VREDRAW;
    	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)GetStockObject (WHITE_BRUSH);
    	wc.lpszMenuName = NULL;
    	wc.lpszClassName = "myclass";
    	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Error in Class Registeration", "Error", MB_OK);
    		return 0;
    	}
    	winhwnd = CreateWindowEx(
    		WS_EX_CLIENTEDGE, 
    		"myclass", 
    		"the title", 
    		WS_OVERLAPPEDWINDOW,
    		0,
    		0,
    		340,
    		304,
    		NULL,
    		NULL,
    		hInstance,
    		NULL);
    	if(winhwnd == NULL)
    	{
    		MessageBox(NULL, "Error in hwnd Registeration", "Error", MB_OK);
    		return 0;
    	}
    	ShowWindow(winhwnd, nShowCmd);
    	UpdateWindow(winhwnd);
    	while(GetMessage(&msg, NULL, 0,0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return msg.wParam;
    }

    Please help me and find the solution!
    Baaaaaaaaah

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Which line is it on?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Here

    hbrush = GetStockObject(GRAY_BRUSH);

    ----------------------------------------------------------------\

    Anyway, I got it. I used this:
    hBrush = CreateSolidBrush(RGB(34,34,34));

    -----------------

    But now I have another question. I want set the border color of the ellipse to red and the background to blue.
    If I create my own "pen" and a "Brush" then I cannot select two objects at one time and then use them like this

    Code:
    HPEN hpen;
    HBRUSH hbrush;
    hpen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
    hbrush = CreateSolidBrush(RGB(0,0,255));
    SlectObject(hdc, hbrush | hpen);
    that return an error when i select two objects at once so how can i change the border and back color of the ellipse at once?
    Baaaaaaaaah

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    hbrush = (HBRUSH)GetStockObject(GRAY_BRUSH);
    A cast is all that's required You shouldn't delete graphics objects retrieved as a stock object. Plus, you need to store the original before deleting:
    Code:
    HGDIOBJ hObj = CreateGDIObject(...);
    HGDIOBJ hOld = SelectObject(hDC, hObj);
    
    // use hDC
    
    SelectObject(hDC, hOld);
    DeleteObject(hObj); // Unless it was from GetStockObject
    Pens and brushes are exclusive, so use two calls to SelectObject.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Thanks a lot! Another question:

    I am doing windows programming in both vb and C++. Should I learn windows API programming in C++ or vb first?
    Baaaaaaaaah

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    In VB, using the API is mostly redundant, since CreateWindow is hardly ever required. So...that leaves C++
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width