Board index » cppbuilder » how to create a window when I click the button

how to create a window when I click the button

Dear All:
My code as following:
But it's  nothing to happen when I click the button

void __fastcall TForm1::Button1Click(TObject *Sender)
{

       WNDCLASS wClass;
               wClass.style         =CS_HREDRAW|CS_VREDRAW;
               wClass.lpfnWndProc   =WindowProcedure;  //??w3B2zT?ao?{
|?;
               wClass.cbClsExtra    =0;
               wClass.cbWndExtra    =0;
               wClass.hInstance     =Application->Handle;
               wClass.hIcon
=LoadIcon(Application->Handle,IDI_APPLICATION);
               wClass.hCursor       =LoadCursor(NULL,IDC_ARROW);
               wClass.hbrBackground =(HBRUSH)(COLOR_WINDOW+7);
               wClass.lpszMenuName  =NULL;
               wClass.lpszClassName =WINDOWNAME;

      RegisterClass(&wClass);

    CreateWindowEx(WS_EX_TOOLWINDOW ,  "WINDOWNAME", (LPCTSTR)"Screen",
  WS_POPUP | WS_VISIBLE ,
  300, 300, 100, 100 ,NULL,NULL,Application->Handle,NULL);

Quote
}

Thanks~

David

 

Re:how to create a window when I click the button


   ::RegisterClass(...);
   HWND    hWnd = ::CreateWindow(...);
  ::ShowWindow(hWnd, SW_SHOWNORMAL);
  ::UpdateWindow(hWnd);
  while (WM_QUIT != msg.message)
  {
      if (!::GetMessage(&msg, NULL, 0U, 0U))
        break;
      ::TranslateMessage(&msg);
      ::DispatchMessage(&msg);
  }
  ::UnregisterClass(TEXT("DSPGame"), hInstance);

Ciao

Re:how to create a window when I click the button


Dear Ciao wrote :

 >    ::RegisterClass(...);

Quote
>    HWND    hWnd = ::CreateWindow(...);
>   ::ShowWindow(hWnd, SW_SHOWNORMAL);
>   ::UpdateWindow(hWnd);
>   while (WM_QUIT != msg.message)
>   {
>       if (!::GetMessage(&msg, NULL, 0U, 0U))
>         break;
>       ::TranslateMessage(&msg);
>       ::DispatchMessage(&msg);
>   }
>   ::UnregisterClass(TEXT("DSPGame"), hInstance);

> Ciao

Dear Ciao,
How to get the hInstance?

Thanks~

David

Re:how to create a window when I click the button


::GetCurrentProcess()

My name is Paolo, Ciao is an italian word to say hello.

Ciao

Re:how to create a window when I click the button


Ciao , Dear Paolo:

Oh~ you are italian.
Thanks~
But it seems to add the HInstance.
It works correctly now.
And I have a question to move the window and to change it's size.
It's nothing to respond.
By the way, How to add the photo or button or some object to it?

My code as following:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
          HINSTANCE hInstance=HInstance;
       MSG msg;
       WNDCLASS wClass;
               wClass.style         =CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
               wClass.lpfnWndProc   =WindowProcedure;  //??w3B2zT?ao?{
|?;
               wClass.cbClsExtra    =0;
               wClass.cbWndExtra    =0;
               wClass.hInstance     =hInstance;
               wClass.hIcon         =LoadIcon(hInstance,IDI_APPLICATION);
               wClass.hCursor       =LoadCursor(NULL,IDC_ARROW);
               wClass.hbrBackground =(HBRUSH)(COLOR_BACKGROUND+9 );
               wClass.lpszMenuName  =NULL;
               wClass.lpszClassName =WINDOWNAME;
      RegisterClass(&wClass);
    hWnd =CreateWindowEx(WS_EX_TOOLWINDOW ,   WINDOWNAME ,
(LPCTSTR)"Screen",
  WS_POPUP | WS_VISIBLE   ,
  10, 10, 200, 200 ,NULL,NULL,hInstance,NULL);
    HRGN rgnA;
    rgnA=CreateRoundRectRgn(0,0,200,200,20,20);
    SetWindowRgn(hWnd,rgnA,TRUE);
      ::ShowWindow(hWnd, SW_SHOWNORMAL);
     ::UpdateWindow(hWnd);
::UnregisterClass(TEXT("WINDOWNAME"), hInstance);

Quote
}

void __fastcall TForm1::ButtonMoveClick(TObject *Sender)
{
TRect rect,m_rcStart;
GetWindowRect(hWnd,&rect);
HDC hDC=GetWindowDC(hWnd);
DrawFocusRect(hDC,   &m_rcStart);
m_rcStart.Bottom=m_rcStart.Bottom+100;
rect.Top = rect.Top+100;
  PAINTSTRUCT PS;
  PS.hdc=hDC;
  PS.fErase=0;
  PS.rcPaint=rect;
  BeginPaint( hWnd,&PS);
  ::ShowWindow(hWnd, SW_SHOWNORMAL);
  ::UpdateWindow(hWnd);
ReleaseDC(hWnd,hDC);

Quote
}

Thanks~

David

Re:how to create a window when I click the button


Button is a window CreateWindow() with button style.
Image may be load with LoaImage() and drawed directly in the window DC
GetDC(), BitBlt(), ReleaseDC().
In order to respond at the windows messages i.e. WM_SIZE, WM_MOVE, etc you
have to send windows messages to the created window with SendMessage(), or
call MoveWindow() to move and size.

Ciao.

Re:how to create a window when I click the button


