-2

I want to display text only after I clicked with left mouse button into the client area of the window. I have this code, but it doesn't work. When I click left mouse button nothing happens:

void Text(HDC hdc)
{
    SetTextColor(hdc, RGB(255, 0, 0));
    SetBkColor(hdc, RGB(0, 0, 0));
    TCHAR display_msg[] = _T("Message in window");
    TextOut(hdc, RestartButtonWidth, 10, display_msg, _tcslen(display_msg));
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    bool Clicked = false;

    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);

        if (Clicked == true) 
        {
            Text(hdc);
        }          

        EndPaint(hWnd, &ps);
        break;

    case WM_LBUTTONDOWN:
        Clicked = true;
        break;

Why is the change of state of Clicked boolean not registered by WM_PAINT message?

MPython
  • 3
  • 2

1 Answers1

1

Every time your WndProc is called you create new variable Clicked. So, on WM_LBUTTONDOWN you set that local variable to true and that variable will be destroyed at the end of the scope. On the WM_PAINT event you are checking state of newly created Clicked variable and state of that variable is false.

static bool Clicked = false;

is quick solution for your problem.

Elvis Oric
  • 1,289
  • 8
  • 22
  • Do have any idea how to do this in OOP? Because when I set class variable to static then I get error: unresolved external symbol – MPython Dec 01 '17 at 15:26
  • That's another problem. You probably just declared your static member, you also need to define that member. http://en.cppreference.com/w/cpp/language/static – Elvis Oric Dec 01 '17 at 15:31
  • bool ClassName::Clicked = false; // definition (does not use 'static') – Elvis Oric Dec 01 '17 at 15:36