关闭

windows编程之简单的画图操作

标签: windows
687人阅读 评论(0) 收藏 举报
分类:

利用windows api可以画一些简单的图形;这涉及到GDI、画笔、画刷的一些简单操作。

示例代码如下:

// test.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <stdio.h>

LRESULT CALLBACK WndProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

void paintLine(HWND hwnd);
void paintRectangle(HWND hwnd);
void paintElipse(HWND hwnd);

int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)
{
	WNDCLASS wndclass;

	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hInstance = hInstance;
	wndclass.lpfnWndProc = WndProc;
	wndclass.lpszClassName = "我的窗口";
	wndclass.lpszMenuName = NULL;
	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&wndclass); //注册窗口类

	HWND hwnd;
	hwnd = CreateWindow("我的窗口", "窗口", WS_OVERLAPPEDWINDOW, 
		0, 0, 640, 480, NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, SW_SHOWNORMAL);
	UpdateWindow(hwnd);

	MSG Msg;
	while(GetMessage(&Msg, NULL, 0, 0))
	{
		TranslateMessage(&Msg); 
        DispatchMessage(&Msg);
		paintLine(hwnd);
		paintRectangle(hwnd);
		paintElipse(hwnd);
	}

	return 0;
}

void paintLine(HWND hwnd)
{
	PAINTSTRUCT ptStr;
	HPEN hpen;
	HDC hdc;
	hdc = GetDC(hwnd);
	hpen = (HPEN)GetStockObject(WHITE_PEN);
	SelectObject(hdc, hpen);
	DeleteObject(hpen);
	LineTo(hdc, 100, 100);
	EndPaint(hwnd, &ptStr);
	ReleaseDC(hwnd, hdc);
}

void paintRectangle(HWND hwnd)
{
	PAINTSTRUCT ptStr;
	HPEN hpen;
	HBRUSH hbrush;
	HDC hdc;
	hdc = GetDC(hwnd);
	hpen = (HPEN)GetStockObject(WHITE_PEN);
	hbrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
	SelectObject(hdc, hpen);
	SelectObject(hdc, hbrush);
	Rectangle(hdc, 100, 100, 200, 200);
	DeleteObject(hpen);
	DeleteObject(hbrush);
	EndPaint(hwnd, &ptStr);
	ReleaseDC(hwnd, hdc);

}

void paintElipse(HWND hwnd)
{
	PAINTSTRUCT ptStr;
	HPEN hpen;
	HBRUSH hbrush;
	HDC hdc;
	hdc = GetDC(hwnd);
	hpen = (HPEN)GetStockObject(WHITE_PEN);
	hbrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
	SelectObject(hdc, hpen);
	SelectObject(hdc, hbrush);
	Ellipse(hdc, 200, 200, 280, 280);
	DeleteObject(hpen);
	DeleteObject(hbrush);
	EndPaint(hwnd, &ptStr);
	ReleaseDC(hwnd, hdc);
}

LRESULT CALLBACK WndProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
	switch(uMsg)
	{
	case WM_CHAR:
		char strChar[20];
		sprintf(strChar, "char is %d", wParam);
		MessageBox(hwnd, strChar, "window", MB_OK);
		break;

	case WM_LBUTTONDOWN:
		MessageBox(hwnd, "mouse left-click", "window", MB_OK);
		HDC hdc;
		hdc = GetDC(hwnd);
		TextOut(hdc, 0, 250, "hello mouse left-click", strlen("hello mouse left-click"));
		ReleaseDC(hwnd, hdc);
		break;


	case WM_RBUTTONUP:
		MessageBox(hwnd, "release mouse", "window", MB_OK);
		hdc = GetDC(hwnd);
		TextOut(hdc, 0, 300, "hello release mouse", strlen("hello release mouse"));
		ReleaseDC(hwnd, hdc);
		break;

	
	
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
	break;

default:return DefWindowProc(hwnd, uMsg, wParam, lParam);}return 0;}


总结:

感觉这些画图主要就是对画笔和画刷进行操作,再加上与图形设备接口相关的一些处理。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:148858次
    • 积分:3357
    • 等级:
    • 排名:第10026名
    • 原创:187篇
    • 转载:3篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    最新评论