自己完成的第一个程序,真心不容易。。。通过我大概齐学了6章之后,第一个windows程序终于做出来了。。。
今天实在是挺高兴的,发个博客记录一下,既是激励自己努力学习,也是肯定下一下自己真的坚持到了写出第一个windows程序。。
以前看windows程序都是看一个星期之后,觉得写不出来东西就坚持不下去了。。。现在我也能挺过来,做出点东西,好,很好,继续努力中...............
程序内容大概就是 第一行用textout输出“请用键盘输入:”,然后下一行输出键盘输入的程序。
程序大概就是这个样子,后来发现还能改进,就是把判断回车键放到里面,这样子做起来好像就是dos下面的模拟操作了。。
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("KeyView1") ;
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 ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Keyboard Message Viewer #1"),
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 ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxClientMax, cyClientMax, cxClient, cyClient, cxChar, cyChar , cLinesMax;
HDC hdc ;
int i = 0, iType, j ;
PAINTSTRUCT ps ;
static TCHAR szBuffer[70] ; //szKeyName [32] ;
TEXTMETRIC tm ;
static RECT rectScroll ;
static int * tpmsg, *ina;
static int isz = 0;
TCHAR szTop[ 20 ] = L"请用键盘输入:" ;
switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd) ;
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ; //等宽字体
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight ;
ReleaseDC (hwnd, hdc) ;
return 0;
/*case WM_DISPLAYCHANGE: //窗口分辨率改变时会发送这个消息
// Get maximum size of client area
cxClientMax = GetSystemMetrics (SM_CXMAXIMIZED) ;
cyClientMax = GetSystemMetrics (SM_CYMAXIMIZED) ;
//窗口最大化缺省值
// Get character size for fixed-pitch font */
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
// Calculate scrolling rectangle
if( tpmsg )
free( tpmsg );
cLinesMax = cxClient / cxChar ;
tpmsg = ( int * ) malloc ( cLinesMax * sizeof( int ) ) ;
rectScroll.left = 0 ;
rectScroll.right = cxClient ;
rectScroll.top = cyChar ;
rectScroll.bottom = 2 * cyChar;
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case WM_CHAR:
switch( wParam )
{
case VK_RETURN:
break;
default:
szBuffer[ isz++ ] = wParam ;
InvalidateRect( hwnd, &rectScroll, TRUE ) ; //是第二行变为无效区域,并把WM_PAINT消息放入队列
UpdateWindow( hwnd ) ; //判断是否有无效区域( WM_PAINT ),如果有就立刻刷新屏幕
break;
}
return 0;
case WM_PAINT:
hdc = BeginPaint( hwnd, &ps ) ;
SelectObject( hdc, GetStockObject( SYSTEM_FIXED_FONT ) );
SetBkMode( hdc, TRANSPARENT ) ;
TextOut( hdc, 0, 0, szTop, lstrlen( szTop ) ) ;
TextOut( hdc, 0, cyChar, szBuffer, lstrlen( szBuffer ) ) ;
EndPaint( hwnd, &ps ) ;
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}