⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vgaout.cpp

📁 AppWizard has created this VGAout application for you.
💻 CPP
字号:
// VGAout.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100
#define OBJ_RECTANGLE       0
#define OBJ_ELLIPSE         1
#define OBJ_ROUNDRECT       2
#define OBJ_CHORD           3
#define OBJ_PIE             4

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text
HANDLE hButton; //the button to start the VGA out display
BOOL bVGA;
HDC hVGA;
DWORD dwVGAThreadID;
HANDLE hVGAThread;
TCHAR szHello[MAX_LOADSTRING];

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
DWORD WINAPI VGAThreadProc(  LPVOID lpParameter);

int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_VGAOUT, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_VGAOUT);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASS wc;

    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC) WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = 0;
    wc.hCursor = 0;
    wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = 0;
    wc.lpszClassName = szWindowClass;

	return RegisterClass(&wc);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE | WS_CAPTION | WS_SYSMENU,
      0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

DWORD WINAPI VGAThreadProc(LPVOID lpParameter)
{
    int x1,y1,x2,y2,x3,y3,x4,y4,r,g,b,nObject;
    RECT rect, textrect;
    HBRUSH hBrush;
    LPTSTR DebugMsg;

    rect.top = 0;
    rect.left = 0;
    rect.right = 1024;
    rect.bottom = 768;
    if(hBrush = SelectObject(hVGA,CreateSolidBrush(RGB(0,0,0))))
    {
        Rectangle(hVGA, -2, -2, 1026, 770);
        DeleteObject(SelectObject(hVGA,hBrush));
    }

    while (bVGA) 
    {   

        // avoid divide by zero errors when the window is small.
        if ( rect.right== 0) rect.right++;
        if ( rect.bottom== 0) rect.bottom++;

        r = (rand() % 25) * 10;
        g = (rand() % 25) * 10;
        b = (rand() % 25) *10;

        if(hBrush = SelectObject(hVGA,CreateSolidBrush(RGB(r,g,b))))
        {
            x1 = rand() % rect.right;
            y1 = rand() % rect.bottom;
            x2 = rand() % rect.right;
            y2 = rand() % rect.bottom;
            x3 = rand() % rect.right;
            y3 = rand() % rect.bottom;
            x4 = rand() % rect.right;
            y4 = rand() % rect.bottom;


            nObject = rand() % 3;

            switch(nObject)
            {
                default:
                case OBJ_RECTANGLE:
                    Rectangle(hVGA,x1,y1,x2,y2);
                    break;

                case OBJ_ELLIPSE:
                    Ellipse(hVGA,x1,y1,x2,y2);
                    break;

                case OBJ_ROUNDRECT:
                    RoundRect(hVGA,x1,y1,x2,y2,x3,y3);
                    break;

            }

            DeleteObject(SelectObject(hVGA,hBrush));
        } //end if (hBrush) = ...
        textrect.top = 100;
        textrect.left = 100;
        textrect.bottom = 300;
        textrect.right = 300;
        DrawText(hVGA, szHello, _tcslen(szHello), &textrect, DT_CENTER);
        Sleep(1000);
    } //end while(bVGA)
    ExitThread(0x0000);
    return TRUE;
}


//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
    RECT rect;
    int x1,y1,x2,y2,x3,y3,x4,y4,r,g,b,nObject;
    DEVMODE InitData;
    int StartVal;
    HBRUSH hBrush;




	switch (message) 
	{
        case WM_CREATE:
            hButton = CreateWindow(TEXT("BUTTON"), TEXT("VGA!!!"),
                            WS_VISIBLE | WS_CHILD,
                            25, 25, 575, 121, hWnd,
                            NULL, hInst, NULL); 
            bVGA = FALSE;
            hVGAThread = NULL;
            LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
            return TRUE;
            //break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...
			// RECT rt;
			// GetClientRect(hWnd, &rt);
			GetClientRect(hWnd, &rect);
			//DrawText(hdc, szHello, _tcslen(szHello), &rt, DT_CENTER);   
			DrawText(hdc, szHello, _tcslen(szHello), &rect, DT_CENTER);   
            if (bVGA) 
            {                   
                // avoid divide by zero errors when the window is small.
                if ( rect.right== 0) rect.right++;
                if ( rect.bottom== 0) rect.bottom++;

                r = rand() % 255;
                g = rand() % 255;
                b = rand() % 255;

                if(hBrush = SelectObject(hVGA,CreateSolidBrush(RGB(r,g,b))))
                {
                    x1 = rand() % rect.right;
                    y1 = rand() % rect.bottom;
                    x2 = rand() % rect.right;
                    y2 = rand() % rect.bottom;
                    x3 = rand() % rect.right;
                    y3 = rand() % rect.bottom;
                    x4 = rand() % rect.right;
                    y4 = rand() % rect.bottom;


                    nObject = rand() % 3;

                    switch(nObject)
                    {
                        default:
                        case OBJ_RECTANGLE:
                            Rectangle(hVGA,x1,y1,x2,y2);
                            break;

                        case OBJ_ELLIPSE:
                            Ellipse(hVGA,x1,y1,x2,y2);
                            break;

                        case OBJ_ROUNDRECT:
                            RoundRect(hVGA,x1,y1,x2,y2,x3,y3);
                            break;

                    }

                    DeleteObject(SelectObject(hVGA,hBrush));
                } //end if (hBrush) = ...
            } //end if(bVGA)*/
			EndPaint(hWnd, &ps);
			break;
        case WM_COMMAND:
            if ((HWND)lParam == hButton)
            { 
                if (bVGA)
                {
                    bVGA = FALSE; //first set bVGA to FALSE so thread can exit!!
                    DeleteDC(hVGA);
                    bVGA = FALSE;
                    SetWindowText(hButton, TEXT("VGA!!!")); 
                    return TRUE;
                }                
                hVGA = CreateDC(TEXT("skvout2.dll"), NULL, NULL, &InitData);
                if (!hVGA)
                {
                    MessageBox(hWnd, TEXT("Unable to initialize VGA output device."), TEXT("ERROR:"), MB_OK);
                    return TRUE;
                }
                bVGA = TRUE; //we are now drawing to VGA output!
                StartVal = 0;
                hVGAThread = CreateThread(NULL, 0, &VGAThreadProc, &StartVal, 0, &dwVGAThreadID);
                SetWindowText(hButton, TEXT("End VGA"));  
                SendMessage(hWnd, WM_PAINT, 0, 0);
                //SendMessage(hWnd, WM_COMMAND, 0, (LPARAM));                       
                return TRUE;
            }
            break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -