[Original] Use Delphi to implement the size of the IE form - ISizer program principle analysis (2003-12)

xiaoxiao2021-03-06  183

Friends who have done a web page know that there is a very important issue when designing a web page, that is to have a good display effect on different resolutions (reflected in the visual screen size); another time may To do some of the special web pages (such as advertising or notifications), when designing a web page, you may need frequently testing the effects under different browsers. At these times, we can't keep changing the system resolution. ! (Executive people and no efficiency, let alone who will be so stupid, huh, if you can control the size of the browser as you want! Some friends will definitely say: "Those automatic advertising windows can use JavaScript to control the size." Yes! Since JavaScript can control the form size of the browser by calling a function, the powerful Delphi is certain! Thinking, I will roll up the sleeve immediately, prepare to give yourself a program that controls the browser form size (the most common browser currently used, we will try to control the size of the IE). First we analyze the functions that the program needs to be implemented: 1 After the program is started, since there is no IE form being controlled in this program, then you need to open a new IE form after pressing the control button in the form. Then set this IE window to the specified width (helight); 2 during operation, each time the control button is pressed, first determined whether the IE window opened by this program exists, if there is adjustment Its width and height are currently specified, otherwise it will open an IE form and then set it; the initial function seems very simple, but I still spend a lot of time for the rookie. By reviewing related books and MSDN, I found some information required to implement this program. Let's first learn about some of the following functions: 1 getWindowsDirectory (): Return to the Windows System Directory path. Function prototype: uint getWindowsDirectory (LPTSTR LPBUFFER, / / ​​pointer points to the size of the Windows directory buffer UINT USIZE // directory buffer); 2 Winexec (): Run the specified application. Function prototype: uint Winexec (LPCSTR LPCMDLINE, / / ​​pointer points to the address of the address of the command line UINT UCMDSHOW // The form type of the newly running program); MS recommends using the createProcess () function when running the Win32 program. However, since Winexec () is relatively convenient, and it is running in a synchronous manner (that is, the function will wait until the function running is started, this is the way we need this program), so In this program I used Winexec (); and createProcess () is asynchronous (it does not wait for the initialization of the sub-process it created, but directly returns), you need to use the waitforInputIdle () function to achieve synchronization. 3 getWindow (): Search and return a handle with a specified form with a specified relationship (z order or for the owner). Function prototype: hwnd getWindow (HWND HWND, // Initial Form Handle UINT UCMD // Relational Tag); 4 getClassName (): Returns the form class name to which the specified form belongs.

Function prototype: int getClassName (HWND HWND, // Form Handle LPTSTSTSTSTSTSTSTSTSTSTSTSTSNAME, / / ​​Form Cycle Cuffed address int nmaxcount // buffer size, in bytes); DFW searches for MS Internet Explorer windows The body class name is "IEframe". 5 setWindowPos (): Change the size, location, etc. of the specified window. Function prototype: BOOL SETWINDOWPOS (HWND HWND, // Form Hand HWND HWNDINSERTAFTER, / / ​​Sub-positioning handle INT X, // horizontal position int y, // vertical position int Cx, // Width int CY, // high uint uflags // Form configuration tag); if the function runs successfully, returns a non-zero value, otherwise (if the specified form handle is invalid) returns zero. OK, with these functions, we can start writing the code. When you start Delphi and create a new project, call the shellapi unit in the unit file (this example is main.pas) and declare a global variable HIEWND: Uses Windows, Messages, ..., shellapi ;. ..... var mainform: tMAINFORM; HIEWND: HWND; {Controlled IE Form Handle} ...... then declare the following function functions and processes in the private segment of the main window TMAINFORM:. .... private procedure SetIEWindowSize (Width, Height: Integer); procedure CreateAnIeWnd; function GetIeWndHandle: Boolean; ...... their codes are as follows: to open a new IE window {} procedure TMainForm.CreateAnIeWnd; var WinDir: array [0..max_path] of char; {System Directory path} sysdir: string; {System location} begin {obtain the partition (for example, C: /) where the system is located. Since the function of obtaining the partition disk letter where the current system is located, you have to get the system directory path, and then intercept the top three characters to get the system drive letter.

If you have a better solution, please contact me, thank you! } GetWindowsDirectory; {Due to the returned character serial pointer, you need to take it out and save it to the sysdir variable} sysdir: = strpas (@windir); sysdir: = Copy (Sysdir, 1, 3); {Take the first three characters} {Run IE} Winexec (Pchar (Sysdir 'Program files / Internet Explorer / ipplore.exe'), sw_show); End; {Looking for IE Form and return its handle} function TMAINFORM.GETIEWNDHANDLE: Boolean; var hCurrwnd: hw; {{{{{} Classname: array [0..10] of char; {Class name of the current form} flag_ie : Boolean; {IE formwork found} begin result: = false; flag_ie: = false; {start searching from the current form} hcurrwnd: = getWindow (Handle, GW_HWndFirst); {Setting relational tag value is gw_hwndfirst, ie Search from the top of the Z sequence until you find the IE form or all the forms are traversed} while hcurrwnd <> 0 do begin getclassname (hcurrwnd, @classname, 10); if strpas (@classname) = 'iprame' Then Begin HIEWND : = hcurrwnd; flag_ie: = true; result: = true; end; {Search Next Form} hcurrwnd: = getWindow (hcurrwnd, gw_hwndnext); if flag_ie then hcurrwnd: = 0; end; end; {Set specified handle IE window size} Procedure Tmainform.Setiewindowsize (width, height: integer); Begin {If the Controlled IE form exists} if HIEWND <> 0 the begin {set the form size of the specified width (Width) and high. If the execution is unsuccessful (if the specified form handle is invalid), the recorded IE form handle is clear, and it is called itself to recreate the controlled IE form} if not setWindowPos (HIEWND, HWND_TOP, 0, 0, WIDTH) , Height, SWP_SHOWINDOW) THEN BEGIN HIEWND: = 0; setiewindowsize (width, height); end; end else {If there is no controlled IE form, call itself to recreate controlled IE forms} Begin CreateAnieWnd; If GetiewndHandle Ten SetiewInDowsize (width, height); end; end; here, most of the program is complete, we can test it.

First initialize the HIEWND variable in the mainform.oncreate event: Procedure TMAINFORM.FORMCREATE (Sender: TOBJECT); begin hieWnd: = 0; END; then placed a Button1 in the form, write the following code in its OnClick event: Procedure TMAINFORM .Sizebtn1click (sender: TOBJECT); Begin Setiewindowsize (640, 430); end; save the project and pressing the F9 running program, IE after pressing the button, but not only the launch of the obsession, but also adjusts the size of us (640 * 430, this It is a substantially magnitude of the IE window on the desktop at 640 * 480 resolution), the effect is very good :). Next, I put four Button and two Edit on the main form, three button as the shortcut button, which used to control the IE form size (640 * 430: 640 * 480 resolution), (800 * 550: 800 * 600 Resolution), (1024 * 718: 1024 * 768 1168 resolution), the fourth Button is used to control the value entered in two edits (don't forget to input The value is judged, only the non-negative integer is detected. The program interface is shown in Figure 1. I have to tell me! Don't worry, there is such a problem: When you open an IE form through this program, you minimize this IE window, no matter which button you press! This is not good, hurry and go, and finally finds showWindow () such a function, which is used to set the form to a specified display state. The function prototype is as follows: BOOL ShowWindow (HWND HWND, // Form Handle INT NCMDSHOW // Form Display Status); then simply, just add such a code in the onclick event in each button, then call SetiewIndowsize ( Enable controlled IE forms to be converted back to normal state and then control its form size: procedure tMainform.sizebtn1click (sender: TOBJECT); begin {Restore the controlled IE form to normal display (if you are smallized During ShowWindow (HIEWND, SW_NORMAL); SETIEWINDOWSIZE (640, 430); END; in which the parameter value sw_normal represents: activation and display a form; if this form is minimized or maximized, first adjust back Original size and location. To this end, we finally completed a program that controls the size of the IE form - Iesizer. Feeling is not difficult and is it interesting? :) The principle of this program is to run a IE and get its handle, and then controlled its size via the API (but I spend most of the time to find the data and debug the parameter settings for each function, huh). After understanding this, we can even control almost all known forms, of course, the premise is that you have to know their form name. The above program is compiled in Delphi 7 Win2000 Pro / XP Pro.

转载请注明原文地址:https://www.9cbs.com/read-128526.html

New Post(0)