[windows纲]api编程

文档状态:电脑维修中....



沉浸在思想的牢笼里.


Table of Contents

windows 编程是基于消息驱动的,当消息到来时,应用程序就调用消息映射函数,但他是如何做到的?
- [乱答]这个应该是把消息的调用方提升给了操作系统,操作系统驱动API?
- 找到答案了,果然出现了控制权的移交
在程序执行时,系统会创建一个能控制他运行的数据结构,叫做pcb,pcb不断的查询消息队列
一旦出现消息,就会根据相应的消息号去调用消息映射函数

Win图形设计与接口

设备接口[图形]

代码实现

/*You should concern about the window 'class', it's not the concept of OOP
*if you are familar with c programing,it's easy for you to know the meaning
*of the 'class' , JUST A 'Struct' type....
*
*/

#include <windows.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void Ipaint(HWND);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInst,LPSTR lpszCmdLine,int nCmdShow){
    MSG msg;
    WNDCLASS wndclass;
    char classname[] ="painting....";

    wndclass.style =0;//defalut type
    wndclass.lpfnWndProc = WndProc;//msg resolve function bundle
    wndclass.cbClsExtra = 0; //no extends for window class
    wndclass.cbWndExtra=0;//not extends for window instance
    wndclass.hInstance = hInstance;// current instance handle
    wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDI_APPLICATION);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszClassName = classname;
    wndclass.lpszMenuName = NULL; // NO MENU
    if(!RegisterClass(&wndclass)){//register window class
        MessageBox(NULL,"register fault!!!","提示",MB_ICONERROR);
        return FALSE;

    }
    HWND hwnd;
    char lpszTitle[] = "绘图";
    hwnd= CreateWindow(
                classname,
                lpszTitle,
                WS_OVERLAPPEDWINDOW,// window type
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                NULL,
                NULL,
                hInstance,
                NULL
        );
    ShowWindow(hwnd,nCmdShow);//nCmdShow---> the type of appearence
    UpdateWindow(hwnd);// paint the user area
    while(GetMessage(&msg,NULL,0,0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;// additional message
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    switch(message){
        case WM_PAINT:
            Ipaint(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd,message,wParam,lParam);

    }
    return 0;
}

void Ipaint(HWND hwnd){
    PAINTSTRUCT pt;
    HDC hdc;
    HPEN hpen;
    HBRUSH hbrushHatch;
    HBRUSH hbrush;
    hdc = BeginPaint(hwnd,&pt);// get device context handle
    hpen = (HPEN)GetStockObject(BLACK_PEN);// get sys's black brush
    SelectObject(hdc,hpen);// choose the hpen in hdc
    Rectangle(hdc,60,100,100,200);
    hbrush = CreateSolidBrush(RGB(0,255,0));
    SelectObject(hdc,hbrush);
    RoundRect(hdc,150,100,200,200,20,20);// paint round rect
    DeleteObject(hbrush);
    // create cross-shaped shade brush
    hbrushHatch = CreateHatchBrush(HS_CROSS,RGB(0,0,255));
    SelectObject(hdc,hbrushHatch);
    Ellipse(hdc,300,100,500,200);
    DeleteObject(hbrushHatch);
    EndPaint(hwnd,&pt);
}

效果