6166247
This presentation is the property of its rightful owner.
Sponsored Links
1 / 38

( 第四十五讲 ) PowerPoint PPT Presentation


  • 66 Views
  • Uploaded on
  • Presentation posted in: General

( 第四十五讲 ). 吉林大学远程教育课件. Windows A P I 编 程. 学 时: 48. 主讲人 : 翟慧杰. 函数 BroadcastTransaction 是一个自定义的函数,在例 13-2 中的具体定义如下:. 在上述程序段中,函数 DdeQueryNextServer 用于获得连接列表中的下一个连接句柄。其函数原型定义如下:.

Download Presentation

( 第四十五讲 )

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


6166247

()

Windows A P I

48

:


6166247

BroadcastTransaction13-2


6166247

DdeQueryNextServer


6166247

DdeQueryNextServer while whileDdeClientTransaction DdeClientTransaction


6166247

DdeAbandonTransactionDdeAbandonTransaction

MainwindProcWM_TIMERDdePostAdviseXTYP_ADVREQDDEDdePostAdvise


6166247

132 DDE


6166247

wTypehszTopichszItemm

XTYP_CONNECTTRUEDDEDDE

DdeInitializeDDE 132


6166247

13-2

#include <windows.h>

#include <ddeml.h>

#include <stdlib.h>

#include <string.h>

#ifdef UNICODE

#define STRICMP wcsicmp

#define ITOA(c, sz, b) (itoa(sizeof(szA), szA, b),

mbstowcs(sz, szA, b), sz)

#else

#define STRICMP stricmp

#define ITOA itoa

#endif

//

HDDEDATA CALLBACK DdeCallback(WORD wType, WORD wFmt, HCONV hConv, HSZ hszTopic,

HSZ hszItem, HDDEDATA hData, DWORD lData1, DWORD lData2);


6166247

VOID PaintDemo(HWND hwnd);

LONG APIENTRY MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LONG lParam);

VOID BroadcastTransaction(PBYTE pSrc,DWORD cbData,UINT fmt,UINT xtyp);

#define BASE_TIMEOUT 100

BOOL fActive; //

DWORD idInst = 0; // DDEML

HANDLE hInst; //

HCONVLIST hConvList = 0; //

HSZ hszAppName = 0; //

HWND hwndMain; //

TCHAR szT[20]; //

#ifdef UNICODE

CHAR szA[20]; //UNICODE

TCHAR szTitle[] = TEXT("DDEmo (U)");

#else

TCHAR szTitle[] = TEXT("DDEmo");

#endif


6166247

TCHAR szApp[] = TEXT("DDEmo"); // DDE

TCHAR szPause[] = TEXT("PAUSE"); // DDE

TCHAR szResume[] = TEXT("RESUME");// DDE

UINT OurFormat; //

int InCount = 0; //

int cConvs = 0; // conversations

int count = 0; //

int cyText, cxText, cyTitle; //

//WinMain

//

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,INT nCmdShow)

{

MSG msg;

WNDCLASS wc;

TEXTMETRIC metrics;

HDC hdc;


6166247

//

wc.style = 0;

wc.lpfnWndProc = MainWndProc;

wc.cbClsExtra = 0;

wc.cbWndExtra = 0;

wc.hInstance = hInstance;

wc.hIcon = NULL;

wc.hCursor = LoadCursor(NULL, IDC_ARROW);

wc.hbrBackground = NULL;

wc.lpszMenuName = NULL;

wc.lpszClassName = szTitle;

if (!RegisterClass(&wc))

return(FALSE);

//DDE

if (DdeInitialize(&idInst,

(PFNCALLBACK)MakeProcInstance((FARPROC)DdeCallback, hInstance), APPCMD_FILTERINITS |CBF_SKIP_CONNECT_CONFIRMS |CBF_FAIL_SELFCONNECTIONS |CBF_FAIL_POKES,0))

return(FALSE);


6166247

hInst = hInstance;

//

hwndMain = CreateWindow( szTitle, szTitle,

WS_CAPTION | WS_BORDER | WS_SYSMENU,

CW_USEDEFAULT, CW_USEDEFAULT,

0, 0, NULL, NULL,

hInstance,

NULL);

if (!hwndMain)

{ DdeUninitialize(idInst);//DDE

return(FALSE);

}

hdc = GetDC(hwndMain);

GetTextMetrics(hdc, &metrics);//

cyText = metrics.tmHeight + metrics.tmExternalLeading;

cxText = metrics.tmMaxCharWidth * 8;

cyTitle = GetSystemMetrics(SM_CYCAPTION);

ReleaseDC(hwndMain, hdc);


6166247

//

hszAppName = DdeCreateStringHandle(idInst, szApp, 0);

//

OurFormat = RegisterClipboardFormat(szApp);

//

DdeNameService(idInst, hszAppName, 0, DNS_REGISTER);

//DDE

hConvList = DdeConnectList(idInst, hszAppName, hszAppName, hConvList, NULL);

//

BroadcastTransaction(NULL, 0, OurFormat, XTYP_ADVSTART);

SetWindowPos(hwndMain, 0, 0, 0, cxText,

(cyText * (cConvs + 1)) + cyTitle, SWP_NOMOVE | SWP_NOZORDER);

ShowWindow(hwndMain, nCmdShow);

UpdateWindow(hwndMain);

//


6166247

while (GetMessage(&msg, 0, 0, 0))

{ TranslateMessage(&msg);

DispatchMessage(&msg);

}

DestroyWindow(hwndMain);

UnregisterClass(szTitle, hInstance);

return(FALSE);

}

