C# GDI+ Programming (5)
Call the API function to draw in the non-client area of the window
There is a FromHdc function in the Graphics class of GDI+. This function can create a Graphics object based on the window device context (DC). In vc++, drawing in the window client area and non-client area It's nothing more than different calls to the GetWindowDC and GetDC functions. The former gets the entire window DC, and the latter gets the window client area DC.
Then we can call the GetWindowDC function in C# to get the entire window DC, and then load it through FromHdc, so that we can draw for the entire window.
How to call WINDOWS API in C#, or how to call functions in dynamic link library (DLL).
Similar to VC++, import the dynamic link library first, and then declare the API function, as follows:
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
Of course, the above is the simplest, and there are still some details that I haven’t talked about. Let’s just leave it like this for now. Just be able to use it basically. Those details will be explained in detail later.
In C#, we find that the parameter types of API functions are different, such as handles HDC and HWND in VC++. When declaring here, IntPtr was used instead. There is no way to do this, because C# does not have the concept of pointers, and when we checked the HDC and HWND type definitions, we found that they are both pointer types.
So in C#, these "handle" types are replaced by IntPtr, including area handles HRGN, HICON icons, HFONT font handles, etc.
Let’s look at an example, (continuing from the previous chapter)
Public partial class Form1: Form
{
//Import the dynamic link library and declare the function. This function is declared in the Form1 class.
does not do not have the same effect? path = new GraphicsPath();
/ /Load PNG Pictures t Bitmap BMP = New Bitmap ("D: \ Image \ Win.png");
Public form1 () {
initializedComponent (); Area
Color cor = bmp.GetPixel(x, y); B int argb = cor.toargb ();
Byte [] Bargb = Bitconverter.getBytes (ARGB);
// Pixel color value is not transparent
if (bargb [3]! = 0) {
// This is this this Add the pixel area to the path
. //Set the window display area and create a region through the path
This.Region = new Region( PATH); i this.paint+= formpaint;
}}
Private void Formpaaint (Object SENDER, PAINTEVENTARGS E) {
OnPaintBackground (E);
// Handle is the window handle, It is an Intptr type
intptr HDC = GetWindowDC (this.Handle); new Rectangle(0, 0, bmp.Width , bmp.Height));
}
protected override void OnPaintBackground(PaintEventArgs e)
. this.ClientRectangle);
How about it, the effect is good, but as soon as you drag the window, the original shape is revealed. Have you noticed the shadow under the apple? It is just to achieve this effect that it will cause some problems, or it will be a lot of trouble. It’s just that I didn’t solve it. Moving the window or maximizing the window without completely refreshing the entire window will cause this problem. This problem will be solved later.
Friends who are interested can also solve this problem.
Form )
MessageBox.Show("You double-clicked the title bar");
When you click the title bar, a prompt will be given, and then Then handle it by default. To check the value corresponding to the message, you can check it in the VC++ compiler. For example, mark WM_LBUTTONDOWN and then right-click and select Go to definition to view it.
m.HWnd stores the window handle. m.LParam and m.WParam are incidental information of the message. You can refer to the explanation of WPARAM and LPARAM parameters in the CreateWindow function.
The workload of self-painting the non-client area is really too much. Here I will only give you a general idea and direction. I will do it again when I have time.
The premise is of course to calculate various data, such as whether the window has borders. If so, obtain the border width and height, and then calculate the rectangular area of the four borders.
Finally, determine whether the window has the maximum and minimum attributes, and then obtain the area for the three buttons.
These data are stored in the SystemInformation class. For example, SystemInformation.CaptionButtonSize stores the size of the title bar button. Once you get the size, you can
For more C# GDI+ Programming (5) related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.
