c++ - 将 win32 控制台应用程序切换到图形模式

标签 c++ visual-c++

我想将我的 win32 控制台应用程序切换到图形模式以使用 SetPixel 函数绘制线条:

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
          //code to switch to graphics mode

    return 0;
}

请多多指教:)

最佳答案

这是一个很好的 SetPixel() 示例。

创建win32应用工程,粘贴代码并编译

//header files to include
#include<windows.h>
#include<stdlib.h>
#include<time.h>

//application title
#define APPTITLE "Hello World"

//function prototypes (forward declarations)
BOOL InitInstance(HINSTANCE, int);
ATOM MyRegisterClass(HINSTANCE);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);

//the window event callback function
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    char *szHello = "SetPixel";
    RECT rt;
    int x=0, y=0, n=0;
    COLORREF c;
    int j;

    switch (message)
    {
    case WM_PAINT:
        //get the dimensions of the window
        GetClientRect(hWnd, &rt);

        //start drawing on devicce context
        hdc = BeginPaint (hWnd, &ps);

        //draw some text
        DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
        j = (rand()*100);

        c = RGB(0, 0, 0);

        while( x<25000)
        {

            SetPixel(hdc,  rand()%400,  rand()%400, rand()%255);
            x++; 
        }

        //stop drawing
        EndPaint(hWnd, &ps);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    //create the window class structure
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);

    //fill the struct with info
    wc.style                 = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc           = (WNDPROC)WinProc;
    wc.cbClsExtra            = 0;
    wc.cbWndExtra            = 0;
    wc.hInstance             = hInstance;
    wc.hIcon                 = NULL;
    wc.hCursor               = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground         = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName          = NULL;
    wc.lpszClassName         = APPTITLE;
    wc.hIconSm               = NULL;

    //set up the window with the class info
    return RegisterClassEx(&wc);
}

//helper function to create the window and refresh it
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;

    //create a new window
    hWnd = CreateWindow(
        APPTITLE,           //window class
        APPTITLE,       //title bar
        WS_OVERLAPPEDWINDOW,    //window style
        CW_USEDEFAULT,      //x position of window
        CW_USEDEFAULT,      //y position of window
        400,                //width of the window
        400,                //height of the window
        NULL,               //parent window
        NULL,           //menu
        hInstance,          //application instance
        NULL);              //window parameters


    //was there an error creating the window?
    if(!hWnd)
        return FALSE;

    //display the window
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return TRUE;
}

//entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR            lpCmdLine,
                   int              nCmdShow)
{
    //declare variables
    MSG msg;

    //register the class
    MyRegisterClass(hInstance);

    //initialize application
    if(!InitInstance (hInstance, nCmdShow))
        return FALSE;

    //set random number seed
    srand(time(NULL));

    //main message loop
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

关于c++ - 将 win32 控制台应用程序切换到图形模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8827044/

相关文章:

c++ - 编译的 C 可执行文件被 Windows Defender 检测为病毒

c++ - 在 Win 7 中从远程进程获取 PEB

c++ - 为什么我得到 "recursive type or function dependency context too complex"?

python - 错误 "Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat)"

c++ - 如何在非 MFC 项目中使用 TRACE 宏?

C++ 创建文本适合编辑框

c++ - 在 C++ 中将 UID 设置为 root 以外的用户

c++ - 为什么 QGraphicsItem::scenePos() 不断返回 (0,0)

c++ - QOpenGLWidget 的makeCurrent 不起作用?

c++ - 使用数组 : It's crashing on run-time 的单链表