The three shots use Direct technology to perform image cutting three (end)

xiaoxiao2021-03-06  111

It is N (n> 7), there is no to maintain his blog, or because there is no time, today I remember that there is still an article in my blog (really a person who is not responsible), so, no matter How to take a lot of time today to complete your stuff, you can't bring a halfway. Today is the last thing, and nonsense is so long, I don't have to cut the theme. I can't help you. Today, we will enter the core of this lengthy story, and the last A part. Book is positive, there are two more important functions in Direct programming, but also two functions with high frequency, we must introduce these two functions before entering the topic, and we use them in this program. The first very useful function is bltfast (), his prototype is HRESULT BLTFAST (DWORD DWX, DWORD DWY, LPDIRECTDRAWSURFACE LPDDSRCSURFACE, LPRECT LPSRCRECT, DWORD DWTRANS); following these parameters one by one: (1) DWX and DWY : Where will the image will be transferred to the target page. (2) LPDDSRCSURFACE: The source page of the image transfer operation. The target page is a page that calls this method. (3) LPSRCRECT: An address of a Rect (Rectangle, ie, a rectangle) structure, indicating the area that will be transmitted on the source page. If this parameter is NULL, the entire source page will be used. The Rect structure is very common in DirectDraw. It is best to define a RECT type global variable, such as RECT, then write a function: void makerect () (about this function is the second very useful function I said. We will introduce and instructions on him later. (4) DWTRANS: Specifies the transfer type. There are several of the following: DDBLTFAST_NOCOLORKEY specifies a normal replication, without a transparent component. DDBLTFAST_SRCCOLORKEY specifies the transparent color-colored image transfer and uses the transparent color of the source page. DDBLTFAST_WAIT If the image transmitter is busy, returning until the image transmitter is ready and transferred. This parameter is usually used. Since the fourth parameters are relatively long, and sometimes two matching, most of them are defined two global variables, which is more convenient to use. DWORD SrcKey = DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAITDWORD NoKey = DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT second useful function is that we mentioned earlier void MakeRect (), which is generally a custom function, we can define him: void MakeRect (int left, int Top, int right, int = {rect.bottom = bottom; Rect.Light = left; Rect.right = right; Rect.top = top;} mainly determines the four margins of this R structure.

So it is very simple to cut a pair of pictures. Is it just ^ _ ^, give an example: Which background we have to post into the top of Sprimary, you can make MakeRect (0,0,640,480) LPDDSBuffer-> BLTFAST (0, 0, LPDDSPRIMARY, & R, NOKEY); Core part because of which background is 640 * 480, so our right side is 640, the lower edge is 480, and finally using the bltfast () function. It's okay, and there is no transparency, so we can watch it. But how can I cut? In fact, there is nothing difficult, you want to change the size of the map to map in MakeRect (), you can't give the picture part of any position in the same picture, this is cutting, then we set a variable, With the size of the mouse custom post, you must join the Windows message loop mechanism to use the plug. We get a coordinate at MouseDown. When you have another coordinate, you get another coordinate, between two coordinates The picture is sticked out. In fact, the main core technology of this cutting program is here, and the idea is to be such a thing, simple :), the other is some of the ends of the branches. However, we still have to introduce one by one. OK, here we can write a class separately, complete our picture cuts and map function, we write a class called draw.cpp, here we also write his header file Draw.h. Draw.h: #if! defined (Draw_h) #define Draw_Hclass Draw {public: int x1; int y1; int x2; int y2; void Rectanglep (int x1, int y1, int width, int hotht); // A mouse runs the function void Blt (INT X1, INT Y1, INT X2, INT Y2); // complete our picture clipping function}; # endifdraw.cpp: #include "main.h"

