Special application from VB 6 to VB.NET - form

zhaozj2021-02-17  52

Special application from VB 6 to VB.NET - form

Li Honggen

First, summary

VB .NET is a VB6 upgrade version with many new features, it can create a .NET application (including XML Web Services and ASP.NET web applications), or a powerful object-oriented programming Language (such as inheritance, interface and overload). The new language features include free thread processing and structural exception handling. VB.NET is fully integrated with .NET framework and public language runtime, .NET framework, and public language runtime to provide language interoperability, garbage collection, enhanced security and improved version support. It can be said that it is an epochial product!

During the development of VB6 to VB.NET, the form application is always an eternal topic. Any Windows application is closely related to the form, in many occasions, we need to make some special settings or operations for the form, this paper combines with VB6 and VB.NET to illustrate the special problem of the form application And handle, as well as the new features brought by VB.NET!

Second, the body

1. Create a special shape of the form

Let's take a look at the implementation in VB6, realize in VB6 (with API functions)

It is also necessary to use a weird window. It is also the most important function in this program is setWindowRgn.

Its function is to scribble to the specified window, leave the rest of this window you choose to erase

Parameters: hwnd: The handle of the window you want to redraw, such as you want to redraw Form1, you should let this parameter is form1.hwnd

HRGN: The handle of the area you want to keep, this handle is the key, you need to get through other channels

The area here is a new area synthesized by Combinergn

BREDRAM: Do you want to scruck immediately, a usually set to true

Function combinergn combines two zones into a new area

Function CreateRectrGn is a rectangular area that is created by points x1, y1, and x2, y2

Function CreatellipticRGN is an ellipse area of ​​X1, Y1 and X2, Y2

Use DeleteObject this function to delete GDI objects, such as brush, brush, font, bitmap, area, and palette, and more. All system resources used by objects will be released

The following is the code of VB6:

Private Declare Function CreatellipticRgn Lib "GDI32" (Byval Y1 As Long, Byval X2 As long, Byval Y2 As long) AS Long

Private Declare Function CreateRectrgn LIB "GDI32" (Byval Y1 As Long, Byval x2 As long, Byval Y2 As long) As long

Private Declare Function Combinergn LIB "GDI32" (Byval Hsrcrgn1 As Long, Byval Hsrcrgn2 As Long, Byval NCombinemode As long) As long

Private Declare Function SetWindowRgnn Lib "User32" (Byval HRGN As Long, Byval Bredraw As Boolean) AS Long

Private Declare Function DeleteObject LIB "GDI32" (Byval Hobject As Long) AS Longprivate const rgn_diff = 4

Private sub flow_load ()

DIM RGN As Long

DIM RGNRECT AS Long

DIM RGNDEST AS Long

RGN = CreatellipticRGN (0, 0, Me.width / Screen.twipsPerpixelx, Me.Height / Screen.twipsPerpixely)

RGNRECT = CreateRectrgn (Me.Width / Screen.twipsPixelx - 20) / 2, (Me.Height / Screen.TwipsPixely - 20) / 2, (Me.Width / Screen.twipsPerpixelx 20) / 2, (Me.Height / Screen.TwipsPerpixely 20) / 2)

Rgndest = CreateRectrgn (0, 0, 1, 1)

Combinergn RGndest, RGN, RGNRECT, RGN_DIFF

SetwindowRgn Me.hwnd, Rgndest, True

Call DeleteObject (RGNRECT)

Call DeleteObject (rgndest)

End Sub

Private submmand1_click ()

End

End Sub

In VB.NET, we can use the .NET Framework class library system.drawing.drawing2d GraphicsPath class (application use path to draw shapes, fill the shape inside and created the clip area), to draw graphics,

Then set the visible area of ​​the window through the ME. AGION of the form.

The following is the code of VB.NET:

'Declare a Boolean variable, determine if the form is normal

DIM isnorMalRegion as boolean = TRUE

Private sub button2_click (byval sender as system.object, _

Byval e as system.eventargs) Handles Button2.click

IF (isnorMalregion) THEN

'Construct an instance of a GraphicsPath object

