windows编程——画键盘

来源:互联网 发布:火车票生成器软件 编辑:程序博客网 时间:2024/06/04 19:49
#include<windows.h>
#define sourcexw    150
#define    sourceyh    150//键盘的起始位置
#define    wkeyw    40
#define    hkeyh    40//基本键的高 宽
#define keyt    60    //定义tab的宽度;
#define wkeyE    70    //定义enter建的宽度;
#define wkeyS    100    //定义shift的宽度;
#define wkeyK    280    //定义空格键的宽度;
#define totalKeyw    600
#define totalKeyh    230//键盘的大小
#define th    3
#define tw    3

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
    static TCHAR szAppName[]=TEXT("HelloWin");
HWND hwnd;
//HCURSOR NH=(HCURSOR)LoadImage(hInstance,"C://桌面//C_NO04.CUR",IMAGE_CURSOR,0,0,LR_DEFAULTCOLOR);
MSG msg;
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbWndExtra=0;
wndclass.cbClsExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(hInstance,"IDI_ICON1");
wndclass.hCursor=LoadCursor(hInstance,"IDC_CURSOR1");
//wndclass.hCursor=NH;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
//wndclass.hbrBackground=(HBRUSH)CreateHatchBrush(HS_HORIZONTAL,RGB(255,0,0));
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;
if(! RegisterClass(&wndclass))
{MessageBox(NULL,TEXT("This program requires Windows nt!"),szAppName,MB_ICONERROR);
 return 0;
}
hwnd=CreateWindow(szAppName,TEXT("The Hello Program"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
                CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{ TranslateMessage(&msg);
  DispatchMessage(&msg);
}
return msg.wParam ;
}
/*RECT rect[40];
rect[0].left=
rect[0].right=rect[0].left+40;
rect[0].top=
rect[0].left=*/


/*画一个按钮的函数*/

void CreateKeyBoard(HDC hdc,int sourcex,int sourcey,int wkey,int hkey,LPCTSTR str)
{     HBRUSH    hbr;
     HPEN hpen1,hpen2;
     hbr=CreateSolidBrush(RGB(192,192,192));
     SelectObject(hdc,hbr);    
     Rectangle(hdc,sourcex+tw,sourcey,sourcex+wkey,sourcey+hkey);
     hpen1=CreatePen(PS_SOLID,3,RGB(255,255,255));
     hpen2=CreatePen(PS_SOLID,3,RGB(0,0,0));
        SelectObject(hdc,hpen1);
     MoveToEx(hdc,sourcex,sourcey,NULL);
     LineTo(hdc,sourcex+wkey,sourcey);
     SelectObject(hdc,hpen2);
     LineTo(hdc,sourcex+wkey,sourcey+hkey);
     LineTo(hdc,sourcex+tw,sourcey+hkey);
     SelectObject(hdc,hpen1);
     LineTo(hdc,sourcex+tw,sourcey);
     SetBkMode(hdc,TRANSPARENT);
     TextOut(hdc,sourcex+10,sourcey+10,str,lstrlen(str));
    
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
    /*int sourcex=200;
    int sourcey=200;//定义键盘起点;
    int wkeyb=585;
    int hkeyb=205;//定义键盘的宽,高;
    int wkey=40;
    int hkey=40;//定义基本键盘的宽,高;*/
    
    HDC  hdc;
 PAINTSTRUCT ps;

     LPCTSTR str1[]={"~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "="};
     LPCSTR str2[]={"Q","W","E","R","T","Y","U","I","O","P","[","]","//"};
     LPCSTR str3[]={"A","S","D","F","G","H","J","k","L",";","/'"};
     LPCSTR str4[]={"Z","X","C","V","B","N","M","<",">","?"};
    HBRUSH hbr1=CreateSolidBrush(RGB(255,0,0));
    HBRUSH hbr2=CreateSolidBrush(RGB(0,255,0));
    HBRUSH hbr3=CreateSolidBrush(RGB(0,0,255));
    HBRUSH hbr4=CreateHatchBrush(HS_CROSS,RGB(250,50,100));
        POINT pt[]={{200,400},{300,500},{300,600},{100,600},{100,500}};
 switch(message)
 {
 case WM_PAINT:
     hdc=BeginPaint(hwnd,&ps);
     /*画一个椭圆*/
     SelectObject(hdc,hbr1);
     Ellipse(hdc,400,400,600,500);
     SelectObject(hdc,hbr2);
     Rectangle(hdc,400,500,600,600);
     SelectObject(hdc,hbr3);
     Polygon(hdc,pt,5);
     SelectObject(hdc,hbr4);
     RoundRect(hdc,700,400,800,500,(800-700)/4,(500-400)/4);
     Chord(hdc,700,550,800,700,50,50,50,50);




     /*定义一个画键盘按键的程序*/
   /*  HBRUSH    hbr;
     HPEN hpen1,hpen2;
     hbr=CreateSolidBrush(RGB(192,192,192));
     SelectObject(hdc,hbr);    
     Rectangle(hdc,20,20,60,60);
     //GetStockObject(WHITE_PEN);
     hpen1=CreatePen(PS_SOLID,1,RGB(255,255,255));
     hpen2=CreatePen(PS_SOLID,2,RGB(0,0,0));
        SelectObject(hdc,hpen1);
     MoveToEx(hdc,20,20,NULL);
     LineTo(hdc,60,20);
     SelectObject(hdc,hpen2);
     LineTo(hdc,60,60);
     LineTo(hdc,20,60);
     SelectObject(hdc,hpen1);
     LineTo(hdc,20,20);*/



     int i;
       i=0;
        CreateKeyBoard(hdc,sourcexw-10,sourceyh-10,totalKeyw,totalKeyh," ");
     for(i=0;i<13;i++)
     {
       CreateKeyBoard(hdc,sourcexw+wkeyw*i,sourceyh,wkeyw,hkeyh,str1[i]);
     }
     CreateKeyBoard(hdc,sourcexw+wkeyw*13,sourceyh,keyt,hkeyh,"退格");
    
     CreateKeyBoard(hdc,sourcexw,sourceyh+hkeyh+th,keyt,hkeyh,"Tab");
      for(i=0;i<13;i++)
     {
      CreateKeyBoard(hdc,sourcexw+wkeyw*i+keyt,sourceyh+hkeyh+th,wkeyw,hkeyh,str2[i]);
     }
     CreateKeyBoard(hdc,sourcexw,sourceyh+(hkeyh+th)*2,wkeyE,hkeyh,"Caps");
     for(i=0;i<11;i++)
     {
         CreateKeyBoard(hdc,sourcexw+wkeyw*i+wkeyE,sourceyh+(hkeyh+th)*2,wkeyw,hkeyh,str3[i]);
     }
     CreateKeyBoard(hdc,sourcexw+wkeyw*11+wkeyE,sourceyh+(hkeyh+th)*2,wkeyE,hkeyh,"Enter");
    CreateKeyBoard(hdc,sourcexw,sourceyh+(hkeyh+th)*3,wkeyS,hkeyh,"Shift");
 for(i=0;i<10;i++)
     {
         CreateKeyBoard(hdc,sourcexw+wkeyw*i+wkeyS,sourceyh+(hkeyh+th)*3,wkeyw,hkeyh,str4[i]);
     }
   CreateKeyBoard(hdc,sourcexw,sourceyh+(hkeyh+th)*4,keyt,hkeyh,"Ins");

CreateKeyBoard(hdc,sourcexw+wkeyS,sourceyh+(hkeyh+th)*4,keyt,wkeyw,"Del");
    CreateKeyBoard(hdc,sourcexw+wkeyS+keyt,sourceyh+(hkeyh+th)*4,wkeyK,hkeyh,"");
CreateKeyBoard(hdc,sourcexw+wkeyw*13,sourceyh+(hkeyh+th)*4,keyt,wkeyw,"ESC");

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