寫的很差...........
一開始也沒討論好...........
一堆亂七八糟的東西都出現了.........
但是我很愛這代表我們已經開始可以寫api的程式了........
在此貼上主程式..................
也正式宣告我們已經會api了XD.................
// 小畫家.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR 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_MY, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY);
// 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)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MY);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH) GetStockObject (GRAY_BRUSH) ;
wcex.lpszMenuName = (LPCSTR)IDC_MY;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// 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_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hwnd)
{
return FALSE;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
void GetLargestDisplayMode (int * pcxBitmap, int * pcyBitmap)
{
DEVMODE devmode ;
int iModeNum = 0 ;
* pcxBitmap = 0 ;
* pcyBitmap = 0 ;
ZeroMemory (&devmode, sizeof (DEVMODE)) ;
devmode.dmSize = sizeof (DEVMODE) ;
while (EnumDisplaySettings (NULL, iModeNum++, &devmode))
{
* pcxBitmap = max (* pcxBitmap, (int) devmode.dmPelsWidth) ;
* pcyBitmap = max (* pcyBitmap, (int) devmode.dmPelsHeight) ;
}
}
void DrawFocus(HDC hdc,int cxBlock,int cyBlock, int xb,int yb)
{
MoveToEx (hdc, xb * cxBlock, yb * cyBlock, NULL) ;
LineTo (hdc, xb * cxBlock, (yb+1) * cyBlock) ;
MoveToEx (hdc, xb * cxBlock, (yb+1) * cyBlock, NULL) ;
LineTo (hdc, (xb+1) * cxBlock, (yb+1) * cyBlock) ;
MoveToEx (hdc, (xb+1) * cxBlock, (yb+1) * cyBlock, NULL) ;
LineTo (hdc, (xb+1) * cxBlock, yb * cyBlock) ;
MoveToEx (hdc, (xb+1) * cxBlock, yb * cyBlock, NULL) ;
LineTo (hdc, xb * cxBlock, yb * cyBlock) ;
}
//畫圖功能函式
void Draw(HDC hdc,POINT apt,POINT bpt,POINT cpt,INT iState)
{
switch (iState)
{
case 0: //選擇任意範圍
break;
case 1: //橡皮擦
break;
case 2: //吸管(選取顏色)
break;
case 3: //鉛筆
break;
case 4: //噴槍
break;
case 5: //直線
MoveToEx (hdc, bpt.x , bpt.y , NULL) ;
LineTo (hdc, apt.x , apt.y ) ;
break;
case 6: //矩形
Rectangle(hdc, bpt.x , bpt.y, apt.x , apt.y );
break;
case 7: //橢圓
Ellipse(hdc, bpt.x , bpt.y, apt.x , apt.y );
break;
case 8: //選擇
break;
case 9: //填滿顏色 (油漆桶)
break;
case 10://放大鏡
break;
case 11://畫刷
break;
case 12://文字
break;
case 13://曲線
break;
case 14://多邊形
break;
case 15: //圓角矩形
RoundRect(hdc,bpt.x , bpt.y, apt.x , apt.y,(apt.x -bpt.x)/4,(apt.y- bpt.y)/4);
break;
}
}
//
// 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)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
HINSTANCE hInstance ;
BITMAP bitmap ;
// int x, y ;
static BOOL fState[2][8] ;
static BOOL fLeftButtonDown, fRightButtonDown ;
static HBITMAP hBitmap ;
static HDC hdcMem,hdcMem1, hdcMem2;
static int cxBitmap, cyBitmap, cxClient, cyClient, xMouse, yMouse,
cxSource1, cySource1, cxSource2, cySource2,
cxBlock,cyBlock,xb, yb,iState, xColor, yColor;
static HPEN hPentest1,hPentest2;
static HBITMAP hBitmap3,hBitmap1, hBitmap2;
static HBRUSH hBrush;
static RECT rect ;
static POINT apt,bpt,cpt;
static POINT ToolBMP,ColorBMP,TopBMP;
static POINT MouseFous;
//static HWND test;
switch (message)
{
case WM_CREATE:
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
//載入功能表及色塊的bmp
hBitmap3 = LoadBitmap (hInstance, TEXT ("topbmp")) ;
hBitmap1 = LoadBitmap (hInstance, TEXT ("Color")) ;
hBitmap2 = LoadBitmap (hInstance, TEXT ("ToolBar")) ;
//取得視窗大小
GetObject (hBitmap1, sizeof (BITMAP), &bitmap) ;
ColorBMP.x = bitmap.bmWidth ;
ColorBMP.y = bitmap.bmHeight ;
GetObject (hBitmap2, sizeof (BITMAP), &bitmap) ;
ToolBMP.x = bitmap.bmWidth ;
ToolBMP.y = bitmap.bmHeight ;
GetObject (hBitmap3, sizeof (BITMAP), &bitmap) ;
TopBMP.x = bitmap.bmWidth ;
TopBMP.y = bitmap.bmHeight ;
return 0 ;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hwnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hwnd);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
break;
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
/*
hdc = BeginPaint (hwnd, &ps) ;
hdcMem1 = CreateCompatibleDC (hdc) ;
hdcMem2 = CreateCompatibleDC (hdc) ;
SelectObject (hdcMem1, hBitmap1) ;
SelectObject (hdcMem2, hBitmap2) ;
BitBlt (hdc, 0, 0, cxSource2, cySource2, hdcMem2, 0, 0, SRCCOPY);
BitBlt (hdc, 56, 0, cxClient, cyClient, hdcMem, 0, cyClient-47, SRCCOPY) ;
BitBlt (hdc, 0, cyClient-46, cxSource1, cySource1, hdcMem1, 0, 0, SRCCOPY) ;
EndPaint (hwnd, &ps) ;*/
//左功能區塊的分割
cxBlock=ToolBMP.x /2;
cyBlock=215 / 8 ;
return 0 ;
case WM_LBUTTONDOWN:
/* if (!fRightButtonDown)
SetCapture (hwnd) ;
xMouse = LOWORD (lParam) ;
yMouse = HIWORD (lParam) ;
fLeftButtonDown = TRUE ;*/
hdc=GetDC(hwnd);
if (MouseFous.x
{
SelectObject(hdc,GetStockObject(WHITE_PEN));
DrawFocus(hdc, cxBlock, cyBlock, MouseFous.x, MouseFous.y);
}
MouseFous.x = LOWORD (lParam) / cxBlock ;
MouseFous.y = HIWORD (lParam) / cyBlock ;
bpt.x = LOWORD (lParam) ;
bpt.y = HIWORD (lParam) ;
if (MouseFous.x
{
fState [MouseFous.x][MouseFous.y] ^=1 ;
rect.left = MouseFous.x * cxBlock ;
rect.top = MouseFous.y * cyBlock ;
rect.right = (MouseFous.x + 1) * cxBlock ;
rect.bottom = (MouseFous.y + 1) * cyBlock ;
iState=MouseFous.y;
if(MouseFous.y==1) {iState+=8;}
SelectObject(hdc,GetStockObject(BLACK_PEN));
DrawFocus(hdc, cxBlock, cyBlock, MouseFous.x, MouseFous.y);
}
ReleaseDC(hwnd,hdc);
return 0 ;
/* case WM_LBUTTONUP:
if (fLeftButtonDown)
SetCapture (NULL) ;
fLeftButtonDown = FALSE ;
return 0 ;
*/
case WM_RBUTTONDOWN:
/*if (!fLeftButtonDown)
SetCapture (hwnd) ;
xMouse = LOWORD (lParam) ;
yMouse = HIWORD (lParam) ;
fRightButtonDown = TRUE ;
*/
return 0 ;
case WM_RBUTTONUP:
/* if (fRightButtonDown)
SetCapture (NULL) ;
fRightButtonDown = FALSE ;
*/
return 0 ;
case WM_MOUSEMOVE:
// if (!fLeftButtonDown && !fRightButtonDown)
// return 0 ;
/* hdc = GetDC (hwnd) ;
hPentest1=CreatePen(PS_SOLID,0,RGB(255,0,0));
hPentest2=CreatePen(PS_SOLID,0,RGB(0,255,0));
SelectObject (hdc, fLeftButtonDown ? hPentest1 : hPentest2) ;
SelectObject (hdcMem,fLeftButtonDown ? hPentest1 : hPentest2) ;
MoveToEx (hdc, xMouse, yMouse, NULL) ;
MoveToEx (hdcMem, xMouse, yMouse, NULL) ;
xMouse = (short) LOWORD (lParam) ;
yMouse = (short) HIWORD (lParam) ;
LineTo (hdc, xMouse, yMouse) ;
LineTo (hdcMem, xMouse, yMouse) ;
//test hbrush
hBrush=CreateSolidBrush(RGB(255,50,0));
SelectObject(hdc,hBrush);
SelectObject(hdcMem,hBrush);
RoundRect(hdc,30,30,100,100,10,10);
RoundRect(hdcMem,30,30,100,100,10,10);
ReleaseDC (hwnd, hdc) ;*/
if (wParam & MK_LBUTTON )//還要加上焦點下去判斷
{
hdc = GetDC (hwnd) ;
if (bpt.x > 2*cxBlock)
{
if (apt.x > 2*cxBlock)
{
SelectObject (hdc, GetStockObject (WHITE_PEN)) ;
Draw (hdc, apt, bpt,cpt,iState) ;
}
apt.x = LOWORD (lParam) ;
apt.y = HIWORD (lParam) ;
if (apt.x > 2*cxBlock)
{
SelectObject (hdc, GetStockObject (BLACK_PEN)) ;
Draw (hdc, apt, bpt,cpt,iState) ;
}
if(iState==3)
{
MoveToEx (hdc, bpt.x , bpt.y , NULL) ;
bpt.x = LOWORD (lParam) ;
bpt.y = HIWORD (lParam) ;
LineTo (hdc, bpt.x , bpt.y ) ;
}
}
ReleaseDC (hwnd, hdc) ;
}
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
hdcMem2 = CreateCompatibleDC (hdc) ;
SelectObject (hdcMem2, hBitmap2) ;
BitBlt (hdc, 0, 0, ToolBMP.x, ToolBMP.y, hdcMem2, 0, 0, SRCCOPY);
hdcMem = CreateCompatibleDC (hdc) ;
SelectObject (hdcMem, hBitmap3) ;
BitBlt (hdc,ToolBMP.x+1 , 0, TopBMP.x,TopBMP.y, hdcMem, 0, 0, SRCCOPY) ;
hdcMem1 = CreateCompatibleDC (hdc) ;
SelectObject (hdcMem1, hBitmap1) ;
BitBlt (hdc, 0, cyClient-46, ColorBMP.x, ColorBMP.y, hdcMem1, 0, 0, SRCCOPY) ;
DeleteDC (hdcMem1) ;
DeleteDC (hdcMem2) ;
DeleteDC (hdcMem) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
DeleteObject(hPentest1);
DeleteObject(hPentest2);
DeleteObject (hBitmap1) ;
DeleteObject (hBitmap2) ;
DeleteObject(hBrush);
DeleteObject (hBitmap3) ;
PostQuitMessage (0) ;
return 0 ;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
- Mar 31 Thu 2005 15:38
[code]小畫家第一版
close
全站熱搜
留言列表
發表留言