当前位置:早雪网网络学院编程文档其他语言 → windowAPI笔记(1)

windowAPI笔记(1)

减小字体 增大字体 作者:未知  来源:supcode.com收集整理  发布时间:2005-7-1 14:55:12

走了一趟MFC,虽然MFC封装了的类很好用,但是总是感觉到对MFC的了解只能停留在表面上,很难进一步地理解MFC.关键原因还是自己对windowsAPI学习的不够,现在打算丢掉MFC一段时间,先还是把windowAPI回顾一便,加深点印象.

特地在网上下了本大师侯捷的<<windows程序设计>>,打算结合这本书在学好window的基础的同时把多线程和dll.但是光看书是不够的,必须在看书的时候自己动手设计点东西出来.所以我打算设计一个小软件同时包含dll,多线程,等基本的技术.现在的基本设想是搞一个类QQ的通信软件,现在还只是一个设想,基本的框架和功能还没有设计,过几天来具体设计一下.用API难度比较大,不过慢慢来,一口吃不一个胖子,时间长点应该没关系.相信我能坚持把这个软件做完的.

////引用侯捷老师书上的开始一段,也是入们的一段

#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 ("HelloWin") ;
 
    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,      // window class name
       
  TEXT ("The Hello Program"),   // window caption
       
  WS_OVERLAPPEDWINDOW,  // window style
       
  CW_USEDEFAULT,// initial x position
       
  CW_USEDEFAULT,// initial y position
       
  CW_USEDEFAULT,// initial x size
       
  CW_USEDEFAULT,// initial y size
       
  NULL,                 // parent window handle
       
  NULL,            // window menu handle
       
  hInstance,   // program instance handle
       
  NULL) ;      // creation parameters
 
 
 
    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 ;
 
    RECT          rect ;
 
 
 
    switch (message)
       
    {
       
    case WM_CREATE:
       
  PlaySound (TEXT ("hellowin.mp3"), NULL, SND_FILENAME | SND_ASYNC) ;
       
  return 0 ;
       
  
    case   WM_PAINT:
       
  hdc = BeginPaint (hwnd, &ps) ;
       
       
       
  GetClientRect (hwnd, &rect) ;
       
       
       
  DrawText (hdc, TEXT ("Hello, Windows 2000!"), -1, &rect,
   
   DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
       
  EndPaint (hwnd, &ps) ;
       
  return 0 ;
       
       
       
    case   WM_DESTROY:
       
  PostQuitMessage (0) ;
       
  return 0 ;
       
    }
 
 return DefWindowProc (hwnd, message, wParam, lParam) ;
 
}

///////////////////////////////////////--------------------------------------------------

对没个细节的说明

窗口的设计就不说了,以前对这个看得太熟悉了

对句柄的一点小误会澄清一下

以前老是弄不清楚

HWND和HINSTANCE

hinstance是进程本身的句柄,牛的windows核心编程上说就是进程的在内存中开始的地址而HWND是指向一个窗口的句柄

三个大写标识符,用于不同型态的「句柄」:

标识符

含义

HINSTANCE

执行实体(程序自身)句柄

HWND

窗口句柄

HDC

设备内容句柄

下面的是匈牙利表示法的一些列

前缀

数据型态

c

char或WCHAR或TCHAR

by

BYTE (无正负号字符)

n

short

i

int

x, y

int分别用作x坐标和y坐标

cx, cy

int分别用作x长度和y长度;C代表「计数器」

b或f

BOOL (int);f代表「旗标」

w

WORD (无正负号短整数)

l

LONG (长整数)

dw

DWORD (无正负号长整数)

fn

function(函数)

s

string(字符串)

sz

以字节值0结尾的字符串

h

句柄

p

指标

在窗口创建之前要调用RegisterClass

以前对RegisterClass也不是很了解,只是知道有这一步

MSDN:

The RegisterClass function registers a window class for subsequent use in calls to the CreateWindow or CreateWindowEx function.(RegisterClass函数来Register一个窗口类为后面的CreateWindow or CreateWindowEx 函数来调用)

大致意思是如果不Register那么CreateWindow or CreateWindowEx 函数就无法创建你想要的窗口

以前对CreateWindow or CreateWindowEx 也很熟悉了,这里就不在累赘了.

ShowWindow在MSDN的"注意"上上是这样说明的

To perform certain special effects when showing or hiding a window, use AnimateWindow.

The first time an application calls ShowWindow, it should use the WinMain function's nCmdShow parameter as its nCmdShow parameter. Subsequent calls to ShowWindow must use one of the values in the given list, instead of the one specified by the WinMain function's nCmdShow parameter.

As noted in the discussion of the nCmdShow parameter, the nCmdShow value is ignored in the first call to ShowWindow if the program that launched the application specifies startup information in the structure. In this case, ShowWindow uses the information specified in the STARTUPINFO structure to show the window. On subsequent calls, the application must call ShowWindow with nCmdShow set to SW_SHOWDEFAULT to use the startup information provided by the program that launched the application. This behavior is designed for the following situations:

  • Applications create their main window by calling CreateWindow with the WS_VISIBLE flag set.
  • Applications create their main window by calling CreateWindow with the WS_VISIBLE flag cleared, and later call ShowWindow with the SW_SHOW flag set to make it visible.

The RegisterClass function registers a

[1] [2]  下一页


Tags:windowAPI,笔记
[数据载入中...] [返回上一页] [打 印]