程式設計作業

哲宇提供

/* Lesson 2 - Understanding Messages and Events

* Pravin Paratey ()

**/



#include <windows.h>



HWND hwndMain;//Main window handle



// Callback function

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);



// Windows entry point

int WINAPI

WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)

{

MSG msg; // MSG structure to store messages

WNDCLASSEX wcx; // WINDOW class information



// Initialize the struct to zero

ZeroMemory(&wcx,sizeof(WNDCLASSEX));

wcx.cbSize = sizeof(WNDCLASSEX); // Window size. Must always be sizeof(WNDCLASSEX)

wcx.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ; // Class styles

wcx.lpfnWndProc = (WNDPROC)MainWndProc; // Pointer to the callback procedure

wcx.cbClsExtra = 0; // Extra byte to allocate following the wndclassex structure

wcx.cbWndExtra = 0; // Extra byte to allocate following an instance of the structure

wcx.hInstance = hInstance; // Instance of the application

wcx.hIcon = NULL; // Class Icon

wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // Class Cursor

wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW); // Background brush

wcx.lpszMenuName = NULL; // Menu resource

wcx.lpszClassName = "Lesson2"; // Name of this class

wcx.hIconSm = NULL; // Small icon for this class



// Register this window class with MS-Windows

if (!RegisterClassEx(&wcx))

return 0;



// Create the window

hwndMain = CreateWindowEx(0, //Extended window style

"Lesson2", // Window class name

"Lesson 2 - A simple win32 application", // Window title

WS_OVERLAPPEDWINDOW, // Window style

CW_USEDEFAULT,CW_USEDEFAULT, // (x,y) pos of the window

CW_USEDEFAULT,CW_USEDEFAULT, // Width and height of the window

HWND_DESKTOP, // HWND of the parent window (can be null also)

NULL, // Handle to menu

hInstance, // Handle to application instance

NULL); // Pointer to window creation data



// Check if window creation was successful

if (!hwndMain)

return 0;



// Make the window visible

ShowWindow(hwndMain,SW_SHOW);



// Process messages coming to this window

while (GetMessage(&msg,NULL,0,0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

// return value to the system

return msg.wParam;

}



// Callback procedure

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)

{

HDChMyDC;

PAINTSTRUCTps;

charszText[] = "畫出圓形及其半徑";

intnLen = sizeof(szText)-1;



int num = 360;//描繪的點數

int r = 100;//半徑

int i,x,y;





switch(msg)

{

case WM_PAINT:

hMyDC=BeginPaint(hwnd, &ps);

//當BeginPaint()函式成功執行完畢,就會傳回DC的識別代碼,因此我們必須宣告hMyDC 變數用以儲存這個回傳的DC識別代碼。



TextOut(hMyDC,150,100,szText,nLen);



//(1)畫線

// MoveToEx(hMyDC, 100, 20, NULL);

// LineTo(hMyDC, 100, 122);



//(2)畫三角形

// MoveToEx(hMyDC, 60, 20, NULL);

// LineTo(hMyDC, 60, 122);

// LineTo(hMyDC, 264, 122);

// LineTo(hMyDC, 264, 20);

// LineTo(hMyDC, 60, 20);





//(3)畫圓

SetPixel(hMyDC,200,150,RGB(0,255,0)); //畫圓心



for (i = 0;i < num; i++){

x= (200)+i; //利用x=cos(i),y=sin(i) 的公式畫圓

y= (150)+(100)*sin(-i*3.14159/180*2);

SetPixel(hMyDC,x,y,RGB(0,255,0));



x= (200)-i; //利用x=cos(i),y=sin(i) 的公式畫圓

y= (150)+(100)*sin(i*3.14159/180*2);

SetPixel(hMyDC,x,y,RGB(0,255,0));

}

MoveToEx(hMyDC,(400/2),(300/2),NULL);//線段的起點

LineTo(hMyDC,600,150);//畫線到(x,y)座標

MoveToEx(hMyDC,(400/2),(300/2),NULL);//線段的起點

LineTo(hMyDC,200,0);//畫線到(x,y)座標

MoveToEx(hMyDC,(400/2),(300/2),NULL);//線段的起點

LineTo(hMyDC,0,150);//畫線到(x,y)座標

MoveToEx(hMyDC,(400/2),(300/2),NULL);//線段的起點

LineTo(hMyDC,200,600);//畫線到(x,y)座標

EndPaint(hwnd, &ps);



break;

case WM_CLOSE:

DestroyWindow(hwnd);

break;

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(hwnd, msg, wParam, lParam);

}

}



