Use DirectX in C # To enable winform)

Source: Internet
Author: User

Preparations:
1. Install the DirectX SDK (with 9 DLL files ). Here we only use Microsoft. DirectX. dll and Microsoft. DirectX. directsound. dll.
2. a wav file. (These files are easy to find. There are a lot of files in the QQ directory. .) The name is SND. wav, which is placed at the end of the target. Program Under the same directory
Start writing programs. Just use ultraedit.
1. The namespace of the DLL file that introduces DirectX:
Using Microsoft. DirectX;
Using Microsoft. DirectX. directsound;
2. Create a device. There is a device class in the imported Microsoft. DirectX. directsound space. This indicates the sound device in the system.
Device DV = new device ();
3. Set cooperativelevel. Because Windows is a multitasking System and the device is not exclusive, you must set cooperativelevel for the device before using it. Call the setcooperativelevel method of device: the first parameter is a control parameter, and the second parameter is an enumeration type.
In this program, I randomly set a parameter to control (very sweaty !). In a Windows program, you can use this instead. The second parameter is the priority level, which indicates the priority level.
DV. setcooperativelevel (New UF (), cooperativelevel. Priority );
4. Open the buffer zone. For the above sound device, he has his own buffer zone called the main buffer zone. In the system, a device has a unique primary buffer. Windows is a multi-task (this is the same !), So there are several programs that can use a device to play the sound at the same time, so each program opens a second-level buffer for its own sound.
Based on the priority of each program, the system reads the content from each second-level buffer to play in the primary buffer in the corresponding order. Here, we open up a buffer zone for SND. wav.
The first parameter indicates the file name (which can be seen by dummies !), The second is the device to be used.
Secondarybuffer Buf = new secondarybuffer (@ "snd.wav", DV );
5. Now you can play the video. The first parameter indicates priority, and 0 indicates the lowest priority. The first parameter is the playback mode. Here it is loop playback.
Buf. Play (0, bufferplayflags. looping );
6. Because the command line program does not have a message loop, the execution is complete. Code So we need to pause the program.
Console. Read ();
7. The key part is complete. Here we will just explain what the unlucky new UF () was just now. This is to meet the parameter requirements of setcooperativelevel. I don't know how this works (if you have burned your sound card ............)
Class UF: FORM {}
8. The Code has been written ~~~. The following code can be compiled. Compilation here is more complex.
CSC/R: DirectX \ Microsoft. DirectX. dll; DirectX \ Microsoft. DirectX. directsound. dll dxsnd. CS
Here, I put the two DLL files in the DirectX directory of the current directory (this is built by myself, you only need to point out the location of the two files .)
By the way, I will describe the directory structure:
|
| -- Dxsnd. CS
| --Snd.wav
| -- <DirectX>
|
| -- Microsoft. DirectX. dll
| -- Microsoft. DirectX. dll
The complete code is as follows:
// Dxsnd. CS
Using system;
Using Microsoft. DirectX;
Using Microsoft. DirectX. directsound;
Using system. Windows. forms;
Namespace test1
{
Class Test
{
Public static void main (string [] ARGs)
{
Device DV = new device ();
DV. setcooperativelevel (New UF (), cooperativelevel. Priority );
Secondarybuffer Buf = new secondarybuffer (@ "snd.wav", DV );
Buf. Play (0, bufferplayflags. looping );
Console. Readline ();
}
Class UF: FORM {}
}
}

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.