2012-01-11 201 views
0

我想我的Win32控制檯應用程序切換到圖形模式下使用SetPixel函數來畫線:開關Win32控制檯應用程序,以圖形方式

#include "stdafx.h" 

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

    return 0; 
} 

請指點:)

+0

沒有「控制檯模式」與「圖形模式」。您可以像控制其他應用程序一樣在控制檯應用程序中使用「CreateWindow」。 – tenfour 2012-01-11 21:41:18

+0

沒有「切換到圖形模式」這樣的事情。您必須重寫該程序才能用作Windows應用程序。 – 2012-01-11 21:42:31

+1

@tenfour,CreateWindow只是開始。然後你必須創建一個消息泵並對消息進行解碼。您必須更改設置,以便在啓動時不會打開控制檯。等等,重新開始更容易。 – 2012-01-11 21:44:28

回答

0

您可以切換到「graphich模式「意味着使用項目設置的Windows窗體應用程序模式。但你必須到主功能更改爲winMain

項目設置>

鏈接器>系統>子系統>的Windows(/子系統:WINDOWS)

C/C++> PREPROCESSOR>預處理器定義> WIN32,_DEBUG; _WINDOWS;%(PreprocessorDefinitions)

,這是你在這種情況下函數如何應該是:

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int nCmdShow) 
{ 
     ///.... 
} 
1

這裏有個很好的例子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; 
} 
相關問題