//BroadcastTransaction

//

VOID BroadcastTransaction(PBYTE pSrc,DWORD cbData,UINT fmt,UINT xtyp)

{ HCONV hConv;

DWORD dwResult;

int cConvsOrg;

cConvsOrg = cConvs;

cConvs = 0;//0

if (hConvList)


6166247

{//

hConv = DdeQueryNextServer(hConvList, 0);

while (hConv)

{

cConvs++;

//

if (DdeClientTransaction(pSrc, cbData, hConv, hszAppName, fmt, xtyp, TIMEOUT_ASYNC, &dwResult))

{

//

DdeAbandonTransaction(idInst, hConv, dwResult);

}

hConv = DdeQueryNextServer(hConvList, hConv);

}

}

if (cConvs != cConvsOrg)


6166247

{//

InvalidateRect(hwndMain, NULL, TRUE);

}

}

//MyProcessKey

//

VOID MyProcessKey(TCHAR tchCode,LONG lKeyData)

{

switch (tchCode)

{


6166247

case TEXT('B'):

case TEXT('b'):

*((PBYTE)(-1)) = 0;

break;}}

//MainWndProc

//

LONG APIENTRY MainWndProc(HWND hwnd,UINT message,WPARAM wParam,LONG lParam)

{ RECT rc;

switch (message)

{


6166247

case WM_CREATE://

fActive = FALSE;

break;

case WM_RBUTTONDOWN: //

if (GetKeyState(VK_CONTROL) & 0x8000) //

{


6166247

//

BroadcastTransaction((PBYTE)szPause, sizeof(szPause), 0, XTYP_EXECUTE);

MessageBeep(0);

}

KillTimer(hwndMain, 1);//

fActive = FALSE;

InvalidateRect(hwnd, NULL, TRUE);//

UpdateWindow(hwnd);

break;

case WM_LBUTTONDOWN: //

if (GetKeyState(VK_CONTROL) & 0x8000) //

{

//

BroadcastTransaction((PBYTE)szResume, sizeof(szResume), 0,

XTYP_EXECUTE);

MessageBeep(0); //

}


6166247

//

SetTimer(hwndMain, 1, BASE_TIMEOUT + (rand() & 0xff), NULL);

fActive = TRUE;

InvalidateRect(hwnd, NULL, TRUE);//

UpdateWindow(hwnd);

break;

case WM_CHAR://

MyProcessKey((TCHAR)wParam, lParam);

break;

case WM_TIMER://

count++;

//XTYP_ADVREQ

DdePostAdvise(idInst, hszAppName, hszAppName);

SetRect(&rc, 0, 0, cxText, cyText);

InvalidateRect(hwndMain, &rc, TRUE);//

UpdateWindow(hwndMain);

break;


6166247

case WM_PAINT://

PaintDemo(hwnd);

break;

case WM_CLOSE://

KillTimer(hwnd, 1);

DdeDisconnectList(hConvList);//DDE

//

DdeNameService(idInst, 0, 0, DNS_UNREGISTER);

//

DdeFreeStringHandle(idInst, hszAppName);

DdeUninitialize(idInst);//DDE

PostQuitMessage(0);

break;

default://

return (DefWindowProc(hwnd, message, wParam, lParam));

}

return(0);

}


6166247

