吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4197|回复: 3
收起左侧

[其他转载] win程序设计笔记5

[复制链接]
zapline 发表于 2009-2-10 03:51
GDI:Graphics Device Interface

GDI函数分五大类:
1,取得(或者建立)和释放(或者清除)对象内容的函数
2,取得有关对象内容信息的函数
3,绘图函数
4,设定和取得对象内容参数的函数
5,使用GDI对象的函数

GDI基本图形分四类:
1,直线和曲线
2,填入区域
3,点阵图
4,文字

想在一个图形输出设备(诸如萤幕或者印表机)上绘图时,您首先必须获得一个对象内容(或者DC)的代号
1,最常用的取得并释放装置内容代号的方法是,在处理WM_PAINT讯息时,使用BeginPaint和EndPaint呼叫:
hdc = BeginPaint (hwnd, &ps) ;
***
EndPaint (hwnd, &ps) ;
2,Windows程式还可以在处理非WM_PAINT讯息时取得装置内容代号:
hdc = GetDC (hwnd) ;
***
ReleaseDC (hwnd, hdc) ;
3,Windows程式还可以取得适用於整个视窗(而不仅限於视窗的显示区域)的装置内容代号:
hdc = GetWindowDC (hwnd) ;
***
ReleaseDC (hwnd, hdc) ;
4,BeginPaint、GetDC和GetWindowDC获得的装置内容都与视讯显示器上的某个特定视窗相关。取得装置内容代号的另一个更通用的函式是CreateDC:
hdc = CreateDC (pszDriver, pszDevice, pszOutput, pData) ;
***
DeleteDC (hdc) ;
5,使用点阵图时,取得一个「记忆体装置内容」有时是有用的:
hdcMem = CreateCompatibleDC (hdc) ;
***
DeleteDC (hdcMem) ;
6,metafile是一些GDI呼叫的集合,以二进位形式编码,可以通过取得metafile装置内容来建立metafile:
hdcMeta = CreateMetaFile (pszFilename) ;
***
hmf = CloseMetaFile (hdcMeta) ;

可以通过呼叫GetDeviceCaps(「取得设备功能」)函式来取得一个对象的信息:
iValue = GetDeviceCaps (hdc, iIndex) ;
其中,参数iIndex取值为WINGDI.H表头档案中定义的29个识别字之一

关于色彩
获得色彩平面的数目:
iPlanes = GetDeviceCaps (hdc, PLANES) ;
获得每个图素的色彩位元数:
iBitsPixel = GetDeviceCaps (hdc, BITSPIXEL) ;
得到的色彩数值:
iColors = GetDeviceCaps (hdc, NUMCOLORS) ;

画点和线
在指定的x和y座标以特定的颜色设定图素:
SetPixel (hdc, x, y, crColor) ;
传回指定座标处的图素颜色:
crColor = GetPixel (hdc, x, y) ;
画一条直线,必须呼叫两个函数。第一个函式指定了线的开始点,第二个函式指定了线的终点:
MoveToEx (hdc, xBeg, yBeg, NULL) ;
LineTo (hdc, xEnd, yEnd) ;

需要目前位置,就可以通过以下呼叫获得:
GetCurrentPositionEx (hdc, &pt) ;
画一个网格,线与线之间相隔100个图素:
GetClientRect (hwnd, &rect) ;
for (        x = 0 ; x < rect.right ; x+= 100)
{
        MoveToEx (hdc, x, 0, NULL) ;
           LineTo (hdc, x, rect.bottom) ;
}
for (y = 0 ; y < rect.bottom ; y += 100)
{
        MoveToEx (hdc, 0, y, NULL) ;
        LineTo (hdc, rect.right, y) ;
}
画一个矩形的边界框:
POINT apt[5] = { 100, 100, 200, 100, 200, 200, 100, 200, 100, 100 } ;
MoveToEx (hdc, apt[0].x, apt[0].y, NULL) ;
for (        i = 1 ; i < 5 ; i++)
        LineTo (hdc, apt[i].x, apt[i].y) ;
或者
POINT apt[5] = { 100, 100, 200, 100, 200, 200, 100, 200, 100, 100 } ;
Polyline (hdc, apt, 5) ;
或者
MoveToEx (hdc, apt[0].x, apt[0].y, NULL) ;
PolylineTo (hdc, apt + 1, 4) ;

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| zapline 发表于 2009-2-10 04:13

练习一下,写了个小程序,画X
#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
VOID DrawLine (HDC hdc, int x1, int y1, int x2, int y2);

int WINAPI WinMain (        HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                        PSTR szCmdLine, int iCmdShow)
{
        static TCHAR szAppName[] = TEXT ("SineWave") ;
        HWND                 hwnd ;
        MSG                  msg ;
        WNDCLASS         wndclass ;

        wndclass.style                = CS_HREDRAW | CS_VREDRAW ;
        wndclass.lpfnWndProc= WndProc ;
        wndclass.cbClsExtra        = 0 ;
        wndclass.cbWndExtra        = 0 ;
        wndclass.hInstance        = hInstance ;
        wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
        wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW) ;
        wndclass.hbrBackground        = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName  = NULL ;
        wndclass.lpszClassName = szAppName ;

        RegisterClass (&wndclass);

        hwnd = CreateWindow (        szAppName, TEXT ("BRAW LINE"),
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT, CW_USEDEFAULT,
                300, 300,
                NULL, NULL, hInstance, NULL) ;
        ShowWindow (hwnd, iCmdShow) ;
        UpdateWindow (hwnd) ;

        while (GetMessage (&msg, NULL, 0, 0))
        {
                TranslateMessage (&msg) ;
                DispatchMessage (&msg) ;
        }
        return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        HDC         hdc ;
        PAINTSTRUCT ps ;;

        switch (message)
        {
        case         WM_SIZE:
                return 0 ;

        case         WM_PAINT:
                hdc = BeginPaint (hwnd, &ps) ;
                DrawLine(hdc,1,1,300,300);
                DrawLine(hdc,1,300,300,1);
                EndPaint (hwnd, &ps) ;
                return 0 ;

        case         WM_DESTROY:
                PostQuitMessage (0) ;
                return 0 ;
        }
        return DefWindowProc (hwnd, message, wParam, lParam) ;
}

VOID DrawLine (HDC hdc, int x1, int y1, int x2, int y2)
{
        MoveToEx (hdc, x1, y1, NULL) ;
        LineTo (hdc, x2, y2) ;
}
QQ截图未命名.jpg

[ 本帖最后由 zapline 于 2009-2-10 04:14 编辑 ]
惜双双 发表于 2009-2-10 14:32
呵呵。。楼主很强。。

佩服一个先。。

我直接从MFC起家了。

这些SDK看起来有点陌生:L
lovely 发表于 2009-2-11 23:48
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 07:30

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表