Changing color of pen

This is a discussion on Changing color of pen within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I have got a problem with changing color of the pen... Can someone analyse what is wrong in the ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    Changing color of pen

    Hi, I have got a problem with changing color of the pen...
    Can someone analyse what is wrong in the source

    Compiler sais that problem is on line which is highlighted...
    Crosses initialization of 'struct HPEN__ * hBluePen'

    Dev-c++

    Code:
    #include <windows.h>
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       PSTR szCmdLine, int iCmdShow)
    {
        TCHAR szAppName[] = TEXT("Default window");           // Header
        HWND hWnd;
        MSG msg;
        WNDCLASSEX wc;
        
        wc.cbSize = sizeof(wc);
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);            // Ico
        wc.hIconSm = NULL;                                     // Ico_sm
        wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+0);           // Background
        wc.lpszMenuName = "WMENU";
        wc.lpszClassName = szAppName;
        
        RegisterClassEx(&wc);
        
        hWnd = CreateWindowEx(0,szAppName,
                            szAppName,
                            WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT,                    // Margin from left
                            CW_USEDEFAULT,                    // Margin from top
                            CW_USEDEFAULT,                    // Widht
                            CW_USEDEFAULT,                    // Height
                            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 message, WPARAM wParam, LPARAM lParam)
    {
    HDC              hDC;
    PAINTSTRUCT      ps;
     HPEN hPen = CreatePen(PS_SOLID, 1, RGB(0,0,255));
             switch(message)
             {
              case WM_PAINT:
                HPEN hBluePen = SelectObject(hDC, hBluePen);
                hDC = BeginPaint(hWnd, &ps);
                TextOut(hDC, 50, 50, TEXT("ahoj"), 5);
                EndPaint(hWnd, &ps);
                return 0;
              case WM_DESTROY:
              PostQuitMessage(0);
              return 0;
            }
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    Thx for help

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Declaring variables inside switch statements is the cause.

    Try this code which uses braces to make the variable be declared in a smaller scope.
    Code:
              case WM_PAINT: {
                HPEN hBluePen = SelectObject(hDC, hBluePen);
                hDC = BeginPaint(hWnd, &ps);
                TextOut(hDC, 50, 50, TEXT("ahoj"), 5);
                EndPaint(hWnd, &ps);
                return 0;
              }
    The normal way of a switch is break, not return
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by Salem View Post
    Declaring variables inside switch statements is the cause.

    Try this code which uses braces to make the variable be declared in a smaller scope.
    Code:
              case WM_PAINT: {
                HPEN hBluePen = SelectObject(hDC, hBluePen);
                hDC = BeginPaint(hWnd, &ps);
                TextOut(hDC, 50, 50, TEXT("ahoj"), 5);
                EndPaint(hWnd, &ps);
                return 0;
              }
    The normal way of a switch is break, not return
    I tried your code but it reports new error: "ANSI C++ forbids implicit conversion from 'void*' in initialization"

  4. #4
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308
    cast it
    PHP Code:
    hPenOld = (HPEN)SelectObject(hdchPen); 
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  5. #5
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    The problem was that you used hBluePen instead of hPen. That's all.

    To Salem:
    Declaring variables in switch statements is possible if you put braces around it, which is just what he already did.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tutle graphic pen problem
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2007, 06:04 AM
  2. Changing Text Colors Or Look In C++
    By mfm1983 in forum C++ Programming
    Replies: 3
    Last Post: 12-16-2006, 05:17 PM
  3. Changing windows without changing?
    By Lionmane in forum Windows Programming
    Replies: 7
    Last Post: 10-19-2005, 11:41 AM
  4. [C++/WinAPI] Changing bitmap contrast
    By jagi in forum Windows Programming
    Replies: 0
    Last Post: 03-27-2005, 02:51 PM
  5. GDI: Creating a pen VS taking it as a stock object.
    By limacat in forum Windows Programming
    Replies: 5
    Last Post: 03-31-2003, 09:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21