//PaintDemo

//

VOID PaintDemo(HWND hwnd)

{

PAINTSTRUCT ps;

RECT rc;

HCONV hConv;

CONVINFO ci;

int cConvsOrg = cConvs;

BeginPaint(hwnd, &ps);//

SetRect(&rc, 0, 0, cxText, cyText);//

SetBkMode(ps.hdc, TRANSPARENT);//

SetTextColor(ps.hdc, 0x00FFFFFF); //

//

FillRect(ps.hdc, &rc, (HBRUSH)GetStockObject(fActive ? BLACK_BRUSH : GRAY_BRUSH));


6166247

//

DrawText(ps.hdc, ITOA(count, szT, 10), -1, &rc, DT_CENTER | DT_VCENTER);

if (hConvList)

{

OffsetRect(&rc, 0, cyText);

SetTextColor(ps.hdc, 0); //

cConvs = 0;

//

hConv = DdeQueryNextServer(hConvList, 0);

while (hConv)

{

cConvs++;

ci.cb = sizeof(CONVINFO);

//DDE

DdeQueryConvInfo(hConv, QID_SYNC, &ci);

FillRect(ps.hdc,&rc, (HBRUSH)GetStockObject(WHITE_BRUSH)); // 13-2


6166247

//

DrawText(ps.hdc, ITOA(ci.hUser, szT, 10), -1, &rc,DT_CENTER | DT_VCENTER);

OffsetRect(&rc, 0, cyText);

hConv = DdeQueryNextServer(hConvList, hConv);

}

}

EndPaint(hwnd, &ps);//

if (cConvsOrg != cConvs)

{

SetWindowPos(hwndMain, 0, 0, 0, cxText, (cyText * (cConvs + 1)) + cyTitle,SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);

}

}

//DdeCallback

//DDE


6166247

HDDEDATA CALLBACK DdeCallback(WORD wType,WORD wFmt,HCONV hConv,HSZ hszTopic,HSZ hszItem,HDDEDATA hData,DWORD lData1,DWORD lData2)

{LPTSTR pszExec;

switch (wType)

{ case XTYP_CONNECT://XTYP_CONNECT

return((HDDEDATA)TRUE);

case XTYP_ADVREQ:

case XTYP_REQUEST:

//

return(DdeCreateDataHandle(idInst,(PBYTE)&count, sizeof(count), 0, hszAppName, OurFormat, 0));

case XTYP_ADVSTART:

return(HDDEDATA) ((UINT)wFmt == OurFormat && hszItem == hszAppName);

case XTYP_ADVDATA:

if (DdeGetData(hData, (PBYTE)&InCount, sizeof(InCount), 0)) {


6166247

//

DdeSetUserHandle(hConv, QID_SYNC, InCount);

}

InvalidateRect(hwndMain, NULL, TRUE);

return((HDDEDATA)DDE_FACK);

case XTYP_EXECUTE:

//DDE

pszExec = (LPTSTR)DdeAccessData(hData, NULL);

if (pszExec)

{

if (fActive && !STRICMP(szPause, pszExec))

{

//

KillTimer(hwndMain, 1);

fActive = FALSE;

InvalidateRect(hwndMain, NULL, TRUE);

UpdateWindow(hwndMain);

}


6166247

else

if (!fActive && !STRICMP(szResume, pszExec))

{

//

SetTimer(hwndMain, 1, BASE_TIMEOUT + (rand() & 0xff), NULL);

fActive = TRUE;

InvalidateRect(hwndMain, NULL, TRUE);

UpdateWindow(hwndMain);

}

MessageBeep(0);

}

break;

case XTYP_DISCONNECT:

InvalidateRect(hwndMain, NULL, TRUE);

break;

case XTYP_REGISTER:


6166247

//

BroadcastTransaction( NULL, 0, OurFormat, XTYP_ADVSTOP );

hConvList = DdeConnectList(idInst, hszItem, hszAppName, hConvList, NULL);

BroadcastTransaction(NULL, 0, OurFormat, XTYP_ADVSTART);

SetWindowPos(hwndMain, 0, 0, 0, cxText,(cyText * (cConvs + 1)) + cyTitle, SWP_NOMOVE | SWP_NOZORDER);

UpdateWindow(hwndMain);

return((HDDEDATA)TRUE);

}return(0);

}


6166247

13-213-2


6166247

133

1131

OLEOLEOLEOLE


6166247

OLE

OLE


  • Login