Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

Make Window Modal....

Hi

I have created a program using code from http://www.angelfire.com/hi5/delphizeus/index.html using CreateWindow, etc. (simple form with one button on it)

I also have a second window (with different class, message handler, etc) which is created when i click on a button on the main form.

It shows as expected except it is not modal to the main form.

Anyone know how to make it modal? (i know i could use DIALOG as the method - just want to know if it can be done the way i have gone)
0
GroundFloor
Asked:
GroundFloor
  • 2
1 Solution
 
Slick812Commented:
you can effect a Modal form window by using -
EnableWindow(hForm1, False);

and you MUST use

EnableWindow(hForm1, True);

when the modal form window closes, or else it stays dis-abled


program DoModal;

uses
  Windows, Messages;

{$R *.RES}

const
ID_ModalBut = 101;
ID_OkBut = 102;

var
winClass: TWndClass;
hForm1, hButton1, hModal: Integer;
MainMsg: TMSG;


procedure ShowModal;
begin
hModal := CreateWindow(winClass.lpszClassName, 'Modal Form',WS_CAPTION or
             WS_SYSMENU or WS_POPUP or WS_VISIBLE,
             (GetSystemMetrics(SM_CXSCREEN) div 2)-110,
             (GetSystemMetrics(SM_CYSCREEN) div 2)-75, 220, 150,hForm1, 0, hInstance, nil);

SendMessage(CreateWindow('STATIC','Modal message is here'#10+
            'Second Line shows here'#10'Last line in this message',
 WS_VISIBLE or WS_CHILD,3,6,214,80,hModal,0,hInstance,nil),
    WM_SETFONT,GetStockObject(ANSI_VAR_FONT),0);

CreateWindow('BUTTON', 'O K',
    WS_VISIBLE or WS_CHILD or BS_PUSHBUTTON,
    82,90,58,28,hModal,ID_OkBut,hInstance,nil);

EnableWindow(hForm1, False);
end;

function WindowFunc(hWnd, Msg, wParam, lParam: Integer): Integer; stdcall;
begin
case Msg of
  WM_DESTROY: if hWnd = hForm1 then PostQuitMessage(0);
   
  WM_COMMAND: if lParam = hButton1 then PostMessage(hForm1,WM_CLOSE,0,0)
    else if LOWORD(wParam) = ID_ModalBut then
     ShowModal
     else if LOWORD(wParam) = ID_OkBut then
     begin
     PostMessage(hModal,WM_CLOSE,0,0);
     EnableWindow(hForm1, True);
     end;
  end;
Result := DefWindowProc(hWnd,Msg,wParam,lParam);
end;

begin
ZeroMemory(@winClass, SizeOf(winClass));
with winClass do
  begin
  hInstance := SysInit.hInstance;
  hIcon := LoadIcon(hInstance,'MAINICON');
  lpfnWndProc := @WindowFunc;
  hbrBackground := COLOR_BTNFACE+1;
  hCursor := LoadCursor(0,IDC_ARROW);
  lpszClassName := 'A Class Name';
  end;

RegisterClass(winClass);

hForm1 := CreateWindow(winClass.lpszClassName, 'Form Caption',
    WS_OVERLAPPEDWINDOW or WS_VISIBLE, 100, 50, 356, 220, 0, 0, hInstance, nil);

CreateWindow('STATIC','A Title Label',
 WS_VISIBLE or WS_CHILD or SS_CENTER,3,10,347,16,hForm1,0,hInstance,nil);

hButton1 := CreateWindow('BUTTON', 'Close Me',
    WS_VISIBLE or WS_CHILD or BS_PUSHBUTTON,
    140,146,64,28,hForm1,0,hInstance,nil);

SendMessage(hButton1,WM_SETFONT,GetStockObject(ANSI_VAR_FONT),0);

SendMessage(CreateWindow('BUTTON', 'Show Modal',
    WS_VISIBLE or WS_CHILD or BS_PUSHBUTTON,
    8,46,94,26,hForm1,ID_ModalBut,hInstance,nil),
    WM_SETFONT,GetStockObject(ANSI_VAR_FONT),0);

while GetMessage(MainMsg,0,0,0) do
  begin
  TranslateMessage(MainMsg);
  DispatchMessage(MainMsg);
  end;

END.
0
 
GroundFloorAuthor Commented:
thanks, i was able to work out what i needed from your example
0
 
Slick812Commented:
I was hoping that you would notice that I placed the

EnableWindow(hForm1, True);

in the wrong place, it would be better if it was like this -


function WindowFunc(hWnd, Msg, wParam, lParam: Integer): Integer; stdcall;
begin
case Msg of
  WM_CLOSE: if hWnd = hModal then EnableWindow(hForm1, True);// better place
  WM_DESTROY: if hWnd = hForm1 then PostQuitMessage(0);
   
  WM_COMMAND: if lParam = hButton1 then PostMessage(hForm1,WM_CLOSE,0,0)
    else if LOWORD(wParam) = ID_ModalBut then
     ShowModal
     else if LOWORD(wParam) = ID_OkBut then
     PostMessage(hModal,WM_CLOSE,0,0);
  end;
Result := DefWindowProc(hWnd,Msg,wParam,lParam);
end;
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now