Wm_drawitem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Wm_drawitem

  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    190

    Wm_drawitem

    I'm writing a chessboard with GDI and when I click on a square the color changes to the opposite color but then it wont get redrawn again to normal.
    I verified and I get only 5 WM_DRAWITEM message after I click a case instead of 64.

    I wonder why?

    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    #pragma comment(linker, "/subsystem:\"console\" /entry:\"WinMainCRTStartup\"")
    
    #define ID_SQUARE_1      101
    #define ID_SQUARE_2      102
    #define SIZE     80
    bool BlackOrWhite = 0;
    int j=1, k=1, Ycoord=SIZE;
    HINSTANCE hInst ;
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    void CheckClickedSquare(int wParam)
    {
        switch (wParam)
        {
        case ID_SQUARE_1:
            //cout <<"1 work"<< endl;
            break ;
        case ID_SQUARE_2:
            //cout << "2 work" << endl;
            break ;
        }
    }
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
        static TCHAR szAppName[] = TEXT ("GDI Chessboard") ;
        MSG          msg ;
        HWND         hwnd ;
        WNDCLASS     wndclass ;
    
        hInst = hInstance ;
    
        wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
        wndclass.lpfnWndProc   = WndProc ;
        wndclass.cbClsExtra    = 0 ;
        wndclass.cbWndExtra    = 0 ;
        wndclass.hInstance     = hInstance ;
        wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
        wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
        wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName  = szAppName ;
        wndclass.lpszClassName = szAppName ;
    
        if (!RegisterClass (&wndclass))
        {
            MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                        szAppName, MB_ICONERROR) ;
            return 0 ;
        }
    
        hwnd = CreateWindow (szAppName, TEXT ("GDI Chessboard Demo"),
                             WS_OVERLAPPEDWINDOW,
                             CW_USEDEFAULT, CW_USEDEFAULT,
                             820, 800,
                             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)
    {
        static HWND h[64];
        static int       cxClient, cyClient, cxChar, cyChar ;
        int              cx, cy ;
        LPDRAWITEMSTRUCT pdis ;
        POINT            pt[3] ;
        RECT             rc ;
        PAINTSTRUCT ps;
        HDC hdc;
        int x = 116,y=100, number=0;
        char ch = 'A';
        char str[2], num[2];
    
        switch (message)
        {
        case WM_CREATE :
        cout << "WM_CREATE" << endl;
            cxChar = LOWORD (GetDialogBaseUnits ());
            cyChar = HIWORD (GetDialogBaseUnits ());
    
            for (int i=1; i<65; i++)
            {
                h[i] = CreateWindow (TEXT ("button"), TEXT (""),
                                     WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
                                     0, 0, SIZE, SIZE,
                                     hwnd, (HMENU)(i+100), 0, NULL);
            }
            return 0 ;
    
        case WM_SIZE :
        cout << "WM_SIZE" << endl;
     
            for (int i=1; i<65; i++)
            {
                if(i%8==1)
                {
                    j=1;
                    Ycoord += SIZE;
                }
    
                MoveWindow (h[i], j*SIZE, Ycoord-90, SIZE, SIZE, TRUE);
                j++;
             
            }
            return 0 ;
    
        case WM_DRAWITEM :
        cout << "WM_DRAWITEM" << endl;
            pdis = (LPDRAWITEMSTRUCT) lParam ;
            // Fill area with white and frame it black
    
            if(k++%8==1)
            {
                BlackOrWhite = !BlackOrWhite;
            }
            if(BlackOrWhite)
            {
                FillRect (pdis->hDC, &pdis->rcItem,
                          (HBRUSH) GetStockObject (WHITE_BRUSH)) ;
            }
            else
            {
                FillRect (pdis->hDC, &pdis->rcItem,
                          (HBRUSH) GetStockObject (BLACK_BRUSH)) ;
            }
            BlackOrWhite = !BlackOrWhite;
    
            FrameRect (pdis->hDC, &pdis->rcItem,
                       (HBRUSH) GetStockObject (BLACK_BRUSH)) ;
    
            return 0 ;
    
        case WM_PAINT:
        cout << "WM_PAINT" << endl;
            hdc = BeginPaint(hwnd, &ps);
    
            for (int i=1; i<9; i++)
            {
                sprintf(str,"%c", ch++);
                TextOut(hdc, x+((i-1)*(SIZE)), 40, str, 1);
                sprintf(num,"%i", i);
                TextOut(hdc, 60, y+((i-1)*(SIZE)), num, 1);
            }
            EndPaint(hwnd, &ps) ;
            return 0;
    
        case WM_COMMAND :
            cout << wParam << endl;
            CheckClickedSquare(wParam);
            return 0 ;
    
        case WM_DESTROY :
            PostQuitMessage (0) ;
            return 0 ;
        }
        return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    Last edited by MasterDucky; May 17th, 2013 at 12:25 PM.

  2. #2
    Join Date
    Dec 2007
    Location
    France
    Posts
    190

    Re: Wm_drawitem

    I realize that maybe creating a chessboard with 64 "buttons" are not the best way.
    So my question is, is it better to draw the 64 cases and detect mouse clicks or is it better to create 64 "buttons"?

    Does it matter, or one method is better than the other?

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center