2011-12-17 31 views
4

私は、Visual Studio VC++コンソールアプリケーションを使用してレーザービームを使用して描画するopencvアプリケーションを作成しています。私はデスクトップ上に線を描きたい。 私は描画関数はGDI32.dllで利用できるが、GDI32.dllを私のVCコードと統合する方法を混乱させることを知っています。あなたは良い解決策を提案できますか?Visual C++を使用してデスクトップに描画

+1

これは、OpenCVよりもwin32とgdiに関連しています。私はそれを適切にタグ付けすることをお勧めします。 VC++でGDIを使う方法を正確に示すGDIのチュートリアルが数多くあります。 OpenCVプロジェクトでは、適切なライブラリパス、ヘッダー、および適切なGDIライブラリをリンクして機能させるだけです。本当に魔法はありません。 – karlphillip

+0

が変更されましたありがとう! –

+0

私はWin32 APiを使用していません。私のコードはネイティブc、C++にありますGDIライブラリを使用することはできません! –

答えて

9

次のコードは、デスクトップに青い四角形を描いています。

#include <iostream> 
#include <Windows.h> 

int main() {  

    /* hide console window */ 
    ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false); 

    /* Calling GetDC with argument 0 retrieves the desktop's DC */ 
    HDC hDC_Desktop = GetDC(0); 

    /* Draw a simple blue rectangle on the desktop */ 
    RECT rect = { 20, 20, 200, 200 }; 
    HBRUSH blueBrush=CreateSolidBrush(RGB(0,0,255)); 
    FillRect(hDC_Desktop, &rect, blueBrush); 

    Sleep(10); 
    return 0; 
} 

ちょうど楽しいです。デスクトップに直接描かれたマンデルブロフラクタル。

#define MAGNITUDE_CUTOFF 100 
#define NUMCOLOURS 256 
#define WIDTH 640 
#define HEIGHT 200 
#define UP 72 
#define DOWN 80 
#define LEFT 75 
#define RIGHT 77 
#define SPACE 32 
#define ENTER 13 
#define ESCAPE 27 
#define TAB 9 
#define INSERT 82 

#include <stdio.h> 
#include <time.h> 
#include <windows.h> 
#include <iostream> 
using namespace std; 

int col(int x, int y); 
void fract(void); 

char op; 
int ch,max_iterations; 
double xmin = -2.10, xmax = 0.85, ymin = -1.5 , ymax = 1.5; 
double width_fact, height_fact; 


int main(){ 

    COLORREF color = RGB(255,0,0); // COLORREF to hold the color info 


    SetConsoleTitle("Pixel In Console!"); // Set text of the console so you can find the window 
    HWND hwnd = FindWindow(NULL, "Pixel In Console?"); // Get the HWND 
    HDC hdc = GetDC(hwnd); // Get the DC from that HWND 

    width_fact = (xmax-xmin)/WIDTH; 
    height_fact = (ymax-ymin)/HEIGHT; 


    for(int x = 0 ; x < 640 ; x++){ 
     for (int y = 0;y < 480; y++){ 

     int blue = (col(x,y) & 0x0f) << 4; 
     int green = (col(x,y) & 0xf0) << 0; 
     int red = (col(x,y) & 0xf00) >> 4; 
     SetPixel(hdc, x,y, RGB(red,green,blue)); 

     } 
    } 


    system("pause"); 

    ReleaseDC(hwnd, hdc); // Release the DC 
    DeleteDC(hdc); // Delete the DC 
    return(0); 
} 

void fract(){ 
    int x,y,icount=0; 
    width_fact = (xmax-xmin)/WIDTH; 
    height_fact = (ymax-ymin)/HEIGHT; 

    for (y=0;y<HEIGHT;y++){ 
     for (x=0;x<WIDTH;x++){ 
      // setcolor(col(x,y)); 
      // gotoxy(x+3,y+3);printf("Û"); 

     } 

    } 
    //setcolor(15); 
} 


int col(int x, int y){ 
    int n,icount=0; 
    float p,q,r,i,prev_r,prev_i; 

    p= (((float)x) * width_fact) + (float)xmin; 
    q= (((float)y) * height_fact) +(float)ymin; 

    prev_i = 0; 
    prev_r = 0; 

    for (n=0; n <= NUMCOLOURS; n++){ 
     r = (prev_r * prev_r) - (prev_i * prev_i) +p; 
     i = 2 * (prev_r * prev_i) +q; 

     if ((r*r + i*i) < MAGNITUDE_CUTOFF){ 
      prev_r = r; 
      prev_i = i; 
     } 
     else { 
      return n; 
     } 
    } 
    return n; 
} 
+5

そこでは「Sleep(10)」関数呼び出しが何をしていますか?また、それは本当にコンソールウィンドウを隠す/表示する標準的な方法ではありません... –

+2

情報ありがとうございます。出来た!!!! –

関連する問題