Quote
Dear Paolo wrote:
> Button is a window CreateWindow() with button style.
> Image may be load with LoaImage() and drawed directly in the window DC
> GetDC(), BitBlt(), ReleaseDC().
> In order to respond at the windows messages i.e. WM_SIZE, WM_MOVE, etc you
> have to send windows messages to the created window with SendMessage(), or
> call MoveWindow() to move and size.

> Ciao.

Thanks~ I can paint some photo to window, but I still have some question.
I can't both have the event that move the window and  click the window.
It'll just trigger the click event.
I want to move a child window and to click it sometime.
my code as following:
And how to show solid button on the main  window, not just a child window.
It'll be repainted when the child window be covered, and the photo will be
clear.
I want to repaint it, but I don't know how to do it.

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM
lParam) {

      switch(msg) {
            case WM_NCHITTEST:  //sent to a window when the cursor moves
                  if ( hWnd==hWnd2)     // the hWnd2 is the windows of
button
                 {
                   TRect rect;
                   GetWindowRect(hWnd2, &rect);
                   HDC hDC=GetWindowDC(hWnd2);
                   StretchBlt(hDC,0,0,rect.Width(),rect.Height(),

ImageBuffer->Canvas->Handle,0,0,ImageBuffer->Picture->Width
,ImageBuffer->Picture->Height , SRCCOPY );
                   ::ShowWindow(hWnd2, SW_SHOWNORMAL);
                   ::UpdateWindow(hWnd2);
                   ReleaseDC(hWnd2,hDC);
                 }
                  return HTCAPTION;
            case WM_MOVING:
                   return 0;
            case WM_LBUTTONDBLCLK :
ShowMessage("click");
                 return 0;
             case WM_SIZE:
                //SendMessage(hWnd,WM_SIZE,wParam,lParam);
////////////////////It's  error, I don't know why it is.
//                 }
         }

Thanks~

David

Re:how to create a window when I click the button


First when you create, with CreateWindowEx(), you need to pass the parent
and a CREATESTRUCT *.
Second you don't have to call UnregisterClass() until your window is
destroyed.
Remember that when you close the owner window, your's owned window will be
destroyed sending a
WM_DESTROY message to windowProc.
Here yo can call UnregisterClass().

Ciao

Re:how to create a window when I click the button


Quote
Dear Paolo wrote:

 > First when you create, with CreateWindowEx(), you need to pass the parent

Quote
> and a CREATESTRUCT *.

thanks~
I already pass the handle to the parent window.
But how to pass the CREATESTRUCT *.
I don't know how to do it

Quote
> Second you don't have to call UnregisterClass() until your window is
> destroyed.
> Remember that when you close the owner window, your's owned window will be
> destroyed sending a
> WM_DESTROY message to windowProc.
> Here yo can call UnregisterClass().

Oh~
that's great . I see~

Thanks~

 Can the CREATESTRUCT* assign a object of button to the parent window?
I want a child that has a button and a photo to move when I click the
button.
But ,when I add the WM_NCHITTEST and the WM_LBUTTONDBLCLK it just run the
WM_LBUTTONDBLCLK .
How can do both of them?
And does have any message like as repaint, because the photo would be
cleared when it  be covered by other window.

Thanks~

David

Re:how to create a window when I click the button


CREATESTRUCT cs;
CreateWindowEx(,,,&cs,,);

i don't know why don't catch this message...
     case WM_NCHITTEST:
          return HTCAPTION;

to redraw the window
     case WM_PAINT:
          hdc = ::BeginPaint(hWnd, &ps);
          ::BitBlt(hdc,
                   clientRect.left,
                   clientRect.top,
                   clientRect.right,
                   clientRect.bottom,
                   mainCanvas->DC, 0, 0,
                   SRCCOPY);
          ::EndPaint(hWnd, &ps);
          break;

To make a button you must create a child window with the class name
"BUTTON", have a look at
CreateWindow() help.

Ciao

Re:how to create a window when I click the button


Dear Paolo:

I already can create a button.
But why can't I draw  some photo to button.
And how to receive the click event.

I set the CREATESTRUCT cs3;
But it is not requirement for CreateWindowEx.
Because the CreateWindowEx has done any thing to create a window.

Thanks~

David

Re:how to create a window when I click the button


I think that if you catch WM_ERASEBKGND in the button WinProc, returning
true, you can paint the image in the button.
If this don't work you can paint the entire button using DrawFrameContorl(),
BitBlt(), DrawCaption().
Catch the WM_PAINT of the button

Ciao

Re:how to create a window when I click the button


Quote
Dear Paolo wrote:

  > I think that if you catch WM_ERASEBKGND in the button WinProc, returning

Quote
> true, you can paint the image in the button.
> If this don't work you can paint the entire button using
DrawFrameContorl(),
> BitBlt(), DrawCaption().
> Catch the WM_PAINT of the button

Thanks~
but I don't how to get the HDC of button , I can get the HDC of window and
draw photo on the window.

And the createwindowex don't register the WNDCLASS, just write the class
name with "button"
So It hasn't a WindowProc to receive the message.
I try to creat the other windowproc(WindowProcedureButton) to my button
window.
SetWindowLong(hDC3,GWL_WNDPROC,  WindowProcedureButton);
but it has some error:
[C++ Error] Unit1.cpp(257): E2034 Cannot convert 'long (__stdcall *)(void
*,unsigned int,unsigned int,long)' to 'long'
How to resolve it?

I think to create a subclass window procedure .When it receive some message
then call his parent window procdure.
And do some thing by parent window procedure.
And , does it the only mothod  that create a chain of window procedure to
control my button window?

thanks~

David

Re:how to create a window when I click the button


Dear Sir:

Thanks~

I do it~

Thanks~

David

Other Threads