Solved

win32 question

Posted on 2001-08-09
5
601 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Expert Comment

by:MichaelS
ID: 6367091
>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
ID: 6367107
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
ID: 6367126
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
ID: 6369061
Aqqarwal..... how to refresh the screen??
0
 

Expert Comment

by:trongnghia
ID: 6371203
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.

631 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