. Net (C #) platform call: does not rely on the platform's getwindowlongptr and setwindowlongptr APIs

Source: Internet
Author: User

First, in the method declaration, because getwindowlongptr and setwindowlongptr are only macro-defined and not specific functions on 32-bit windows, the getwindowlong and setwindowlongptr functions can only be used. Therefore, we need to define two such functions. The second is parameter changes in different environments. For example, the function prototype of getwindowlang:

Long winapi getwindowlong (

_ In hwnd,

_ In int nindex

);

 

It returns long. The function prototype of getwindowlongptr is as follows:

Long_ptr winapi getwindowlongptr (

_ In hwnd,

_ In int nindex

);

 

It returns long_ptr. Other types such as int_ptr, uint_ptr, dword_ptr ...... It is used to make the defined types easy to run correctly on 32-bit and 64-bit APIs without changing. In 32 bits, they keep their default size. In 64-Bit mode, they are extended to 64-bit. Note that. net, Int Is always 32-bit (Int Is only system. int32 type alias), while long (system. int64 type) is always 64-bit, so we can only use intptr that depends on the platform size to represent the above data type.

 

First, declare the four APIs:

[Dllimport ("user32.dll", entrypoint = "getwindowlong")]

Static extern intptr getwindowlong32 (intptr hwnd, int nindex );

 

[Dllimport ("user32.dll", entrypoint = "getwindowlongptr")]

Static extern intptr getwindowlong64 (intptr hwnd, int nindex );

 

[Dllimport ("user32.dll", entrypoint = "setwindowlong")]

Static extern intptr setwindowlong32 (intptr hwnd, int nindex, intptr dwnewlong );

 

[Dllimport ("user32.dll", entrypoint = "setwindowlongptr")]

Static extern intptr setwindowlong64 (intptr hwnd, int nindex, intptr dwnewlong );

 

Then, use a special method to determine whether the execution environment is a 32-bit or 64-bit, and then call the corresponding local API according to the environment.

// Windowlongflags definition can refer to: http://www.pinvoke.net/default.aspx/Enums/WindowLongFlags.html

 

Public static intptr getwindowlongptr (intptr hwnd, windowlongflags nindex)

{

If (intptr. size = 8)

Return getwindowlong64 (hwnd, (INT) nindex );

Else

Return getwindowlong32 (hwnd, (INT) nindex );

}

 

Public static intptr setwindowlongptr (intptr hwnd, windowlongflags nindex, intptr dwnewlong)

{

If (intptr. size = 8)

Return setwindowlong64 (hwnd, (INT) nindex, dwnewlong );

Else

Return setwindowlong32 (hwnd, (INT) nindex, dwnewlong );

}

 

After the method is defined, you can call it for execution. However, another problem is the setting of enumeration values. Because the corresponding enumeration object depends on the platform, it is defined as intptr, however, intptr may be difficult to set enumeration values. You can use the following method.

 

First, we use this article (. net (C #): The enumhelper type in the negative and positive digit fields), and then define a good encapsulation type for intptr-based enumerated values: intptrenumhelper:

Static class intptrenumhelper

{

// Determine whether the specified flag is included

Public static bool hasflags (intptr Val, object flag)

{

Return enumhelper. hasflag (Val. toint64 (), (long) Flag );

}

// Set the flag

Public static intptr setflag (intptr Val, object flag)

{

Return new intptr (enumhelper. setflag (Val. toint64 (), (long) Flag ));

}

// Cancel the flag

Public static intptr unsetflag (intptr Val, object flag)

{

Return new intptr (enumhelper. unsetflag (Val. toint64 (), (long) Flag ));

}

}

 

In this case, we want to set the style of the form through setwindowlongptr. We can directly use the above method to set or cancel bitfield settings for intptr-based enumeration objects.

 

The following code is encapsulated:

// Windowlongflags definition can refer to: http://www.pinvoke.net/default.aspx/Enums/WindowLongFlags.html

 

// Set the flag

Public static intptr setwindowstyles (intptr hwnd, windowstyles ws)

{

VaR style = getwindowlongptr (hwnd, windowlongflags. gwl_style );

Return setwindowlongptr (hwnd, windowlongflags. gwl_style, intptrenumhelper. setflag (style, WS ));

}

// Cancel the flag

Public static intptr unsetwindowstyles (intptr hwnd, windowstyles ws)

{

VaR style = getwindowlongptr (hwnd, windowlongflags. gwl_style );

Return setwindowlongptr (hwnd, windowlongflags. gwl_style, intptrenumhelper. unsetflag (style, WS ));

}

 

Download Code:

Refer to this article:. Net (C #): Win32 form API encapsulation Project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.