Solved

win32 question

Posted on 2001-08-09
5
568 Views
Last Modified: 2008-02-01
I wanna do a win32 programming that when i right click my mouse, it will display "hello" and when i drag it from a position to another position.... it come out with many many shadow with the word "hello". What is the problem actually?

below is my program:

#include<windows.h>

long FAR PASCAL WndProc(HWND,UINT,WPARAM,LPARAM);

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
     HWND hWnd;
     MSG msg;
     WNDCLASS wc;

if(!hPrevInstance)
{
     wc.style = NULL;
     wc.lpfnWndProc = (WNDPROC) 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 = "BATCH 23";
     if(!RegisterClass(&wc))
     {
          MessageBox(0,"Could not register WNDCLASS wc","Application",MB_OK);
          return FALSE;
     }
}

hWnd = CreateWindow("BATCH 23","Sample window", WS_OVERLAPPEDWINDOW,10,10,200,300,NULL,NULL,hInstance,NULL);
if(!hWnd)
{
     MessageBox(0,"Could not create window","Application",MB_OK);
     return FALSE;
}
ShowWindow(hWnd,nCmdShow);
while(GetMessage(&msg,0,0,0))
{
     TranslateMessage(&msg);
     DispatchMessage(&msg);
}

return(0);
} //end of WinMain()

long FAR PASCAL WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     int Xpos,Ypos;
     HDC hdc;
     static POINT current,last;
     static int flag=0;

     switch(message)
     {
          case WM_RBUTTONDOWN:
               current.x=last.x=LOWORD(lParam);
               current.y=last.y=HIWORD(lParam);
               SetCapture(hWnd);
               flag=1;
               break;
          case WM_MOUSEMOVE:
               current.x=LOWORD(lParam);
               current.y=HIWORD(lParam);
               if(flag==1)
               {
                    hdc=GetDC(hWnd);
                    SetROP2(hdc,R2_NOT);
                    //ERASE THE PREVIOUS LINE
                    MoveToEx(hdc,current.x,current.y,NULL);
                    LineTo(hdc,last.x,last.y);
                    MoveToEx(hdc,current.x,current.y,NULL);
                    Xpos=LOWORD(lParam);
                    Ypos=HIWORD(lParam);
                    TextOut(hdc,Xpos,Ypos,"Hello",5);
                    ReleaseDC(hWnd,hdc);
               }
               last.x=current.x;
               last.y=current.y;
               break;
          case WM_RBUTTONUP:
               last.x=LOWORD(lParam);
               last.y=HIWORD(lParam);
               hdc=GetDC(hWnd);
               MoveToEx(hdc,current.x,current.y,NULL);
               LineTo(hdc,last.x,last.y);
               ReleaseDC(hWnd,hdc);
               ReleaseCapture();
               flag=0;
               break;
     case WM_DESTROY:
               PostQuitMessage(0);
               break;
     default:
          return DefWindowProc(hWnd,message,wParam,lParam);
     }
     return 0;
}



0
Comment
Question by:fl78
5 Comments
 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
>What is the problem actually?

Hmmm, I just can repeat your question, what is the problem?
Does you code works or if it doesn't than what is doesn't works? Can you make yourself more clear?
0
 
LVL 1

Accepted Solution

by:
Aggarwal earned 100 total points
Comment Utility
It becasue on RBUTTONDOWN u are setting the flag and not doing anything else..

u are displaying "Hello" on MouseMove ..that's the reason for getting number of shadows !!

what u can do is ..
display Hello on RBUTTONDOWN ..

and on ONMOUSEMOVE set some flag ..

now in RBUTTONUP ..check for this flag ..if it is set ..refreash the screen and display hello at new location !! Otherwise ..do nothing ??????
0
 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
Ahh, I think I can catch what are you trying to do. You have to use WM_PAINT, only in that handler you have to write. On MouseMove you have to invalidate your window, use something like InvalidateRect(hWnd, NULL, TRUE) for that
0
 

Author Comment

by:fl78
Comment Utility
Aqqarwal..... how to refresh the screen??
0
 

Expert Comment

by:trongnghia
Comment Utility
I think the answer of MichaelS is correct.

And to refresh the screen you can use InvalidateRect(...) function to add the rectangle of drawing text "Hello"
to the specified window's update region.
The invalidated areas is processed when the next WM_PAINT message occurs. The system sends this message when there are no other messages in the application's message queue
and the invalidated areas is not null.

Cheers,
TrongNghia

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now