Void Draw :: BLT (INT X1, INT Y1, INT X2, INT Y2) {x1 = Drawp.x1; Y2 = Drawp.y1; X2 = Drawp.x2; Y2 = Drawp.y2; MakeRect (x1, y1, x2 , Y2); LPDDSBuffer-> Bltfast (0, 0, LPDDSMAP, & R, SrcKey);

Void Draw :: Rectanglep (int x1, int y1, int x2, int y2) {HDC HDC; LPDDSPRIMARY-> getDC (& HDC); // Main page get a handle HPEN red_pen = createpen (ps_solid, 0, rgb (0,255,0 ))); // Pack the color SelectObject (HDC, Red_pen) of the brush; // Set the type of brush LINE (X1, Y1, X1, Y2, HDC); // Picture LINE (x1, y1, x2, y1, hdc LINE (X2, Y1, X2, Y2, HDC); LINE (X1, Y2, X2, Y2, HDC); LPDDSPRIMARY-> ReleaseDC (HDC); // Release the handle after using it} This is the two self Define the specificization of the function. The first function is part of the cutting of the graphics, and defines four variables as the coordinates of the mouse point. The second function is the trajectory of the mouse on the screen. It is a rectangle consisting of four lines. It uses the API function map, which is Microsoft gives us a function (Microsoft is giving us a package. It is also convenient to get up). Then it is a class of the function of public use, which is customized to be all free use. We are defined as the initialization of publicfuction.cpp // Direct, before, you can see the chapter void init () {ddsurfaceDesc2 DDSD ; DirectDrawCreateEx (NULL, (void **) & lpDD, IID_IDirectDraw7, NULL); lpDD-> SetCooperativeLevel (hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN); lpDD-> SetDisplayMode (640, 480, 32, 0, DDSDM_STANDARDVGAMODE); memset (& ddsd, 0 , sizeof (DDSURFACEDESC2)); ddsd.dwSize = sizeof (ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; ddsd.dwBackBufferCount = 1; lpDD-> CreateSurface (& ddsd, & lpDDSPrimary , NULL); DDSD.DDSCAPS.DWCAPS = DDSCAPS_BACKBuffer; LPDDSPRIMARY-> GetAttachedSurface (& DDSD.DDSCAPS, & LPDDSBUFFER);

ddsd.dwSize = sizeof (ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; ddsd.dwHeight = 480; ddsd.dwWidth = 640; lpDD-> CreateSurface (& ddsd, & lpDDSMap, NULL) ; ddsd.dwHeight = 26; ddsd.dwWidth = 32; lpDD-> CreateSurface (& ddsd, & lpDDSMouse, NULL); ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; dDBLTFX ddBltFx; ddBltFx.dwSize = sizeof (dDBLTFX); ddBltFx.dwFillColor = 0 ; lpDDSPrimary -> Blt (NULL, NULL, NULL, DDBLT_WAIT | DDBLT_COLORFILL, & ddBltFx); lpDDSBuffer-> Blt (NULL, NULL, NULL, DDBLT_WAIT | DDBLT_COLORFILL, & ddBltFx); lpDDSMap-> Blt (NULL, NULL, NULL, DDBLT_WAIT | DDBLT_COLORFILL, & ddBltFx); lpDDSMouse-> Blt (NULL, NULL, NULL, DDBLT_WAIT | DDBLT_COLORFILL, & ddBltFx); DDReLoadBitmap (lpDDSMap, "inn.BMP"); DDReLoadBitmap (lpDDSMouse, "mouse.BMP"); MakeRect (0,0,640,480 ); LPDDSPRIMARY-> BLTFAST (0, 0, LPDDSMAP, & R, NOKEY); DDSETCOLORKEY (LPDDSMAP, RGB (0,255,0)); DDSETCOLORKEY (LPDDSMOUSE, RGB (0, 255, 0));

} // Take a function of the rectangle, also describe the Void MakeRect (int ost, int top, int right, int bottom) {r.Bottom = Bottom; r.L.L.Right; R.TOP = TOP;} // Draw line function void line (int X1, int y1, int x2, int y2, hdc DC) {MoveToex (DC, X1, Y1, 0); // Get the starting coordinate of the draw line Lineto (DC, X2, Y2); // Get the end of the drawing line} // Output function Void Print on the screen (Char Text [255], INT X, INT Y) {HDC HDC; LPDDSBuffer-> Getdc & HDC); // The background page gives a handle setBkMode (HDC, Transparent); // Setting the display mode setTextColor (HDC, RGB (0,255,255))); // Output text color Textout (HDC, X, Y, Text, Strlen (TEXT) ); // Output text LPDDSBuffer-> ReleaseDC (HDC); // Release the function of the handle} // Early transition and background pages to exchange the two pages void flip () {HRESULT DDRVAL; DDRVAL = LPDDSPRIMARY-> FLIP (NULL, DDFLIP_WAIT); if (DDRVAL == DDERR_SURFACELOST) RESTORE ();} // The function released by the pointer, the pointer is released after using the HRESULT RESTORE (VOID) {HRESULT DDRVAL; DDRVAL = LPDDSPRIMARY-> restore (); DDRVAL = lpddsbuffer-> restore (); ddrval = lpddsmouse-> restore (); ddrval = lpddsmap-> rest (); init (); return ddrval;} There is also a most critical function in public class is refresh () Void refresh () {// Stip the background map to the buffer page, so that it is displayed MakeRect (0,0,640,480); lpddsbuffer-> BL TFAST (0, 0, LPDDSMAP, & R, NOKEY); // The mouse movement is also written in the refresh is to leave Point Curpos; getCursorpos (& Curpos); for (int i = 0 i <2; i ) {cursorx [i] = cursorx [i 1]; cursory [i] = cursory [i 1]; CURSORX [2] = curpos.x; cursory [2] = curpos.y; LPDDSBuffer-> Bltfast (CURSORX [1], Cursory [1], LPDDSMOUSE, NULL, SRCKEY);} // ok, the method of clinging, there is a sign, when this flag is 1, IF (enable = = 1) Drawp.blt (Drawp.x1, Drawp.y1, Drawp.x2, Drawp.y2); // Call the mouse to move the rectangle method, there is also a flag, when the flag is 2, call IF (Enable == 2) Drawp.Rectanglep (X, Y, Curpos.x, Curpos.y);

/ / Assign a string to call these string char * text = "Left mousedown: Click the left mouse button to determine the cropped vertex coordinate"; char * text1 = "Left Mousemove: Drag the mouse can choose the cropping area" CHAR * text2 = "Left Mouseup: Remove the left mouse button to get another vertex coordinate"; char * text3 = "Space: Press the space bar to post the cropping area"; char * text4 = "The world is not available, Medi people diesel! " Print (Text2, 10, 50); Print (TEXT3, 10, 70); Print (Text4, 400, 450); // Turn the page, the home page and the buffer page alternately display flip ();}, this is the implementation of all functions And the principle, go back to the message loop mechanism to see. Main.h: #if! Defined (course_design)

#define course_design

#include #include #include #include "DDUTIL.H" #include "draw.h"

EXTERN HDC HDC; Extern Rect R; Extern DWORD SRCKEY; Extern DWORD NOKEY; Extern Point Curpos; Extern Int Enable; Extern Int X; Extern Int Y; Extern Int x3; Extern Int Y3; EXTERN INT Y3;

extern LPDIRECTDRAW7 lpDD; extern LPDIRECTDRAWSURFACE7 lpDDSPrimary; extern LPDIRECTDRAWSURFACE7 lpDDSBuffer; extern LPDIRECTDRAWSURFACE7 lpDDSMap; extern LPDIRECTDRAWSURFACE7 lpDDSMouse; extern draw drawp;

Void init (); void MakeRect (int tent, int intom); void refresh (); void flip (); void print (char text [255), int x, int y); Void Line INT X1, INT Y1, INT X2, INT Y2, HDC DC); HRESULT RESTORE (Void);

# endifmain.cpp # include "main.h"

Main.cppBool INITWINDOW (Hinstance Hinstance, Int ncmdshow); LRESULT CALLBACK WINPROC (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM);

HWND HWnd; Rect R; HDC HDC; Point Curpos; Int Enable; Int x; Int Y; INT X3; INT Y3;

LPDIRECTDRAWSURFACE7 LPDDSPRIMARY; LPDIRECTDRAWSURFACE7 LPDDSBUFFER; LPDIRECTDRAWSURFACE7 LPDDSMAP; LPDIRECTDRAWSURFACE7 LPDDSMOUSE

DWORD SrcKey = DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT; DWORD NoKey = DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT; draw drawp; int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,

INT ncmdshow) {msg msg; initwindow (hinstance, ncmdshow);

Init (); // Initialize while (1) {if (PEEKMESSAG, NULL, 0, 0, PM_Remove) {if (msg.Message == WM_QUIT) Break; TranslateMessage (& MSG); DispatchMessage (& MSG); Else {refresh (); // message loop, complete refresh feature, the specific function has been introduced in front of this function}}} return msg.wparam;}

static BOOL InitWindow (HINSTANCE hInstance, int nCmdShow) {WNDCLASS wc; wc.style = NULL; wc.lpfnWndProc = (WNDPROC) WinProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = NULL; wc.hbrBackground = NULL; wc.lpszMenuName = NULL; wc.lpszClassName = "Course Design"; RegisterClass (& wc); hwnd = CreateWindow ( "Course Design", "My Course

Design ", WS_POPUP | WS_MAXIMIZE, 0,0, GetSystemMetrics (SM_CXSCREEN), GetSystemMetrics (SM_CYSCREEN), NULL, NULL, hInstance, NULL); if (hwnd) return FALSE; ShowWindow (hwnd, nCmdShow); UpdateWindow (hwnd);! Return True;}

LRESULT CALLBACK WinProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {switch (message) {case WM_SETCURSOR: SetCursor (NULL); return 0; case WM_KEYDOWN: switch (wParam) {case VK_ESCAPE: // press the ESC key to exit Program MessageBox (hwnd, "quit!", "Keyboard", mb_ok); postquitMessage (0); break; case vk_space: // Press the space bar to get two coordinates, and set the flag to 1 Drawp.x1 = x; DRAWP .y1 = y; Drawp.x2 = x3; DRAWP.Y2 = Y3; Enable = 1; return 0;} return 0; Case WM_LBUTTONDOWN: // Press the left mouse button to get a point of coordinate x = (int) loword ( LPARAM); Y = (int) HiWord (LPARAM); Break; Case WM_LBUTTONUP: // Release the left mouse button to get the coordinates of a point. And draw this rectangle x3 = (int) loword (lparam); Y3 = (int) HiWord (LPARAM); Drawp.Rectanglep (x, y, x3, y3); Break; Case WM_MOUSEMOVE: // Mouse Movement Setting Sign For 2 enable = 2; Break; Case WM_DESTROY: PostquitMessage (0); Return 0;} Return DefWindowProc (HWND, Message, WPARAM, LPARAM);} I will put it out, I think as long as I look at the comment Understand it. Summary, the ocean sprinkled so many things, he all felt messy, using a mouthful: "Can't open the direction", so it is necessary to summarize, in general, we have made a picture cut, then this It is a very simple program that mainly uses the two aspects of Windows message loops and Direct programming, and the core of implementing the program is that it is only used by Direct programming (BLTFast ()) function, that is, map The size and range of changes have differentiated cuts. (Procedure -> Cut -> Direct -> Bltfast), this is the task of core completion, and other other use is convenient or other. I hope that I will tell myself to say it. I hope that everyone can understand what I am talking about, and I don't have a lot of strings. To be honest, this is the first time I wrote so many technical articles. There is no experience. I feel that I have been writing more messy and seems to have no focus. I can only make trying to make critical principles and knowledge. Source code as much as possible, let everyone understand, if there is anything I don't understand and hope that I will leave a message to the mailbox, I have time, I will try to pay for everyone to reply. In fact, DIERCT programming is a very interesting part. This contains a lot of things, and even play games, but I am just a semi-solving, there is still a lot of things I haven't worried, and I also hope that I have my own in this area. The friends who have experienced and experienced more than I exchanged, and they will progress together.

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

New Post(0)