int nTopXY(UINT wnd_XY, UINT wnd_Dim)

{

int resl;

resl=((wnd_Dim/2)-(wnd_XY/2));

return resl;

}















哲宇提供2

/* Lesson 2 - Understanding Messages and Events

* Pravin Paratey ()

**/



#include <windows.h>



HWND hwndMain;//Main window handle



// Callback function

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);



// Windows entry point

int WINAPI

WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)

{

MSG msg; // MSG structure to store messages

WNDCLASSEX wcx; // WINDOW class information



// Initialize the struct to zero

ZeroMemory(&wcx,sizeof(WNDCLASSEX));

wcx.cbSize = sizeof(WNDCLASSEX); // Window size. Must always be sizeof(WNDCLASSEX)

wcx.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ; // Class styles

wcx.lpfnWndProc = (WNDPROC)MainWndProc; // Pointer to the callback procedure

wcx.cbClsExtra = 0; // Extra byte to allocate following the wndclassex structure

wcx.cbWndExtra = 0; // Extra byte to allocate following an instance of the structure

wcx.hInstance = hInstance; // Instance of the application

wcx.hIcon = NULL; // Class Icon

wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // Class Cursor

wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW); // Background brush

wcx.lpszMenuName = NULL; // Menu resource

wcx.lpszClassName = "Lesson3"; // Name of this class

wcx.hIconSm = NULL; // Small icon for this class



// Register this window class with MS-Windows

if (!RegisterClassEx(&wcx))

return 0;



// Create the window

hwndMain = CreateWindowEx(0, //Extended window style

"Lesson3", // Window class name

"Lesson 3 - A simple win32 application: draw sin function", // Window title

WS_OVERLAPPEDWINDOW, // Window style

CW_USEDEFAULT,CW_USEDEFAULT, // (x,y) pos of the window

CW_USEDEFAULT,CW_USEDEFAULT, // Width and height of the window

HWND_DESKTOP, // HWND of the parent window (can be null also)

NULL, // Handle to menu

hInstance, // Handle to application instance

NULL); // Pointer to window creation data



// Check if window creation was successful

if (!hwndMain)

return 0;



// Make the window visible

ShowWindow(hwndMain,SW_SHOW);



// Process messages coming to this window

while (GetMessage(&msg,NULL,0,0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

// return value to the system

return msg.wParam;

}



// Callback procedure

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)

{

HDChMyDC;

PAINTSTRUCTps;

double pi=3.14159;

charszText[] = "圖為60Sin(x)圖形,週期2Pi";

charszText2[] = "X";

charszText3[] = "Y";

intnLen = sizeof(szText)-1;

intnLen2 = sizeof(szText2)-1;

intnLen3 = sizeof(szText3)-1;



int num = 360;//描繪的點數

int i,x,y;

switch (msg)

{

case WM_PAINT:

hMyDC=BeginPaint(hwnd, &ps);

//當BeginPaint()函式成功執行完畢,就會傳回DC的識別代碼,因此我們必須宣告hMyDC 變數用以儲存這個回傳的DC識別代碼。



TextOut(hMyDC,100,270,szText,nLen);

TextOut(hMyDC,num+110,250,szText2,nLen2);

TextOut(hMyDC,100,35,szText3,nLen3);



// 畫Y軸

MoveToEx(hMyDC, 100, 250, NULL);

LineTo(hMyDC, 100, 50);







//(3)畫Sin

SetPixel(hMyDC,100,250,RGB(0,0,255)); //畫原點



for (i = 0;i < num; i++){

x= (100)+(i);

y= (250)-(60*sin(i*pi/180)); //弳度要換成角度

SetPixel(hMyDC,x,y,RGB(255,0,0));

}





MoveToEx(hMyDC,(100),(250),NULL); //畫X軸

LineTo(hMyDC,i+100,250);

EndPaint(hwnd, &ps);



break;

case WM_DESTROY:

PostQuitMessage(0);

break;



default:

// Call the default window handler

return DefWindowProc(hwnd,msg,wParam,lParam);

}

return 0;

}