DIM graphics as new system.drawing.drawing2d.graphicspath ()

DIM INTHEIGHT AS INTEGER = me.size.height

DIM INTWIDTH AS INTEGER = Me.Size.width

'Define the upper left corner coordinates of inner rectangles

Dim RectTop as integer = 100

'Draw a large ellipse on the form, the coordinates in the upper left corner are taken (0,0)

Graphics.addellipse (0, 0, INTWIDTH, INTHEIGHT)

'Draw a small rectangle

DIM AddRect As New Rectangle (RectTop, Recttop, INTHEIGHT - (RectTop * 2), INTHEIGHT - (RectTop * 2))

Graphics.adDRectangle (AddRect)

'Set the visible area of ​​the window

Me.Region = New Region (Graphics)

Else

Me.Region = Nothing

END IF

IsNorMalRegion = NOT ISNORMALREGONEND SUB

The result of the program run is as follows:

2, make the form above all other forms (Allway On TOP)

Implementation in VB6 (API function setWindowPOS)

Private declare function setwindowpos lib "user32" (Byval Hwnd As Long, _

Byval hwndinsertafter as long, byval x ask, BYVAL Y AS Long, _

BYVAL CX As Long, Byval Cy As Long, Byval Wflags As Long AS Long

---- HWND variable element is the handle of the window; x, y is the coordinate of the upper left corner of the window; CX, CY is the window width and height; HWndInsertAfter zoning is the window handle in front of the HWND window in the window list, there are four Selected value: Selected value of the serial number 1 HWND_BOTTOM put the window at the bottom of the window list 2 HWND_TOP put the window on the window list The top 3 hWnd_topmost put the window on the top of the window 4 hWnd_notopMost put the window in the window list Top, under the uppermost window ---- WFLAGS variable yuan is integer, there are eight optional values: serial number optional value 1 SWP_DRAWFRAME draws a box 2 SWP_HIDEWINDOW Hide Window 3 SWP_NOACTIVATE Do not activate window 4 SWP_NOMOVE Keep Window Current Position 5 SWP_NOREDRAW Window Not Auto Heavy Pictures 6 SWP_NOSIZE Hold Window Current Size 7 SWP_NOZORDER Tool window in the current location of the window in the list 8 SWP_SHOWINDOW display window

Private declare function setwindowpos lib "user32" (Byval Hwnd As Long, _

Byval hwndinsertafter as long, byval x ask, BYVAL Y AS Long, _

BYVAL CX As Long, Byval Cy As Long, Byval Wflags As Long AS Long

Private const swap_nomove = 2

Private const swP_nosize = 1

Private const flags = swp_nomove or swp_nosize

Private const hwnd_topmost = -1

PRIVATE CONST HWND_NOTOPMOST = -2

Private submmand1_click ()

'Put the form in the front:

Res% = setWindowPos (form1.hwnd, hwnd_topmost, 0, 0, 0, 0, flags)

End Sub

Private sub fascist2_click ()

'Enable the form to restore normal mode:

RES% = setWindowPos (Form1.hwnd, Hwnd_Notopmost, 0, 0, 0, 0, Flags)

End Sub

In VB.NET, it is too simple! The system provides topMost properties for the form. We set the TOPMOST property to true. If you implement the function of the AllWays on Top, you want to cancel this feature, set to false.

Private Sub Form1_Load (Byvale AS System.Object, Byval E As System.Eventargs) Handles MyBase.Loadme.topmost = True

End Sub

3, form transparency gradient effect

Let's take a look at the implementation in VB6, VB6 is implemented (with API functions setlayeredWindowAttributes)

Use this function to easily control the transparency of the form. According to Microsoft's requirements, the transparent form should use the WS_EX_LAYERED parameter (with CREATEWINDOWEX) when creating, or set this parameter after being created, and I use the latter.

SetlayeredWindowAttributes function, where hwnd is a transparent form handle, crkey is a color value, Balpha is transparency, the value range is [0,255], DWFlags is a transparent mode, can take two values: When the value is LWA_alpha, the CRKEY parameter is invalid. The Balpha parameter is valid; when the value is LWA_COLORKEY, the Balpha parameter is valid and all colors in the form will become transparent.

Const Lwa_colorKey = & H1

Const lwa_alpha = & h2

Const gwl_exstyle = (-20)

Const WS_EX_LAYERED = & H80000

Private Declare Function GetWindowlong Lib "User32" Alias ​​"getWindowlonga" (Byval Nindex as long) As long

Private Declare Function SetWindowlong Lib "User32" Alias ​​"Setwindowlonga" (Byval Nindex As Long, BYVAL DWNEWLONG AS Long) As long

Private Declare Function SetlayeredWindowAttributes LIB "User32" (Byval Hwnd As Long, Byval Balpha As Byte, BYVAL DWFLAGS AS Long) AS Long

Private sub flow_load ()

DIM RET As Long

'Set the window style to' layer

Ret = getWindowlong (me.hwnd, gwl_exstyle)

RET = RET or WS_EX_LAYERED

SetWindowlong Me.hwnd, GWL_EXSTYLE, RET

'Set the opacity of the layered window to 128

'We can set this value to control transparency

SetlayeredWindowAttributes me.hwnd, 0, 128, lwa_alpha

End Sub

In VB.NET, it is too simple! The system provides an Opacity property for the form to determine the opaque and transparency of the form, 0% is transparent, 100% is opaque.

The following procedure displays the transparency process of the form by looping, in order to let everyone see it clearly, system.threading.thread.sleep is used in the cycle. Private sub button1_click (byval sender as system.Object, _

Byval e as system.eventargs) Handles Button1.click

'Transparency gradient process of form

Button1.enabled = false

DIM I as Double

For i = 0.01 to 1 step 0.01

Me.opacity = i

System.windows.Forms.Application.doevents ()

System.threading.Thread.sleep (5)

NEXT

Me.opacity = 1

Button1.enabled = TRUE

End Sub

4, make the x in the upper right corner of the form, prohibit ALT F4 to close the form

In special form applications, we sometimes need to turn off the button on the top of the form on the upper right corner of the form. When the user hits other places (for example, a button) exits, what do we do? .

Let's take a look at the implementation in VB6, realize in VB6 (with API functions)

Private Declare Function GetSystemMenu Lib "User 32" (Byval Brevert As long) As long

Private Declare Function GetMenuItemCount Lib "User32" (Byval HMENU As Long) As long

Private Declare Function DrawMenuBar LIB "User32" (BYVAL HWND As Long) As long

Private Declare Function Removemenu Lib "User32" (Byval NPSITION As Long, Byval Wflags As Long) AS Long

Const MF_BYPSITION = & H400 &

Const mf_remove = & h1000 &

Private sub flow_load ()

DIM HSYSMENU As Long, NCNT As Long

'Get Handle To Our Form's System Menu

(Restore, Maximize, Move, Close etc.)

Hsysmenu = GetSystemMenu (me.hwnd, false)

IF hsysmenu kil

'Get System Menu's Menu Count

NCNT = GetMenuItemCount (HSYSMENU)

IF NCNT THEN

'Menu Count Is Based On 0 (0, 1, 2, 3 ...)

REMOVEMENU HSYSMENU, NCNT - 1, MF_BYPOSITION OR MF_REMOVE

REMOVEMENU HSYSMENU, NCNT - 2, MF_BYPOSITION OR MF_REMOVE 'Remove The SEPERATOR

Drawmenubar (Me.hwnd)

'Force Caption Bar's Refresh. Disabling X ButtonMe.caption = "Try to Close Me!"

END IF

END IF

End Sub

'If you still have to block Alt F4, plus

Private Sub Form_Queryunload (Byval Cancel AS Integer, Byval UnloadMode As Integer)

Cancel = 1

End Sub

In VB.NET, this time you need to use the API because the system does not provide such a class, this example, provides an example of an API using an API. (Because the system class library packages most of the API, it is not recommended)

The following is the code of VB.NET:

'API declaration

Private Declare Function GetSystemMenu Lib "User32" (Byval Brevert As long) AS Integer

Private Declare Function RemoveMenu Lib "User32" (Byval NPosition As Integer, Byval WFLAGS AS INTEGER) AS INTEGER

Private Declare Function DrawMenuBar LIB "User32" (Byval Hwnd As Integer) AS Integer

Private Decock Function GetMenuItemCount LIB "User32" (Byval HMENU As INTEGER) AS INTEGER

Private const mf_beposition = & h400 &

PRIVATE CONST MF_DISABED = & H2 &

Private sub disableX (Byval WND As Form)

DIM HMENU AS INTEGER, NCOUNT AS INTEGER

'Get the system menu

HMenu = getSystemMenu (wnd.handle.toint32, 0)

'Get the number of system menu

Ncount = getMenuItemcount (HMENU)

'Remove System Menu

Call Removemenu (HMENU, NCOUNT - 1, MF_BYPOSITION OR MF_DISABED)

'Heavy painting menubar

Drawmenubar (Me.Handle.Toint32)

End Sub

Private Sub Form1_Load (Byval E AS System.Object, Byval E AS System.Eventargs) Handles MyBase.LOAD

'Use x can't use

DisableX (ME)

End Sub

Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click

'close the window

Me.close ()

End Sub

'If you still have to block Alt F4, plus

Protected Overrides Sub WndProc (Byref M As System.windows.Forms.Message)

DIM SC_CLOSE AS INTEGER = 61536dim WM_SYSCOMMAND AS INTEGER = 274

'Judgment is the system message, is it close to the form, so Alt F4 is invalid

IF m.msg = wm_syscommand andalso m.wparam.toint32 = sc_close then

EXIT SUB

END IF

Mybase.wndproc (m)

End Sub

The result of the program run is as follows:

5, the drag problem of the form without the title bar

In the application of special forms, we sometimes need to block the title bar of the form and replace its own housing with the form. Yes, when the form title bar is removed, the mobile form has a problem.

Let's take a look at the implementation in VB6, realize in VB6 (with the API function sendMessage)

Set the Border attribute of the form to 0-NONE when designing

Private Declare Function SendMessage Lib "User32" Alias ​​"SendMessagea" (Byval Hwnd As Long, Byval WParam As Any) AS Long

Private Declare Sub ReleaseCapture LIB "User32" ()

Const wm_nclbuttondown = & ha1

Const htcaption = 2

Private Sub Form_Mousemove (Button As Integer, Shift As Integer, x as single, y as single)

Dim LngreturnValue As Long

If Button = 1 THEN

'RELEASE CAPTURE

Call ReleaseCapture ()

'Send a' Left Mouse Button Down on Caption'-Message To Our Form

LNGRETURNVALUE = SendMessage (me.hwnd, wm_nclbuttondown, htcaption, 0 &)

END IF

End Sub

Private sub form_paint ()

Me.Print ("Click on The Form, Hold The Mouse Button and Drag IT")

End Sub

In VB.NET, this time you need to use the API SendMessage.

Set the form.formorderstyle property to none when designing, then add the following code:

Declare function sendMessage Lib "User32" Alias ​​"SendMessagea" (byval hwnd as integer, byval wmsg as integer, byval wparam as integer) AS integer

Private Declare Sub ReleaseCapture LIB "User32" ()

Const wm_nclbuttondown = & ha1

Const htcaption = 2

PRIVATE SUB FORM1_MOUSEDOWN (Byval e as system.windows.forms.mouseeventargs) Handles mybase.mousedownreleaseCapture ()

SendMessage (Me.Handle.Toint64, WM_NCLBUTTONDOWN, HTCAPTION, 0)

End Sub

Third, conclude

The above instances run in Windows 2000, VB6, and VS.NET environments. From the above instance, we can see that the previous VB6 has no many attributes and methods, which have been provided in VB.NET, and .NET provides many class libraries, which can be done in VB6 to achieve a large number of APIs can be implemented. operating. For example, build a multi-thread application, it is easy to use VB.Net! What is worth mentioning is that VB.NET is a complete object-oriented, easier to encapsulate our business logic, build an enterprise-level application such as N-layer applications. I love VB6, love .net!

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

New Post(0)