野田提供

/* Lesson 2 - Understanding Messages and Events

* Pravin Paratey ()

**/



#include <windows.h>

#include <math.h>





HWND hwndMain;//Main window handle



// Callback function

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);



// Windows entry point

int WINAPI

WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)

{

MSG msg; // MSG structure to store messages

WNDCLASSEX wcx; // WINDOW class information



// Initialize the struct to zero

ZeroMemory(&wcx,sizeof(WNDCLASSEX));

wcx.cbSize = sizeof(WNDCLASSEX); // Window size. Must always be sizeof(WNDCLASSEX)

wcx.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ; // Class styles

wcx.lpfnWndProc = (WNDPROC)MainWndProc; // Pointer to the callback procedure

wcx.cbClsExtra = 0; // Extra byte to allocate following the wndclassex structure

wcx.cbWndExtra = 0; // Extra byte to allocate following an instance of the structure

wcx.hInstance = hInstance; // Instance of the application

wcx.hIcon = NULL; // Class Icon

wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // Class Cursor

wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW); // Background brush

wcx.lpszMenuName = NULL; // Menu resource

wcx.lpszClassName = "Lesson3"; // Name of this class

wcx.hIconSm = NULL; // Small icon for this class



// Register this window class with MS-Windows

if (!RegisterClassEx(&wcx))

return 0;



// Create the window

hwndMain = CreateWindowEx(0, //Extended window style

"Lesson3", // Window class name

"Lesson 3 - A simple win32 application: draw circle", // Window title

WS_OVERLAPPEDWINDOW, // Window style

CW_USEDEFAULT,CW_USEDEFAULT, // (x,y) pos of the window

CW_USEDEFAULT,CW_USEDEFAULT, // Width and height of the window

HWND_DESKTOP, // HWND of the parent window (can be null also)

NULL, // Handle to menu

hInstance, // Handle to application instance

NULL); // Pointer to window creation data



// Check if window creation was successful

if (!hwndMain)

return 0;



// Make the window visible

ShowWindow(hwndMain,SW_SHOW);



// Process messages coming to this window

while (GetMessage(&msg,NULL,0,0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

// return value to the system

return msg.wParam;

}



// Callback procedure

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)

{

HDChMyDC;

PAINTSTRUCTps;

charszText[] = "y=r*sin(x)";

intnLen = sizeof(szText)-1;



int num = 3600;//描繪的點數

int r = 100;//半徑

int i,x,y;

switch (msg)

{

case WM_PAINT:

hMyDC=BeginPaint(hwnd, &ps);

//睛^eginPaint()函式成功執行完畢,就會傳回DC的識別代碼,因此我們必須宣告hMyDC 變數用以儲存這個回傳的DC識別代碼。



TextOut(hMyDC,300,50,szText,nLen);



//(1)畫線

// MoveToEx(hMyDC, 60, 20, NULL);

// LineTo(hMyDC, 264, 122);



//(2)畫三角形

// MoveToEx(hMyDC, 60, 20, NULL);

// LineTo(hMyDC, 60, 122);

// LineTo(hMyDC, 264, 122);

// LineTo(hMyDC, 60, 20);





//(3)畫?E

SetPixel(hMyDC,300,100,RGB(0,0,255));



for (i = 0;i < num; i++){

x= (100)+(i/6);

y= (200)-r*(sin(0.01*i));

SetPixel(hMyDC,x,y,RGB(255,0,0));

}



MoveToEx(hMyDC,(100),(50),NULL);

LineTo(hMyDC,100,350);

MoveToEx(hMyDC,(50),(200),NULL);

LineTo(hMyDC,800,200);



EndPaint(hwnd, &ps);



break;

case WM_DESTROY:

PostQuitMessage(0);

break;



default:

// Call the default window handler

return DefWindowProc(hwnd,msg,wParam,lParam);

}

return 0;

}